I am using ActiveJob with delayed_job (4.0.6) in the background and I want to find a scheduled task to delete it.
For example, if I have
class MyClass
def my_method
perform_stuff
MyJob.set(wait: 1.month.from_now).perform_later(current_user)
end
end
Then, if I edit an instance of MyClass and call my_method again, I want to cancel this task and schedule a new one.
As suggested in this post http://www.sitepoint.com/delayed-jobs-best-practices , I added two columns to the slowdown 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'
Thus, I can find the deferred Job and destroy it. But I wanted to do this inside the ActiveJob class to support the job template in my project.
I wanted to do something like:
class MyJob < ActiveJob::Base
after_enqueue do |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 that does not work.
Has anyone had such a problem?