Class: HerdstWorker::Queue::Facade
- Inherits:
-
Object
- Object
- HerdstWorker::Queue::Facade
- Defined in:
- lib/herdst_worker/queue/facade.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#processor ⇒ Object
Returns the value of attribute processor.
Class Method Summary collapse
Instance Method Summary collapse
- #create_email_queue_message(template, company, message_data, attributes = nil, delay = 2) ⇒ Object
- #create_notification_message(message_data, attributes = nil, expiry = nil, delay = nil) ⇒ Object
- #create_queue_message(type, company_id, user_id, data, attributes = nil, delay = nil) ⇒ Object
- #get_processor ⇒ Object
- #get_queue_client(queue_name) ⇒ Object
- #get_status ⇒ Object
- #halt ⇒ Object
-
#initialize(app, enabled, url, queue_wait_time) ⇒ Facade
constructor
A new instance of Facade.
- #send_email_message(template, company, data = {}, attributes = nil, delay = nil) ⇒ Object
- #send_message(type, company_id, user_id, data = {}, queue = :primary, attributes = nil, delay = nil) ⇒ Object
- #send_messages(messages = [], queue = :primary) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(app, enabled, url, queue_wait_time) ⇒ Facade
Returns a new instance of Facade.
13 14 15 16 |
# File 'lib/herdst_worker/queue/facade.rb', line 13 def initialize(app, enabled, url, queue_wait_time) self.app = app self.processor = HerdstWorker::Queue::Processor.new(app, enabled, url, queue_wait_time) end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
10 11 12 |
# File 'lib/herdst_worker/queue/facade.rb', line 10 def app @app end |
#processor ⇒ Object
Returns the value of attribute processor.
10 11 12 |
# File 'lib/herdst_worker/queue/facade.rb', line 10 def processor @processor end |
Class Method Details
.get_delay(index) ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/herdst_worker/queue/facade.rb', line 149 def self.get_delay(index) = 10 = 0.4 offset = (index / ) delay = (offset * ( * )).to_i delay > 900 ? 900 : delay end |
Instance Method Details
#create_email_queue_message(template, company, message_data, attributes = nil, delay = 2) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/herdst_worker/queue/facade.rb', line 91 def (template, company, , attributes = nil, delay = 2) # Convert instances of ApplicationRecord to attributes instead of json representation .each do |key, value| [key] = value.attributes if value.is_a?(ActiveRecord::Base) end # Build data data = Hash.new data[:app] = self.app.name data[:app_id] = company.id data[:app_name] = company.name data[:app_slug] = company.get_host_name data[:app_brand_id] = company.brand_id data[:app_host] = company.get_url data[:type] = "email" data[:template] = template data[:data] = # Add message structure = Hash.new [:id] = SecureRandom.hex(32) [:message_body] = data.to_json [:message_attributes] = attributes if attributes [:delay_seconds] = delay end |
#create_notification_message(message_data, attributes = nil, expiry = nil, delay = nil) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/herdst_worker/queue/facade.rb', line 119 def (, attributes = nil, expiry = nil, delay = nil) = Hash.new [:id] = SecureRandom.hex(32) [:message_body] = Hash.new [:message_body][:Type] = "Notification" [:message_body][:Message] = .to_json [:message_body] = [:message_body].to_json [:message_attributes] = attributes ? attributes : Hash.new [:message_attributes]["expiry"] = { :string_value => expiry.to_s, :data_type => "Number" } if expiry [:delay_seconds] = delay if delay .delete(:message_attributes) if [:message_attributes].size == 0 end |
#create_queue_message(type, company_id, user_id, data, attributes = nil, delay = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/herdst_worker/queue/facade.rb', line 72 def (type, company_id, user_id, data, attributes = nil, delay = nil) = Hash.new [:id] = SecureRandom.hex(32) [:message_body] = Hash.new [:message_body][:eventVersion] = 1.0 [:message_body][:eventSource] = "application:Que" [:message_body][:eventTime] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ") [:message_body][:eventName] = type.to_s.camelize(:lower) [:message_body][:userIdentity] = { :principalId => user_id, :companyId => company_id } [:message_body][:Que] = data [:message_body][:Que][:configurationId] = [:message_body][:eventName] [:message_body] = { :Records => [[:message_body]] }.to_json [:message_attributes] = attributes if attributes [:delay_seconds] = delay if delay end |
#get_processor ⇒ Object
39 40 41 |
# File 'lib/herdst_worker/queue/facade.rb', line 39 def get_processor self.processor end |
#get_queue_client(queue_name) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/herdst_worker/queue/facade.rb', line 136 def get_queue_client(queue_name) queue_url = self.app.queues[queue_name] sqs_client = Aws::SQS::Client.new( :credentials => self.app.config..get_aws_credentials ) Aws::SQS::Queue.new( :url => queue_url, :client => sqs_client ) end |
#get_status ⇒ Object
19 20 21 |
# File 'lib/herdst_worker/queue/facade.rb', line 19 def get_status self.processor.processor_status end |
#halt ⇒ Object
29 30 31 |
# File 'lib/herdst_worker/queue/facade.rb', line 29 def halt self.processor.halt end |
#send_email_message(template, company, data = {}, attributes = nil, delay = nil) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/herdst_worker/queue/facade.rb', line 62 def (template, company, data = {}, attributes = nil, delay = nil) client = get_queue_client(:notifications) = (template, company, data, attributes, delay) .delete(:id) client.() end |
#send_message(type, company_id, user_id, data = {}, queue = :primary, attributes = nil, delay = nil) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/herdst_worker/queue/facade.rb', line 44 def (type, company_id, user_id, data = {}, queue = :primary, attributes = nil, delay = nil) client = get_queue_client(queue) = (type, company_id, user_id, data, attributes, delay) .delete(:id) client.() end |
#send_messages(messages = [], queue = :primary) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/herdst_worker/queue/facade.rb', line 53 def ( = [], queue = :primary) client = get_queue_client(queue) .each_slice(10) do || client.({ :entries => }) end end |
#start ⇒ Object
24 25 26 |
# File 'lib/herdst_worker/queue/facade.rb', line 24 def start self.processor.start end |
#stop ⇒ Object
34 35 36 |
# File 'lib/herdst_worker/queue/facade.rb', line 34 def stop self.processor.stop end |