In may rails application i wanted to use delayed job. SO i installed delayed jobs as follows.
in gemfile
gem 'delayed_job_active_record', '0.4.3'
then in console
rails generate delayed_job:active_record
rake db:create
It had created delayed_jobs table in database.
And i started
rake jobs:work
Then i added the following code in controller:
Task.handle_asynchronously :in_the_future, :run_at => Proc.new { 5.minutes.from_now }
here Task is the model name.
And in the model task.rb i wrote
def in_the_future
self.update_attiributes(:status=> "updated")
end
After running controller method it's not creating any record in the delayed_jobs table. Please correct me if i am doing anything wrong.
Can You change the controller code to
Task.delay(:run_at => Proc.new { 5.minutes.from_now }).in_the_future
Related
I followed this article(How to schedule a function to execute at a future time?) to create schedule a method execution every couple of minutes.
I added the bellow code after my update method in the model controller.
handle_asynchronously :update_delay, :run_at => Proc.new { 1.minutes.from_now }
When i refresh the page i get this error
undefined method `handle_asynchronously'
Your linked post says
With DelayedJob you can do something like
So the method handle_asynchronously is probably a helper served by delayed_job. You should install the gem
#Gemfile
gem 'delayed_job_active_record'
Run
bundle install
and
rails generate delayed_job:active_record
rake db:migrate
I am trying to run the command rake db:migrate using a sidekiq worker but it seems like it just won't work and I am curious if there is a way to do this or not. I am creating a scaffold using sidekiq but cannot migrate it afterwards
This works
class ScaffoldGeneratorWorker
include Sidekiq::Worker
def perform(id)
`rails g scaffold test_#{id} title:string body:text slug:string visible:boolean`
end
end
But I cannot get this to run afterwards and work
class DatabaseMigrationWorker
include Sidekiq::Worker
def perform
`rake db:migrate`
end
end
Is this possible, and, if so, how can I get it to work. Any help is greatly appreciated.
First you should load rake tasks, then invoke:
class DatabaseMigrationWorker
include Sidekiq::Worker
def perform
Name_Of_Your_App::Application.load_tasks
Rake::Task['db:migrate'].invoke
end
end
This code automagically loads the Rake tasks for your Rails application without you even knowing how your application is named (this was the case for me). It also makes the code easier to share between various Rails projects.
class MySidekiqTask
include Sidekiq::Worker
def perform
application_name = Rails.application.class.parent_name
application = Object.const_get(application_name)
application::Application.load_tasks
Rake::Task['db:migrate'].invoke
end
end
If you need to invoke the Rake task with parameters, you can simply pass them in through the invoke method (https://www.rubydoc.info/gems/rake/Rake%2FTask:invoke):
Rake::Task['db:migrate'].invoke(params)
Did you try adding require 'rake' at the top of your file?
a possible duplicate of How do I run rake tasks within my rails application
I'm using the gem 'acts_as_tenant' in a Rails 3 project.
I would like to seed new data every time a new Tenant is added - but only for the new Tenant.
Is there a way to pass a variable to rake db:seed?
Something like:
rake db:seed -tenant=5
Thanks for the help!
This worked:
$ TENANT=5 rake db:seed
In seed.rb:
statuscode = Statuscode.first_or_create(
:tenant_id => ENV['TENANT'],
:statuscode => 'Completed',
:closed => true
)
I am using resque in my application for delayed jobs, where i cant send emails & sms to bulk number of users asynchronously. And the data is stored in mongodb, mongoid is the ODM connects rails & mongo.
My mongoid model looks like this
class Item
include Mongoid::Document
include Geo::LocationHelper
field :name, :type => String
field :desc, :type => String
#resque queue name
#queue = :item_notification
#resque perform method
def self.perform(item_id)
#item = Item.find(item_id)
end
end
I can able to add jobs to resque, i have verified using resque-web. Whenever i start an resque-worker
QUEUE=item_notification rake resque:work
i got the uninitialized constant Item , since i am using resque as rails gem and starting rake in rails root, i believe my mongoid models should be loaded.
After digging lot, i found that we can explicitly ask rake to load the environment by
QUEUE=item_notification rake environment resque:work
but now also i got the same error uninitialized constant Item
can someone help me out?
and my
Actually, its a problem in dev environment. after adding this line to into resque.rake task file
# load the Rails app all the time
namespace :resque do
puts "Loading Rails environment for Resque"
task :setup => :environment
ActiveRecord::Base.send(:descendants).each { |klass| klass.columns }
end
it works fine
The code taken from GitHub-Resque-Wiki
How to call few model methods and send emails through my rake task?
In your rake task you need depend of environment. After you can use all of you Model.
task :my_task => [:environment] do
User.all
end