active_job-performs
active_job-performs copied to clipboard
Access job instance from inside method
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?