Inspired by Beguen's answer and some reverse engineering of the Rails 5 ActiveJob code, I made it work with Rails 4.2 on
1) adding the following code to lib/active_job/queue_adapters/delayed_job_adapter.rb or config/initializers/delayed_job.rb (both sites worked):
The attr_accessor :provider_job_id statement is required in Rails 4.2 because it is used in the enqueue method and is not yet defined in 4.2.
Then we can use it as follows:
2) define our own ActiveJob class:
# file: app/jobs/my_job.rb class MyJob < ActiveJob::Base queue_as :default def perform(object, performmethod = method(:method))
3) Now we can create a new task anywhere in the code:
job = MyJob.perform_later(Myobject, "mymethod")
This will Myobject.mymethod method.
4) The code in 1) helps to find the Delayed task related to our work:
delayed_job = Delayed::Job.find(job.provider_job_id)
5) finally, we can do whatever we do with the delay_job, for example. delete it:
delayed_job.delete
Note: in Rails 5, step 1) is no longer needed, since the same code is an integral part of Rails 5.
source share