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.
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#processor ⇒ Object
Returns the value of attribute processor.
-
#queue_wait_time ⇒ Object
Returns the value of attribute queue_wait_time.
-
#url ⇒ Object
Returns the value of attribute url.
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 ⇒ 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 = {}, attributes = nil, delay = nil) ⇒ Object
- #send_messages(messages = []) ⇒ 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 17 18 |
# File 'lib/herdst_worker/queue/facade.rb', line 13 def initialize(app, enabled, url, queue_wait_time) self.app = app self.enabled = enabled self.url = url self.queue_wait_time = 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 |
#enabled ⇒ Object
Returns the value of attribute enabled.
10 11 12 |
# File 'lib/herdst_worker/queue/facade.rb', line 10 def enabled @enabled 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 |
#queue_wait_time ⇒ Object
Returns the value of attribute queue_wait_time.
10 11 12 |
# File 'lib/herdst_worker/queue/facade.rb', line 10 def queue_wait_time @queue_wait_time end |
#url ⇒ Object
Returns the value of attribute url.
10 11 12 |
# File 'lib/herdst_worker/queue/facade.rb', line 10 def url @url end |
Class Method Details
.get_delay(index) ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/herdst_worker/queue/facade.rb', line 154 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/herdst_worker/queue/facade.rb', line 94 def (template, company, , attributes = nil, delay = 2) company_key = company.class.name.downcase.to_sym rescue "company" # 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.family || self.app.name data[:app_id] = company.id data[:app_name] = company.name data[:app_slug] = company.get_host_name data[:app_theme] = company.get_brand data[:app_host] = company.get_url data[:type] = "email" data[:template] = template data[:data] = || {} data[:data][company_key] = company unless data[:data][company_key].present? # 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
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/herdst_worker/queue/facade.rb', line 125 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/herdst_worker/queue/facade.rb', line 75 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
41 42 43 44 45 46 47 |
# File 'lib/herdst_worker/queue/facade.rb', line 41 def get_processor unless self.processor self.processor = HerdstWorker::Queue::Processor.new(app, self.enabled, self.url, self.queue_wait_time) end self.processor end |
#get_queue_client ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/herdst_worker/queue/facade.rb', line 142 def get_queue_client sqs_client = Aws::SQS::Client.new( :credentials => self.app.config..get_aws_credentials ) Aws::SQS::Queue.new( :url => self.url, :client => sqs_client ) end |
#get_status ⇒ Object
21 22 23 |
# File 'lib/herdst_worker/queue/facade.rb', line 21 def get_status self.get_processor.processor_status end |
#halt ⇒ Object
31 32 33 |
# File 'lib/herdst_worker/queue/facade.rb', line 31 def halt self.get_processor.halt end |
#send_email_message(template, company, data = {}, attributes = nil, delay = nil) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/herdst_worker/queue/facade.rb', line 67 def (template, company, data = {}, attributes = nil, delay = nil) = (template, company, data, attributes, delay) .delete(:id) get_queue_client.() end |
#send_message(type, company_id, user_id, data = {}, attributes = nil, delay = nil) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/herdst_worker/queue/facade.rb', line 50 def (type, company_id, user_id, data = {}, attributes = nil, delay = nil) = (type, company_id, user_id, data, attributes, delay) .delete(:id) get_queue_client.() end |
#send_messages(messages = []) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/herdst_worker/queue/facade.rb', line 58 def ( = []) client = get_queue_client .each_slice(10) do || client.({ :entries => }) end end |
#start ⇒ Object
26 27 28 |
# File 'lib/herdst_worker/queue/facade.rb', line 26 def start self.get_processor.start end |
#stop ⇒ Object
36 37 38 |
# File 'lib/herdst_worker/queue/facade.rb', line 36 def stop self.get_processor.stop end |