简体   繁体   中英

How to process queue sequentially in Redis and SideKiq in Rails?

I am using Rails 4 in my app and this is how i create a new job:

class FetchJob < ActiveJob::Base
  queue_as :fetchjob
  def perform num
    puts num
    sleep 30
  end
end

This code prints out the value of num and then sleeps for 30 seconds. I am using sidekiq gem and redis to process this queue. I am queuing the jobs after a user is created.

class FecthJobController < ApplicationController
  def create
    @user = User.create(user_params)
    FetchJob.perform_later(@user.id)
  end

  private
  def user_params
    ....
  end
end

Jobs in the queue are getting executed parallely. By jobs in the queue i mean like job1, job2, job3, job4 and so on. So how could i possibly execute these jobs sequentially. ie only if job1 is completed, it'll process the job2 and then job3 and so on. How to lock it until the job is completed in sidekiq?

You might find a hack that allows you to do what you want but let me suggest something else: you are using the wrong tool. Sidekiq is not designed to operate that way.

https://github.com/mperham/sidekiq/wiki/Best-Practices#3-embrace-concurrency

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM