问题
I am using delayed_job and delayed_job_active_record for back ground job execution in my rails application. We are using queue based delayed_job. For starting the delayed I am using the following command.
RAILS_ENV=staging script/delayed_job -i=1 --queue=queue_name start
The problem is below query is firing infinitely.
SQL (0.4ms) UPDATE `delayed_jobs` SET `locked_at` = '2013-04-16 09:27:23', `locked_by` = 'delayed_job.=2 host:ip-10-204-210-77 pid:2168' WHERE `delayed_jobs`.`queue` IN ('queue_name') AND ((run_at <= '2013-04-16 09:27:23' AND (locked_at IS NULL OR locked_at < '2013-04-16 05:27:23') OR locked_by = 'delayed_job.=2 host:ip-10-204-210-77 pid:2168') AND failed_at IS NULL) ORDER BY priority ASC, run_at ASC LIMIT 1
And the delayed_job count is zero. Because of this the application is very slow and the pages are not loading in many places.
回答1:
I think what you meant is that delayed_job
polls too frequently (which by the way is every 5
seconds by default) - I know that fills up your log, and seems "infinite".. :)
If that is what you meant, then I suggest you run the workless gem. It will only start delayed_job
on a per need basis. Many people use it to keep their Heroku worker dynos from running idle, but it works just as well in development
mode.
Note that if you are using delayed_job_active_record, you also need to add gem 'daemons'
to your Gemfile
(daemons). See the Running Jobs section of delayed_job.
Thus, your Gemfile
would contain:
gem 'delayed_job_active_record'
gem 'daemons'
gem 'workless'
If you need more guidance, let me know in the comments below.
回答2:
i had to use AR's silence
method, just change in file [path/to/delayed_job_active_record/gem]/delayed_job_active_record-[any.latest.version]/lib/delayed/backend/active_record.rb
around line 68:
count = ready_scope.limit(1).update_all(:locked_at => now, :locked_by => worker.name)
to
count = silence {ready_scope.limit(1).update_all(:locked_at => now, :locked_by => worker.name)}
dirty solution, i know, but it works... welcome to propose a better wrapper, but for me Job.reserve
method there is big enough to kill any thoughts to override it in config/initializers
回答3:
So this is a query specifically designed for Postgres. Please refer to https://github.com/collectiveidea/delayed_job_active_record/blob/master/lib/delayed/backend/active_record.rb#L57 for why it has to be like that.
The idea of delayed job is indeed that it will query the db periodically, so the query in your question is expected to be fired as long as the worker is running. This should happen every second and I can't imagine that this has significant influence on the performance of your app.
Are you running on very limited hardware like a very small virtual machine?
回答4:
I faced same issue and i resolved it by adding index on queue field.
def self.up
create_table :delayed_jobs, :force => true do |table|
# Add index on queue field
add_index :delayed_jobs, [:queue], :name => 'delayed_jobs_queue'
end
For more information visit below document http://himarsh.org/cautions-on-delayed-jobs/
来源:https://stackoverflow.com/questions/16033610/delayed-job-update-query-is-running-infinitely