Sidekiq in Rails


介紹

Sidekiq 是一個多線程的后台任務處理系統, 在其出來以前, 還存在一個名為 Resque 產品(Github 制造), 因為 Resque 是多進程模型, 所以當任務量變多的時候, Resque 所消耗的內存資源會非常的大, 所以就有了借助 Celluoid 來完成多線程形式的 Reqsue 也就是今天的 Sidekiq (2.15.xx)

基本架構

Job

在 Sidekiq 中的 Job 指的是某一個任務的一次執行, 注意與我們通常意義上說 “一個 Job” 指的是一類 Job.

Worker

因為 Sidekiq 是使用 Celluoid 來完成其多線程的控制的, 而 Celluoid 是 Ruby 中的多線程模式 Actor 模式的實現, 所以在 Sidekiq 中的 Worker 我們以擬人的方式去理解. 我擁有一個工人, 不過有一點要區分的是這些 Worker 都是按照”操作手冊”在執行任務, 所以他不會被限制在某一類任務上.

Queue

隊列的意義在於區分任務並且讓任務排隊, Sidekiq 中將每一類的任務使用一個 Queue 來區分開.

Redis Server

指存儲任務的 Redis 來源, 在 Sidekiq 2.x 時代其有一個瓶頸就是無論多少個 Sidekiq Instance 但只能擁有一個 Redis Server, 也就是任務處理的最大速度被限制在了單台 Redis 服務器每秒的處理速度, 大約在 5000 job/s, 但是在 Sidekiq 3.0 以后, 其擴展了 redis_pool 的參數, 每一個 Worker 可以選擇使用 Redis Server.

Redis Client

Redis 作為一個任務提交者, 透過 Worker 向指定的 Redis Client 中提交任務.

開發環境

ubuntu 12.4, ruby 2.12, rails 4.1.1 ,redis 3.0.7 ,sidekiq 3.1.3

安裝配置

  • Gemfile添加 :
gem "redis", "~> 3.0.7"
 
gem 'sidekiq'
 
gem 'sinatra' # 用於使用自帶的監控頁面

 

  • 在initializers下新建sidekiq.rb文件,用來初始化Redis和Sidekiq config。 initializers/sidekiq.rb :
redis_server = '127.0.0.1' # redis服務器
redis_port = 6379 # redis端口
redis_db_num = 0 # redis 數據庫序號
redis_namespace = 'highlander22_sidekiq' #命名空間,自定義的
 
Sidekiq.configure_server do |config|
  p redis_server  # 這個可以去掉
  config.redis = { url: "redis://#{redis_server}:#{redis_port}/#{redis_db_num}", namespace: redis_namespace }
end
 
Sidekiq.configure_client do |config|
  config.redis = { url: "redis://#{redis_server}:#{redis_port}/#{redis_db_num}", namespace: redis_namespace }
end
  • sidekiq 啟動配置文件 config/sidekiq.yml:
:concurrency: 5 # 並發數
:pidfile: tmp/pids/sidekiq.pid
:logfile: ./log/sidekiq.log # 輸出的日志地址
:queues:
- default # 寫在隊列參數中的, 表示讓 sidekiq 處理這個 queue
- [myqueue, 2] # 使用數組的形式寫, 第一個參數為打開的 queue 的名稱, 第二個為優先級
 
development:
:concurrency: 5
staging:
:concurrency: 10
production:
:concurrency: 20

 

  • 首先將 worker 類放到 app/workers文件夾,app/workers/hard_worker.rb:
class HardWorker
  include Sidekiq::Worker
 
  def perform(name, count)
     # do somethings
     puts 'Doing hard work'
  end
end

 

  • 在控制器 action 或者 model 中調用 HardWorker.perform_async:
HardWorker.perform_async('bob', 5)
  • sidekiq 配置參數 命令后加上 --help 可以看到其配置參數:
richard@richard:~/ipucc/blog$ bundle exec sidekiq --help
2014-06-12T10:16:35Z 22651 TID-6dezc INFO: sidekiq [options]
-c, --concurrency INT processor threads to use
-d, --daemon Daemonize process
-e, --environment ENV Application environment
-g, --tag TAG Process tag for procline
-i, --index INT unique process index on this machine
-q, --queue QUEUE[,WEIGHT] Queues to process with optional weights
-r, --require [PATH|DIR] Location of Rails application with workers or file to require
-t, --timeout NUM Shutdown timeout
-v, --verbose Print more verbose output
-C, --config PATH path to YAML config file
-L, --logfile PATH path to writable logfile
-P, --pidfile PATH path to pidfile
-V, --version Print version and exit
-h, --help Show help

 

配置好這些東西,就可以對剛才寫的HardWorker進行測試了。

使用

首先,要啟動Sidekiq

需要在rails項目目錄下啟動

可以通過linux cli的方式,使用添加參數來啟動sidekiq: bundle exec sidekiq -q queue_name_1,queue_name_2
也可以將這些參數放到yml中,通過 -C 參數來啟動: bundle exec sidekiq -C config/sidekiq.yml
也可以直接: sidekiq 或者 bundle exec sidekiq -e production
-r : 指定需要引入的那些自定義 worker 以及相關的 ruby 代碼
-C : 指定配置文件的路徑. 如果配置文件路徑為 config/sidekiq.yml 則可忽略這個參數
-e : 指定當前的 sidekiq 以什么環境進行運行. (控制了使用什么配置信息)

 

richard@richard:~/ipucc/blog$ sidekiq

         s
        ss
   sss  sss         ss
   s  sss s   ssss sss   ____  _     _      _    _
   s     sssss ssss     / ___|(_) __| | ___| | _(_) __ _
  s         sss         \___ \| |/ _` |/ _ \ |/ / |/ _` |
  s sssss  s             ___) | | (_| |  __/   <| | (_| |
  ss    s  s            |____/|_|\__,_|\___|_|\_\_|\__, |
  s     s s                                           |_|
        s s
       sss
       sss 

  

使用Rails Console進行測試

richard@richard:~/ipucc/blog$ rails c
Loading development environment (Rails 4.1.1)
2.1.2 :002 > HardWorker.perform_async 'Hello World', 1 # 調用perform
=> "2f68a6b62418f34670b6afdc"
2.1.2 :003 >

使用自帶的監控頁面

route.rb 添加 :

require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'

訪問 http://localhost:3000/sidekiq/retries 進入監控頁面

這樣,就可以在頁面上看到sidekiq的運行情況了。更多詳情見:sidekiq Monitoring


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM