Sidekiq is a background task-processing system that provides the ability to perform timed/asynchronous tasks. It can be integrated into Rails projects or used on its own. In this article, we’ll introduce and analyze how to use Sidekiq in Rails.

Add the dependent

Sidekiq has been updated to version 6.0, and we are using the latest version here

$ gem install sidekiq
Copy the code
gem 'sidekiq'.'~ > 6.0'.'> = 6.0.3'
Copy the code

Modify the configuration

Add the config/sidekiq.yml file

# Maximum number of concurrent requests
concurrency: 5

# Process and log output location
pidfile: tmp/pids/sidekiq.pid
logfile: log/sidekiq.log

# task queue
queues:
  - default
  - schedule

development:
  concurrency: 5

staging:
  concurrency: 10

production:
  concurrency: 20
Copy the code

Add the config/initializers/sidekiq. Rb file

Sidekiq Cron checks tasks every second. Default is 15 seconds
Sidekiq.options[:poll_interval]                   = 1
Sidekiq.options[:poll_interval_average]           = 1

Set the sidekiq server
Sidekiq.configure_server do |config|

  # Configure redis connection
  config.redis = { url: "redis://localhost:6379/0" }

  Sidekiq checks jobs every second (default: every 5 seconds)
  config.average_scheduled_poll_interval = 1

  Read the scheduled task from the configuration file
  schedule_file = 'config/sidekiq_schedule.yml'
  ifFile.exists? (schedule_file) Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)end
end

Sidekiq.configure_client do |config|
  config.redis = { url: "redis://localhost:6379/0" }
end
Copy the code

Set the configuration items in config/application.rb:

config.active_job.queue_adapter = :sidekiq Use sidekiq as an adapter for asynchronous tasks
Copy the code

Asynchronous tasks

Use the generator to create a task that sends a message:

$ rails g job SendMessage
Copy the code

The command above will create app/jobs/send_message_job.rb

class SendMessageJob < ActiveJob::Base
  queue_as :default
  def perform(*args)
    Rails.logger.info "send message..."
  end
end
Copy the code

How to use:

SendMessageJob.perform_async(xxx, xxx, xxx) Create an asynchronous job
SendMessageJob.perform_in(1.minutes, xxx, xxx) Create an asynchronous job
SendMessageJob.perform_at(1.minutes.from_now, xxx, xxx) Create an asynchronous job at the specified time
Copy the code

Timing task

Add the app/worker/send_message_worker.rb file

class SendMessageWorker
  include Sidekiq::Worker
  sidekiq_options queue: :schedule.backtrace: true.retry: false
  No need to display the call, sidekiq runs automatically
  # Incoming parameters and execution cycles are configured in config/sidekiq_schedule.yml
  def perform(*args)
      Rails.logger.info 'every second execution... '
  end
end
Copy the code

Add the config/sidekiq_schedule.yml configuration file

SendMessageWorker:
  cron: '* * * * * *'
  class: SendMessageWorker
Copy the code

Run the sidekiq -c config/sidekiq command to start the scheduled task periodically

Configuration monitoring

Sidekiq already has a set of monitoring pages built in from 4.2 onwards, additional dependencies need to be added prior to 4.2:

gem 'sinatra'.'~ > 2.0'.'> = 2.0.7'
Copy the code

Mount the route on the WebUI to the config/router.rb file.

require 'sidekiq/web'
Rails.application.routes.draw do
  mount Sidekiq::Web => '/sidekiq'
end
Copy the code

Rails s start-up project sidekiq – C config/sidekiq. Yml start sidekiq, visit http://localhost:3000/sidekiq

reference

  • Github.com/mperham/sid…
  • draveness.me/sidekiq
  • Ruby-china.org/topics/3677…