Skip to content

Instantly share code, notes, and snippets.

@bernstein7
Last active June 7, 2016 10:11
Show Gist options
  • Save bernstein7/e202e984993add636111e6a26379a5d4 to your computer and use it in GitHub Desktop.
Save bernstein7/e202e984993add636111e6a26379a5d4 to your computer and use it in GitHub Desktop.
Sidekiq::JidStorage
require 'active_support/concern'
module Sidekiq::Cancelable
extend ActiveSupport::Concern
included do
include Redis::Objects
attr_accessor :id
value :stored_jid
def self.perform_at(timestamp, *args)
stored_jid = super(timestamp, *args)
worker = self.new
worker.id = args.first.id
worker.stored_jid = stored_jid
end
def self.cancel!(schedulable)
worker = self.new
worker.id = schedulable.id
cancel_by_jid(worker.stored_jid)
end
def self.cancel_by_jid(jid)
Sidekiq.redis { |c| c.setex("cancelled-#{jid}", 86400, 1) }
end
def cancelled?
Sidekiq.redis { |c| c.exists("cancelled-#{jid}") }
end
end
end
Notifications::Scheduler.cancel!(job)
module Notifications
class Scheduler
include Sidekiq::Worker
include Sidekiq::Cancelable
sidekiq_options retry: false, queue: 'session_notifications'
def perform(job_id)
job = Job.find(job_id)
Notification::SessionNotifier.send_reminder!(job)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment