Objective:
To save successfully completed jobs
Methods used
Have looked at this answer which tells how to save completed jobs and this answer which suggests to create a plugin which will be executed after one of lifecycle events.
Problem
There are following lifecycle events, and applicable arguments as present in code:
:enqueue => [:job],
:execute => [:worker],
:loop => [:worker],
:perform => [:worker, :job],
:error => [:worker, :job],
:failure => [:worker, :job],
:invoke_job => [:job]
I had expected there will be some event corresponding to success, I am not getting which one will be executed after job is successful.
Some Code
With the help of above tow answers, I have created this plugin to save completed job and put in config/initializers/save_completed_delayed_job.rb
module Delayed
module Plugins
class SaveCompletedDelayedJobPlugin < Plugin
callbacks do |lifecycle|
# see below for list of lifecycle events
lifecycle.after(:perform) do |job|
p "This should be ran after success so that I can save the job in different table"
CompletedDelayedJob.create({
priority: job.priority,
attempts: job.attempts,
handler: job.handler,
last_error: job.last_error,
run_at: job.run_at,
failed_at: job.failed_at,
completed_at: DateTime.now,
queue: job.queue
})
end
end
end
end
end
Delayed::Worker.plugins << Delayed::Plugins::SaveCompletedDelayedJobPlugin
Why I need plugin
I can not use callback methods as I don't have dedicated job classed, I am using following code to queue job.
handle_asynchronously :deliver_delayed, :queue => 'my_q'
来源:https://stackoverflow.com/questions/41742169/hook-before-all-delyed-job-success-callback-to-save-successfully-completed-jobs