Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
14
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

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

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
14
Help us understand the problem. What are the problem?