fix: expiry delay

This commit is contained in:
Shivam Mishra
2025-10-15 15:15:19 +05:30
parent 0c17d7fce1
commit 4ea8218e30
2 changed files with 5 additions and 5 deletions

View File

@@ -7,10 +7,10 @@ class Messages::SendEmailNotificationService
conversation = message.conversation
conversation_mail_key = format(::Redis::Alfred::CONVERSATION_MAILER_KEY, conversation_id: conversation.id)
# Atomically set redis key to prevent duplicate email workers
# Only the first message in a 2-minute window will successfully set the key and enqueue the worker
# Subsequent messages will fail to set the key (returns false) and skip enqueueing
return unless Redis::Alfred.set(conversation_mail_key, message.id, nx: true, ex: 2.minutes.to_i)
# Atomically set redis key to prevent duplicate email workers. Keep the key alive longer than
# the worker delay (1 hour) so slow queues don't enqueue duplicate jobs, but let it expire if
# the worker never manages to clean up.
return unless Redis::Alfred.set(conversation_mail_key, message.id, nx: true, ex: 1.hour.to_i)
ConversationReplyEmailWorker.perform_in(2.minutes, conversation.id, message.id)
end

View File

@@ -32,7 +32,7 @@ describe Messages::SendEmailNotificationService do
service.perform
expect(Redis::Alfred).to have_received(:set).with(expected_key, message.id, nx: true, ex: 2.minutes.to_i)
expect(Redis::Alfred).to have_received(:set).with(expected_key, message.id, nx: true, ex: 1.hour.to_i)
end
context 'when redis key already exists' do