Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save metadata to jobs on Sidekiq

Is it possible to save metadata to jobs when using Sidekiq?

For example, I want to execute a validation as a background job so that when it finishes, any errors encountered would be saved as metadata inside the job.

If this is possible, will I still be able to recover this metadata after the job is finished or dead?

Thanks in advance.

like image 719
felipeecst Avatar asked Oct 26 '25 10:10

felipeecst


2 Answers

Not straight out of the box with Sidekiq, but I have accomplished this with sidekiq-status

For example, in your scenario, it would look something like this:

class ValidatorJob
  include Sidekiq::Worker
  include Sidekiq::Status::Worker 

  def perform(*args)
    # Run validations

    # after they are done, you can store any data with the store method
    store attr1: 'failed'
  end
end
like image 125
Nobita Avatar answered Oct 28 '25 22:10

Nobita


Yes, Sidekiq provides middleware (client & server) and the possibility to add metadata to the job.

  def call(worker_class, job, queue, redis_pool)
    # return false/nil to stop the job from going to redis
    return false if queue != 'default'
    job['customer'] = Customer.current_id
    yield
  end

Check this link for the docs.

like image 23
MegaTux Avatar answered Oct 29 '25 00:10

MegaTux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!