Sidekiq

Sidekiq is a background job processing system used for tasks like email sending, CI/CD, and background updates.

Configuration

Sidekiq can be configured with various options, which are defined in the config/initializers/sidekiq.rb file.

Redis Configuration:

Concurrency:

  • concurrency: Number of threads used by Sidekiq. Example: 10.

Other options:

  • queue: The default queue for jobs. Example: default.
  • timeout: Maximum time a job can run before being considered a timeout. Example: 600 (seconds).
  • retry_attempts: Number of times to retry a failed job. Example: 3.
  • log_level: Logging level for Sidekiq. Example: :info.

Example configuration:

# config/initializers/sidekiq.rb
          
          Sidekiq.configure_server do |config|
            config.redis = {
              url: ENV['REDIS_URL'],
              namespace: 'gitlab'
            }
            config.concurrency = 10
            config.queue = 'default'
            config.timeout = 600
            config.retry_attempts = 3
            config.log_level = :info
          end
          

Jobs

Sidekiq jobs are defined as classes that inherit from Sidekiq::Worker. Each job must define a perform method which performs the actual task.

Example job:

# app/workers/send_email_worker.rb
          
          class SendEmailWorker
            include Sidekiq::Worker
          
            def perform(user_id, message)
              user = User.find(user_id)
              user.send_email(message)
            end
          end
          

Enqueuing Jobs

To enqueue a job, use the perform_async method.

Example:

# app/controllers/users_controller.rb
          
          class UsersController < ApplicationController
            def create
              user = User.create(user_params)
              SendEmailWorker.perform_async(user.id, "Welcome to GitLab!")
            end
          end
          

Monitoring

Sidekiq provides a web UI for monitoring jobs. The UI can be accessed at /sidekiq (assuming Sidekiq is running on the same server as the Rails application).

Additional Resources