Andy
Andy

Reputation: 541

Understanding Background Workers with Redis, Sidekiq, Heroku and Rails 5

I have a Rails 5 app where I'm starting to move various jobs to the background using Sidekiq, Heroku and Redis.

So far I have ResetFinanceDataWorker which has a Class of the same name.

//reset_finance_data_worker.rb

class ResetFinanceDataWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform()
  end
end

I can call this by calling ResetFinanceDataWorker.perform_in(10.seconds). Ideally, I would like to have several workers in one file, like this:

//finance_worker.rb

class AnotherWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform()
  end
end

class ResetFinanceDataWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform()
  end
end

And being able to call FinanceWorker.AnotherWorker.perform_in(10.seconds). However, this is not possible.

Can someone explain me 1) How would I make that work? and 2) Best practices around organising worker files.

Thanks.

Upvotes: 0

Views: 549

Answers (1)

Vasilisa
Vasilisa

Reputation: 4640

Best practices and convention is to place only one class inside one file with corresponding filename. No matters is it worker or any other class. If you want to organise them, you can wrap similar workers in a module and move them to a separate folder finance_worker:

 module FinanceWorker  
   class AnotherWorker
   end
 end

In this case you can call FinanceWorker::AnotherWorker.perform_async

UPDATE:

Create a separate folder finance_worker inside app/jobs and move there all needed worker files. Wrap all these worker classes in module FinanceWorker. I mean, wrap separately, still in different files, still only one class, wrapped in module, inside the file.

If you have 10 workers you have 10 files for them. But it is Sidekiq workers, not heroku workers, don't mix them up. To start Sidekiq you need only one line in Procfile: worker: bundle exec sidekiq. This line will start 1 heroku worker dyno for Sidekiq, which will start all his 10 workers. And it is OK, it is common practice. If you notice, that 1 dyno for Sidekiq is not enough, you always can add more, they will divide queues between each other.

And don't forget that you can specify queue name for worker. By default all jobs goes to general default queue. If you want that a worker has his own queue just add to sidekiq_options:

sidekiq_options retry: false, queue: 'finance'

Upvotes: 1

Related Questions