How to access Delayed Job instance inside Active Job - Rails 4.2 -
i'm using activejob delayed_job (4.0.6) in background , want find scheduled job deleted it.
for instance, if have
class myclass def my_method perform_stuff myjob.set(wait: 1.month.from_now).perform_later(current_user) end end
then, if edit myclass instance , call my_method again, want cancel job , schedule new one.
as suggested in post http://www.sitepoint.com/delayed-jobs-best-practices, added 2 columns delayed job table:
table.integer :delayed_reference_id table.string :delayed_reference_type add_index :delayed_jobs, [:delayed_reference_id], :name => 'delayed_jobs_delayed_reference_id' add_index :delayed_jobs, [:delayed_reference_type], :name => 'delayed_jobs_delayed_reference_type'
so way may find delayed job , destroy it. wanted inside activejob class, maintain pattern of jobs in project.
i wanted like:
class myjob < activejob::base after_enqueue |job| user = self.arguments.first job.delayed_reference_id = user.id, job.delayed_reference_type = "user" end def perform(user) delete_previous_job_if_exists(user_id) end def delete_previous_job_if_exists(user_id) delayed::job.find_by(delayed_reference_id: 1, delayed_reference_type: 'user').delete end end
but doesn't work.
anyone had kind of issue?
two changes: 1. updated after_enqueue callback can update delayed_jobs table directly 2. fixed typo delayed_reference_id
hard coded 1
this should work:
class myjob < activejob::base after_enqueue |job| user = self.arguments.first delayed_job = delayed::job.find(job.provider_job_id) delayed_job.update(delayed_reference_id:user.id,delayed_reference_type:'user') end def perform(user) delete_previous_job_if_exists(user.id) end def delete_previous_job_if_exists(user_id) delayed::job.find_by(delayed_reference_id: user_id, delayed_reference_type: 'user').delete end end
Comments
Post a Comment