😽

Sidekiq-schedulerを試す...!

2022/02/23に公開

これの続き

  • sidekiq でとりあえず単発実行はできたので、cron的に定期実行を確認したい。
  • なお、今記事は続き記事なので、前提条件含めて下記記事のとおりとなります。
    https://zenn.dev/articles/70a995d95a22c0/edit

利用gem

gem 'sidekiq-scheduler'

お試し

  • Gemfile に追加
gem 'sidekiq-scheduler'
  • install
bundle install
  • config ( config/sidekiq.yml )
require 'sidekiq'
require 'sidekiq-scheduler'

Sidekiq.configure_server do |config|
  config.redis = { url: 'redis://127.0.0.1:6379' }
  config.log_formatter = Sidekiq::Logger::Formatters::JSON.new
  config.on(:startup) do
    yaml = YAML.load_file(Rails.root.join('config/sidekiq_scheduler.yml'))
    raise "config/sidekiq-scheduler.yml no key [#{Rails.env}]" unless yaml.key? Rails.env

    Sidekiq.schedule = yaml[Rails.env]
    SidekiqScheduler::Scheduler.instance.reload_schedule!
  end
end
  • schedule
    • ある程度ベースとなるjob郡があるときに common なり base なり を定義しておいて、ENV側で読み込む、+ENV固有の定義をするとか便利そう
    • そもそも、基本一つにすべきというのはそうなんだが、対向先との関係で特定環境だけjobしたりしなかったりは往々にしてね...。
common: &COMMON
  OtameshiJob2:
    cron: '* * * * * Asia/Tokyo'
    queue: default
    description: お試しcronjob2

development:
  <<: *COMMON
  OtameshiJob3:
    cron: '5 * * * * Asia/Tokyo'
    queue: default
    description: お試しcronjob3
  • お試し動作ログ
  • ちなみに前回つくった moziretsu を受け取る perform はあかんかった...
    • どうにかすると引数を渡せるのだろうか...
    • そもそも job 登録(enqueue)なんだから、そりゃむりやろ・・・の範疇なのかは分かってない。
{"ts":"2022-02-22T14:07:00.101Z","pid":39237,"tid":"w2d","lvl":"INFO","msg":"queueing OtameshiJob2 (OtameshiJob2)"}
{"ts":"2022-02-22T14:07:00.107Z","pid":39237,"tid":"vlt","lvl":"INFO","msg":"start","ctx":{"class":"OtameshiJob2","jid":"1f975f30440519af24ffd9c6"}}
{"ts":"2022-02-22T14:07:00.108Z","pid":39237,"tid":"w2d","lvl":"INFO","msg":"Enqueued OtameshiJob2 (Job ID: 946dcb93-dfa2-47ef-a7c9-a4186e1e2f18) to Sidekiq(default)"}
{"ts":"2022-02-22T14:07:00.147Z","pid":39237,"tid":"vlt","lvl":"INFO","msg":"Performing OtameshiJob2 (Job ID: 946dcb93-dfa2-47ef-a7c9-a4186e1e2f18) from Sidekiq(default) enqueued at 2022-02-22T14:07:00Z","ctx":{"class":"OtameshiJob2","jid":"1f975f30440519af24ffd9c6"}}
おためし cron 実行 "cron: * * * * * Asia/Tokyo"
{"ts":"2022-02-22T14:07:00.147Z","pid":39237,"tid":"vlt","lvl":"INFO","msg":"Performed OtameshiJob2 (Job ID: 946dcb93-dfa2-47ef-a7c9-a4186e1e2f18) from Sidekiq(default) in 0.51ms","ctx":{"class":"OtameshiJob2","jid":"1f975f30440519af24ffd9c6"}}
{"ts":"2022-02-22T14:07:00.147Z","pid":39237,"tid":"vlt","lvl":"INFO","msg":"done","ctx":{"class":"OtameshiJob2","jid":"1f975f30440519af24ffd9c6","elapsed":0.04}}

{"ts":"2022-02-22T14:08:00.140Z","pid":39237,"tid":"w2d","lvl":"INFO","msg":"queueing OtameshiJob2 (OtameshiJob2)"}
{"ts":"2022-02-22T14:08:00.146Z","pid":39237,"tid":"w11","lvl":"INFO","msg":"start","ctx":{"class":"OtameshiJob2","jid":"5c36fbf3e4f0bdc7aafdaca7"}}
{"ts":"2022-02-22T14:08:00.147Z","pid":39237,"tid":"w2d","lvl":"INFO","msg":"Enqueued OtameshiJob2 (Job ID: 3258c939-0cc5-4f2f-b3d2-1b24c60775bf) to Sidekiq(default)"}
{"ts":"2022-02-22T14:08:00.188Z","pid":39237,"tid":"w11","lvl":"INFO","msg":"Performing OtameshiJob2 (Job ID: 3258c939-0cc5-4f2f-b3d2-1b24c60775bf) from Sidekiq(default) enqueued at 2022-02-22T14:08:00Z","ctx":{"class":"OtameshiJob2","jid":"5c36fbf3e4f0bdc7aafdaca7"}}
おためし cron 実行 "cron: * * * * * Asia/Tokyo"
{"ts":"2022-02-22T14:08:00.188Z","pid":39237,"tid":"w11","lvl":"INFO","msg":"Performed OtameshiJob2 (Job ID: 3258c939-0cc5-4f2f-b3d2-1b24c60775bf) from Sidekiq(default) in 0.45ms","ctx":{"class":"OtameshiJob2","jid":"5c36fbf3e4f0bdc7aafdaca7"}}
{"ts":"2022-02-22T14:08:00.189Z","pid":39237,"tid":"w11","lvl":"INFO","msg":"done","ctx":{"class":"OtameshiJob2","jid":"5c36fbf3e4f0bdc7aafdaca7","elapsed":0.043}}

備考

gem 'fugit'

  • 月末処理とかもできる...?(試し辛い.... 月末に覚えてたら試してみたいところ)
      OtameshiJob2:
          cron: '*/5 * last * * Asia/Tokyo'
          queue: default
          description: お試しジョブcron
    

https://github.com/floraison/fugit/blob/master/spec/cron_spec.rb

これをやりたい

  • WebUIからのcronjob登録。
    • ざっくり調べてみると、requestを受けたときの処理の中でcron jobを登録するというのがあったけども、それだと再起動したら消えてしまうのでは...?と。いうところがわからなかった。
    • db にjob入れておいて、書き出してから sidekiq.schedule を呼び出すとかするのだろうか?
  • 定期実行 job も管理画面で見えてるのを見かけるんだけども...?うーん。

Discussion