Search code examples
ruby-on-railssidekiq

Sidekiq worker's responsibility


I would like to know if it is a best design practice to put many database interactions in Sidekiq.

I have a service object that delivers SMS messages to User. It just retrieves id of users and calls .perform_async on a Sidekiq worker class.

The SMS contains many information about other models. Therefore the worker has a long #perform method that just sets a number of interim variables.

For instance, it is querying tables such as jobs, bids, business, and calling associations on ActiveRecord objects.

Should it be the service's responsibility to retreive all the data, and simply pass those ids to the worker? Or can I just pass in ids of user and let the worker worry about database and associations?


Solution

  • Yes, putting a lot of database work in your worker is totally normal, just remember to stick to Sidekiq's Best Practices.

    1. Make your job parameters small and simple.
    2. Make your job idempotent and transactional.
    3. Embrace concurrency.

    https://github.com/mperham/sidekiq/wiki/Best-Practices