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

Closed
brendon opened this issue Feb 11, 2025 · 7 comments · Fixed by #529
Closed

Accessing the job from within the perform method #514

brendon opened this issue Feb 11, 2025 · 7 comments · Fixed by #529

Comments

@brendon
Copy link
Contributor

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 see 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.
@rosa
Copy link
Member

rosa commented Mar 5, 2025

Hey @brendon, you're right, this is currently not accessible to the job from within the perform method. It's only set in the job returned by #enqueue (or by perform_later).

It should be easy to add, though. If you or anyone else wants to open a PR with that, I'd be happy to review it.

@brendon
Copy link
Contributor Author

brendon commented Mar 5, 2025

Thanks @rosa, I'll put together a PR :)

@brendon
Copy link
Contributor Author

brendon commented Mar 6, 2025

Hi @rosa, just wondering if there are any instructions around developing on this project? I'm having a bit of trouble getting a test suite up and running. I see a docker setup there but it only creates the database machines.

@rosa
Copy link
Member

rosa commented Mar 6, 2025

Oh, @brendon, you're right, I should put together some instructions for contributing. In theory, you should simply need to run bin/setup, and then you should be able to run the tests right away with bin/rails test. Do you get any errors there?

@brendon
Copy link
Contributor Author

brendon commented Mar 6, 2025

Thanks for the heads up :D Just the error about the port conflict that I've raised an issue on: #528.

I have the test suite running so will try and work on this PR now :D

@brendon
Copy link
Contributor Author

brendon commented Mar 6, 2025

Hi @rosa, just looking at your commit, is this feature perhaps already a thing?

03eb148

Maybe I'm not reading it right though.

@brendon
Copy link
Contributor Author

brendon commented Mar 7, 2025

Hi @rosa, just looking at your commit, is this feature perhaps already a thing?

03eb148

Maybe I'm not reading it right though.

Nevermind, I see what you said in your original reply. Just bending my mind to figure out a nice way to get at this object at execute time.

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

Successfully merging a pull request may close this issue.

2 participants