Class: HerdstWorker::Queue::Facade

Inherits:
Object
  • Object
show all
Defined in:
lib/herdst_worker/queue/facade.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#appObject

Returns the value of attribute app.



10
11
12
# File 'lib/herdst_worker/queue/facade.rb', line 10

def app
  @app
end

#enabledObject

Returns the value of attribute enabled.



10
11
12
# File 'lib/herdst_worker/queue/facade.rb', line 10

def enabled
  @enabled
end

#processorObject

Returns the value of attribute processor.



10
11
12
# File 'lib/herdst_worker/queue/facade.rb', line 10

def processor
  @processor
end

#queue_wait_timeObject

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

#urlObject

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)
    messages_per_second = 10
    send_time_per_message = 0.4
    
    offset = (index / messages_per_second)
    delay = (offset * (messages_per_second * send_time_per_message)).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 create_email_queue_message(template, company, message_data, attributes = nil, delay = 2)
    company_key = company.class.name.downcase.to_sym rescue "company"
    
    # Convert instances of ApplicationRecord to attributes instead of json representation
    message_data.each do |key, value|
        message_data[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] = message_data || {}
    data[:data][company_key] = company unless data[:data][company_key].present?

    # Add message structure
    message = Hash.new
    message[:id] = SecureRandom.hex(32)
    message[:message_body] = data.to_json
    message[:message_attributes] = attributes if attributes
    message[:delay_seconds] = delay
    message
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 create_notification_message(message_data, attributes = nil, expiry = nil, delay = nil)
    message = Hash.new
    message[:id] = SecureRandom.hex(32)
    message[:message_body] = Hash.new
    message[:message_body][:Type] = "Notification"
    message[:message_body][:Message] = message_data.to_json
    message[:message_body] = message[:message_body].to_json
    message[:message_attributes] = attributes ? attributes : Hash.new
    message[:message_attributes]["expiry"] = { :string_value => expiry.to_s, :data_type => "Number" } if expiry
    message[:delay_seconds] = delay if delay
    
    message.delete(:message_attributes) if message[:message_attributes].size == 0
    
    message
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 create_queue_message(type, company_id, user_id, data, attributes = nil, delay = nil)
    message = Hash.new
    message[:id] = SecureRandom.hex(32)
    message[:message_body] = Hash.new
    message[:message_body][:eventVersion] = 1.0
    message[:message_body][:eventSource] = "application:Que"
    message[:message_body][:eventTime] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
    message[:message_body][:eventName] = type.to_s.camelize(:lower)
    message[:message_body][:userIdentity] = { :principalId => user_id, :companyId => company_id }
    message[:message_body][:Que] = data
    message[:message_body][:Que][:configurationId] = message[:message_body][:eventName]

    message[:message_body] = { :Records => [message[:message_body]] }.to_json
    message[:message_attributes] = attributes if attributes
    message[:delay_seconds] = delay if delay
    message
end

#get_processorObject



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_clientObject



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_statusObject



21
22
23
# File 'lib/herdst_worker/queue/facade.rb', line 21

def get_status
    self.get_processor.processor_status
end

#haltObject



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 send_email_message(template, company, data = {}, attributes = nil, delay = nil)
    message = create_email_queue_message(template, company, data, attributes, delay)
    message.delete(:id)
    
    get_queue_client.send_message(message)
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 send_message(type, company_id, user_id, data = {}, attributes = nil, delay = nil)
    message = create_queue_message(type, company_id, user_id, data, attributes, delay)
    message.delete(:id)
    
    get_queue_client.send_message(message)
end

#send_messages(messages = []) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/herdst_worker/queue/facade.rb', line 58

def send_messages(messages = [])
    client = get_queue_client
    
    messages.each_slice(10) do |messages_group|
        client.send_messages({ :entries => messages_group })
    end
end

#startObject



26
27
28
# File 'lib/herdst_worker/queue/facade.rb', line 26

def start
    self.get_processor.start
end

#stopObject



36
37
38
# File 'lib/herdst_worker/queue/facade.rb', line 36

def stop
    self.get_processor.stop
end