Saturday, 17 August 2013

How do I kill and unlock a Redis::Semaphore (NonBlockingRedis) in Rails?

How do I kill and unlock a Redis::Semaphore (NonBlockingRedis) in Rails?

I have this job that I run every hour.
class ImapJob
include Sidekiq::Worker
sidekiq_options :retry => false
def perform(user_id)
s = Redis::Semaphore.new("fetch_imap_mails_for_#{user_id}".to_sym,
redis: NonBlockingRedis.new(connection: "localhost"))
if s.lock(-1)
begin
emails = ImapMails.receive_mails(user_id)
rescue => e
puts "Error while fetching email"
ensure
s.unlock
end
end
end
end
In some a controller I have an action that is supposed to kill this job. I
need not only to kill the job, but also to unlock, so the job can be run
next hour like it always does.
How do I find it and kill it?
Example code:
c = Redis::Semaphore.find("fetch_imap_mails_for_#{user_id}".to_sym
c.kill_and_unlock

No comments:

Post a Comment