feat: implement mutex for SlackSendJob (#7783)
This commit is contained in:
35
app/jobs/mutex_application_job.rb
Normal file
35
app/jobs/mutex_application_job.rb
Normal file
@@ -0,0 +1,35 @@
|
||||
# MutexApplicationJob serves as a base class for jobs that require distributed locking mechanisms.
|
||||
# It abstracts the locking logic using Redis and ensures that a block of code can be executed with
|
||||
# mutual exclusion.
|
||||
#
|
||||
# The primary mechanism provided is the `with_lock` method, which accepts a key format and associated
|
||||
# arguments. This method attempts to acquire a lock using the generated key, and if successful, it
|
||||
# executes the provided block of code. If the lock cannot be acquired, it raises a LockAcquisitionError.
|
||||
#
|
||||
# To use this class, inherit from MutexApplicationJob and make use of the `with_lock` method in the
|
||||
# `perform` method of the derived job class.
|
||||
#
|
||||
# Also see, retry mechanism here: https://edgeapi.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html#method-i-retry_on
|
||||
#
|
||||
class MutexApplicationJob < ApplicationJob
|
||||
class LockAcquisitionError < StandardError; end
|
||||
|
||||
def with_lock(key_format, *args)
|
||||
lock_key = format(key_format, *args)
|
||||
lock_manager = Redis::LockManager.new
|
||||
|
||||
if lock_manager.locked?(lock_key)
|
||||
Rails.logger.warn "[#{self.class.name}] Failed to acquire lock on attempt #{executions}: #{lock_key}"
|
||||
raise LockAcquisitionError, "Failed to acquire lock for key: #{lock_key}"
|
||||
end
|
||||
|
||||
begin
|
||||
lock_manager.lock(lock_key)
|
||||
Rails.logger.info "[#{self.class.name}] Acquired lock for: #{lock_key} on attempt #{executions}"
|
||||
yield
|
||||
ensure
|
||||
# Ensure that the lock is released even if there's an error in processing
|
||||
lock_manager.unlock(lock_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,7 +1,10 @@
|
||||
class SendOnSlackJob < ApplicationJob
|
||||
class SendOnSlackJob < MutexApplicationJob
|
||||
queue_as :medium
|
||||
retry_on LockAcquisitionError, wait: 1.second, attempts: 6
|
||||
|
||||
def perform(message, hook)
|
||||
Integrations::Slack::SendOnSlackService.new(message: message, hook: hook).perform
|
||||
with_lock(::Redis::Alfred::SLACK_MESSAGE_MUTEX, sender_id: message.sender_id, reference_id: hook.reference_id) do
|
||||
Integrations::Slack::SendOnSlackService.new(message: message, hook: hook).perform
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,28 +1,16 @@
|
||||
class Webhooks::FacebookEventsJob < ApplicationJob
|
||||
class LockAcquisitionError < StandardError; end
|
||||
|
||||
class Webhooks::FacebookEventsJob < MutexApplicationJob
|
||||
queue_as :default
|
||||
# https://edgeapi.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html#method-i-retry_on
|
||||
retry_on LockAcquisitionError, wait: 1.second, attempts: 6
|
||||
|
||||
def perform(message)
|
||||
response = ::Integrations::Facebook::MessageParser.new(message)
|
||||
|
||||
lock_key = format(::Redis::Alfred::FACEBOOK_MESSAGE_MUTEX, sender_id: response.sender_id, recipient_id: response.recipient_id)
|
||||
lock_manager = Redis::LockManager.new
|
||||
|
||||
if lock_manager.locked?(lock_key)
|
||||
Rails.logger.error "[Facebook::MessageCreator] Failed to acquire lock on attempt #{executions}: #{lock_key}"
|
||||
raise LockAcquisitionError, "Failed to acquire lock for key: #{lock_key}"
|
||||
end
|
||||
|
||||
begin
|
||||
lock_manager.lock(lock_key)
|
||||
Rails.logger.info "[Facebook::MessageCreator] Acquired lock for: #{lock_key}"
|
||||
::Integrations::Facebook::MessageCreator.new(response).perform
|
||||
ensure
|
||||
# Ensure that the lock is released even if there's an error in processing
|
||||
lock_manager.unlock(lock_key)
|
||||
with_lock(::Redis::Alfred::FACEBOOK_MESSAGE_MUTEX, sender_id: response.sender_id, recipient_id: response.recipient_id) do
|
||||
process_message(response)
|
||||
end
|
||||
end
|
||||
|
||||
def process_message(response)
|
||||
::Integrations::Facebook::MessageCreator.new(response).perform
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user