Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run the job synchronously with sidekiq

Currently I am working with queue job on the ruby on rail with the Sidekiq. I have 2 jobs that are depend to each other and I want 1st job to finish first before starting the 2nd job, so is there any way to make it with Sidekiq.

like image 321
Thyda Eng Avatar asked Oct 28 '25 10:10

Thyda Eng


1 Answers

Yes, you can use the YourSidekiqJob.new.perform(parameters_to_the_job) pattern. This will run your jobs in order, synchronously.

However, there are 2 things to consider here:

  1. What happens if the first job fails?
  2. How long does the each job run?

For #2, the pattern blocks execution for the length of time each job takes to run. If the jobs are extremely short in runtime, why use the jobs in the first place? If they're long, are you expecting the user to wait until they're done?

Alternatively, you can schedule the running of the second job as the last line in the body of the first one. You still need to account for the failure mode of job #1 or #2. Also, you need to consider that the job won't necessarily run when it's scheduled to run, due to the state of the queue at schedule time. How does this affect your business logic?

Hope this helps

--edit according to last comment

class SecondJob < SidekiqJob
  def perform(params)
    data = SomeData.find
    return unless data.ready?
    # do whatever you need to do with the ready data
  end
end
like image 142
Srdjan Pejic Avatar answered Oct 30 '25 00:10

Srdjan Pejic



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!