Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing the job from within the perform method #514

Open
brendon opened this issue Feb 11, 2025 · 0 comments
Open

Accessing the job from within the perform method #514

brendon opened this issue Feb 11, 2025 · 0 comments

Comments

@brendon
Copy link

brendon commented Feb 11, 2025

I'm currently using DelayedJob with this custom plugin that allows me to access the provider_job_id from within the perform method of a job:

class Delayed::ActiveJobPlugin < Delayed::Plugin
  callbacks do |lifecycle|
    lifecycle.before(:invoke_job) do |job|
      job.payload_object.job_data['provider_job_id'] = job.id if job.payload_object.respond_to?(:job_data)
    end
  end
end

Delayed::Worker.plugins << Delayed::ActiveJobPlugin

The Sidekiq adapter in Rails also does something similar:

https://github.com/rails/rails/pull/25961/files#diff-e1ee600ca5fd100da20810ef5acbab89546064fc323c7b1e6bdb6ed5e681a9d3L40

I'm intending to migrate to Solid Queue but just wondered if this is an option currently? From looking at the source I don't seen the job id being passed in. I have two use cases:

  • Keeping track of a job that generates another job at the end of its run (so a kind of recurring job configurable by the user). I store the job_id in the database so that I can destroy that job and schedule a new job if the user changes the time it should run.
  • Skipping the execution of a job if the object passed into the job has an updated_at that is greater than the job's created_at. This signals that there's now a newer job scheduled that should take precedence. I use this for WebPush notifications.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant