Skip to content

Accessing the job from within the perform method #514

@brendon

Description

@brendon

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions