active_job-performs icon indicating copy to clipboard operation
active_job-performs copied to clipboard

Access job instance from inside method

Open doits opened this issue 1 year ago • 0 comments

I'm just wondering if it can be made possible to access the job from inside the method, e.g. like this:

class Post < ActiveRecord::Base
  extend ActiveJob::Performs

  performs :publish

  def publish
     job # <-- The job currently running this method
  end
end

I've some methods for reporting the execution state for this job (e.g. job.percent_done(21)), but for this I need the job instance.

Maybe it could be passed optionally as a kword argument if defined?

class Post < ActiveRecord::Base
  extend ActiveJob::Performs

  performs :publish, pass_job: true

  def publish(job: nil)
     job
  end
end

... which would generate something like ...

  class PublishJob < Job
    def perform(post, *arguments, **options)
      options[:job] = self if pass_job?
      post.publish(*arguments, **options)
    end
  end

Or do you think this is out of scope for this gem?

doits avatar Sep 04 '24 10:09 doits