20
15

More than 5 years have passed since last update.

未来に登録したActiveJob実行をCancelする方法

Last updated at Posted at 2015-10-30

RailsのActiveJobで未来に登録したJob実行をキャンセルしたい。
ActiveJobの標準的な機能としてサポートされているかと思ったのですが、そんなことなかった。

今回はジョブ実行にSidekiqを使っているとし、キャンセルにはSidekiqのAPIを使います。

job = MyJob.set(wait_until: Date.tomorrow.noon).perform_later(record)
job.job_id # これがjobを特定するid

require 'sidekiq/api' # 実際はinitializers/sidekiq.rbに入れるかも

ss = Sidekiq::ScheduledSet.new # スケジュールされたジョブの一覧を取得
ss.find { |job| job.args[0]['job_id'] == job.job_id}.delete

参考

Sidekiq Wiki API https://github.com/mperham/sidekiq/wiki/API

20
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
15