【发布时间】:2013-04-29 02:13:27
【问题描述】:
我有一个 Rails 应用程序,它可以从多个 IMAP 帐户中获取大量电子邮件。
- 我使用 sidekiq 来处理这些工作。
- 我使用 sidetiq 来安排作业。
- 我使用 redis-semaphore 来确保同一用户的重复性工作不会相互干扰。
虽然有 2 个问题:
- 1:当作业点击“if s.lock”时,redis-semaphore 将其暂停,直到所有之前的作业完成。我需要取消作业而不是排队。
- 2:如果在作业期间引发异常,导致崩溃,sidekiq 会将作业放回队列中重试。我需要取消作业而不是排队。将“sidekiq_options :retry => false”放入代码中似乎没有什么不同。
我的代码:
class FetchMailsJobs
include Sidekiq::Worker
include Sidetiq::Schedulable
tiq { hourly.minute_of_hour(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55) }
def perform(last_occurrence, current_occurrence)
users = User.all
users.each do |user|
if user.imap_accounts.exists?
ImapJob.perform_async(user._id.to_s)
end
end
end
end
class ImapJob
include Sidekiq::Worker
def perform(user_id)
s = Redis::Semaphore.new("fetch_imap_mails_for_#{user_id}".to_sym, connection: "localhost")
if s.lock
user = User.where(_id: user_id).first
emails = ImapMails.receive_mails(user)
s.unlock
end
end
end
【问题讨论】:
标签: ruby redis mutex semaphore sidekiq