Created
January 28, 2016 17:39
-
-
Save namxam/50e3d8941e574a4c74d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'torquebox-messaging' | |
require_relative '../models/subscriber' | |
module Box | |
module Services | |
class SubscriberActivation | |
DELAY = 60 * 60 * 24 * 1000 | |
def self.logger | |
@logger ||= TorqueBox::Logger.new(self.class) | |
end | |
def self.logger=(logger) | |
@logger ||= logger | |
end | |
def self.queue | |
@queue ||= TorqueBox::Messaging.queue('subscriber_activations') | |
end | |
def self.check_delayed(id, delay = DELAY) | |
# We need to assign a unique name, otherwise we would always replace the schedule | |
# The benefit is that if two jobs are triggered for the same subscriber, only the latter one | |
# will be executed. | |
TorqueBox::Scheduling::Scheduler.schedule("service_subscriber_activation_#{id}", in: delay) do | |
queue.publish(subscriber_id: id) | |
end | |
end | |
# Use it to register the queue listener, so a process works the queue | |
def self.register | |
queue.listen(&method(:perform_check)) | |
end | |
def self.perform_check(message) | |
subscriber = Subscriber.find(id: message[:subscriber_id]) | |
if subscriber.activate! | |
TorqueBox::Logger.info("Activated subscriber! subscriber_id=#{subscriber.id}") | |
else | |
check_delayed(subscriber.id) | |
TorqueBox::Logger.info("Failed to activate subscriber! subscriber_id=#{subscriber.id}") | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment