herdst_worker 0.1.5 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,162 +1,164 @@
1
- require 'securerandom'
2
- require_relative 'processor'
3
-
4
-
5
- module HerdstWorker
6
- module Queue
7
- class Facade
8
-
9
-
10
- attr_accessor :app, :processor
11
-
12
-
13
- def initialize(app, enabled, url, queue_wait_time)
14
- self.app = app
15
- self.processor = HerdstWorker::Queue::Processor.new(app, enabled, url, queue_wait_time)
16
- end
17
-
18
-
19
- def get_status
20
- self.processor.processor_status
21
- end
22
-
23
-
24
- def start
25
- self.processor.start
26
- end
27
-
28
-
29
- def halt
30
- self.processor.halt
31
- end
32
-
33
-
34
- def stop
35
- self.processor.stop
36
- end
37
-
38
-
39
- def get_processor
40
- self.processor
41
- end
42
-
43
-
44
- def send_message(type, company_id, user_id, data = {}, queue = :primary, attributes = nil, delay = nil)
45
- client = get_queue_client(queue)
46
- message = create_queue_message(type, company_id, user_id, data, attributes, delay)
47
- message.delete(:id)
48
-
49
- client.send_message(message)
50
- end
51
-
52
-
53
- def send_messages(messages = [], queue = :primary)
54
- client = get_queue_client(queue)
55
-
56
- messages.each_slice(10) do |messages_group|
57
- client.send_messages({ :entries => messages_group })
58
- end
59
- end
60
-
61
-
62
- def send_email_message(template, company, data = {}, attributes = nil, delay = nil)
63
- client = get_queue_client(:notifications)
64
-
65
- message = create_email_queue_message(template, company, data, attributes, delay)
66
- message.delete(:id)
67
-
68
- client.send_message(message)
69
- end
70
-
71
-
72
- def create_queue_message(type, company_id, user_id, data, attributes = nil, delay = nil)
73
- message = Hash.new
74
- message[:id] = SecureRandom.hex(32)
75
- message[:message_body] = Hash.new
76
- message[:message_body][:eventVersion] = 1.0
77
- message[:message_body][:eventSource] = "application:Que"
78
- message[:message_body][:eventTime] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
79
- message[:message_body][:eventName] = type.to_s.camelize(:lower)
80
- message[:message_body][:userIdentity] = { :principalId => user_id, :companyId => company_id }
81
- message[:message_body][:Que] = data
82
- message[:message_body][:Que][:configurationId] = message[:message_body][:eventName]
83
-
84
- message[:message_body] = { :Records => [message[:message_body]] }.to_json
85
- message[:message_attributes] = attributes if attributes
86
- message[:delay_seconds] = delay if delay
87
- message
88
- end
89
-
90
-
91
- def create_email_queue_message(template, company, message_data, attributes = nil, delay = 2)
92
- # Convert instances of ApplicationRecord to attributes instead of json representation
93
- message_data.each do |key, value|
94
- message_data[key] = value.attributes if value.is_a?(ActiveRecord::Base)
95
- end
96
-
97
- # Build data
98
- data = Hash.new
99
- data[:app] = self.app.name
100
- data[:app_id] = company.id
101
- data[:app_name] = company.name
102
- data[:app_slug] = company.get_host_name
103
- data[:app_brand_id] = company.brand_id
104
- data[:app_host] = company.get_url
105
- data[:type] = "email"
106
- data[:template] = template
107
- data[:data] = message_data
108
-
109
- # Add message structure
110
- message = Hash.new
111
- message[:id] = SecureRandom.hex(32)
112
- message[:message_body] = data.to_json
113
- message[:message_attributes] = attributes if attributes
114
- message[:delay_seconds] = delay
115
- message
116
- end
117
-
118
-
119
- def create_notification_message(message_data, attributes = nil, expiry = nil, delay = nil)
120
- message = Hash.new
121
- message[:id] = SecureRandom.hex(32)
122
- message[:message_body] = Hash.new
123
- message[:message_body][:Type] = "Notification"
124
- message[:message_body][:Message] = message_data.to_json
125
- message[:message_body] = message[:message_body].to_json
126
- message[:message_attributes] = attributes ? attributes : Hash.new
127
- message[:message_attributes]["expiry"] = { :string_value => expiry.to_s, :data_type => "Number" } if expiry
128
- message[:delay_seconds] = delay if delay
129
-
130
- message.delete(:message_attributes) if message[:message_attributes].size == 0
131
-
132
- message
133
- end
134
-
135
-
136
- def get_queue_client(queue_name)
137
- queue_url = self.app.queues[queue_name]
138
- sqs_client = Aws::SQS::Client.new(
139
- :credentials => self.app.config.metadata.get_aws_credentials
140
- )
141
-
142
- Aws::SQS::Queue.new(
143
- :url => queue_url,
144
- :client => sqs_client
145
- )
146
- end
147
-
148
-
149
- def self.get_delay(index)
150
- messages_per_second = 10
151
- send_time_per_message = 0.4
152
-
153
- offset = (index / messages_per_second)
154
- delay = (offset * (messages_per_second * send_time_per_message)).to_i
155
-
156
- delay > 900 ? 900 : delay
157
- end
158
-
159
-
160
- end
161
- end
162
- end
1
+ require 'securerandom'
2
+ require_relative 'processor'
3
+
4
+
5
+ module HerdstWorker
6
+ module Queue
7
+ class Facade
8
+
9
+
10
+ attr_accessor :app, :enabled, :url, :queue_wait_time, :processor
11
+
12
+
13
+ def initialize(app, enabled, url, queue_wait_time)
14
+ self.app = app
15
+ self.enabled = enabled
16
+ self.url = url
17
+ self.queue_wait_time = queue_wait_time
18
+ end
19
+
20
+
21
+ def get_status
22
+ self.get_processor.processor_status
23
+ end
24
+
25
+
26
+ def start
27
+ self.get_processor.start
28
+ end
29
+
30
+
31
+ def halt
32
+ self.get_processor.halt
33
+ end
34
+
35
+
36
+ def stop
37
+ self.get_processor.stop
38
+ end
39
+
40
+
41
+ def get_processor
42
+ unless self.processor
43
+ self.processor = HerdstWorker::Queue::Processor.new(app, self.enabled, self.url, self.queue_wait_time)
44
+ end
45
+
46
+ self.processor
47
+ end
48
+
49
+
50
+ def send_message(type, company_id, user_id, data = {}, attributes = nil, delay = nil)
51
+ message = create_queue_message(type, company_id, user_id, data, attributes, delay)
52
+ message.delete(:id)
53
+
54
+ get_queue_client.send_message(message)
55
+ end
56
+
57
+
58
+ def send_messages(messages = [])
59
+ client = get_queue_client
60
+
61
+ messages.each_slice(10) do |messages_group|
62
+ client.send_messages({ :entries => messages_group })
63
+ end
64
+ end
65
+
66
+
67
+ def send_email_message(template, company, data = {}, attributes = nil, delay = nil)
68
+ message = create_email_queue_message(template, company, data, attributes, delay)
69
+ message.delete(:id)
70
+
71
+ get_queue_client.send_message(message)
72
+ end
73
+
74
+
75
+ def create_queue_message(type, company_id, user_id, data, attributes = nil, delay = nil)
76
+ message = Hash.new
77
+ message[:id] = SecureRandom.hex(32)
78
+ message[:message_body] = Hash.new
79
+ message[:message_body][:eventVersion] = 1.0
80
+ message[:message_body][:eventSource] = "application:Que"
81
+ message[:message_body][:eventTime] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
82
+ message[:message_body][:eventName] = type.to_s.camelize(:lower)
83
+ message[:message_body][:userIdentity] = { :principalId => user_id, :companyId => company_id }
84
+ message[:message_body][:Que] = data
85
+ message[:message_body][:Que][:configurationId] = message[:message_body][:eventName]
86
+
87
+ message[:message_body] = { :Records => [message[:message_body]] }.to_json
88
+ message[:message_attributes] = attributes if attributes
89
+ message[:delay_seconds] = delay if delay
90
+ message
91
+ end
92
+
93
+
94
+ def create_email_queue_message(template, company, message_data, attributes = nil, delay = 2)
95
+ # Convert instances of ApplicationRecord to attributes instead of json representation
96
+ message_data.each do |key, value|
97
+ message_data[key] = value.attributes if value.is_a?(ActiveRecord::Base)
98
+ end
99
+
100
+ # Build data
101
+ data = Hash.new
102
+ data[:app] = self.app.name
103
+ data[:app_id] = company.id
104
+ data[:app_name] = company.name
105
+ data[:app_slug] = company.get_host_name
106
+ data[:app_brand_id] = company.brand_id
107
+ data[:app_host] = company.get_url
108
+ data[:type] = "email"
109
+ data[:template] = template
110
+ data[:data] = message_data
111
+
112
+ # Add message structure
113
+ message = Hash.new
114
+ message[:id] = SecureRandom.hex(32)
115
+ message[:message_body] = data.to_json
116
+ message[:message_attributes] = attributes if attributes
117
+ message[:delay_seconds] = delay
118
+ message
119
+ end
120
+
121
+
122
+ def create_notification_message(message_data, attributes = nil, expiry = nil, delay = nil)
123
+ message = Hash.new
124
+ message[:id] = SecureRandom.hex(32)
125
+ message[:message_body] = Hash.new
126
+ message[:message_body][:Type] = "Notification"
127
+ message[:message_body][:Message] = message_data.to_json
128
+ message[:message_body] = message[:message_body].to_json
129
+ message[:message_attributes] = attributes ? attributes : Hash.new
130
+ message[:message_attributes]["expiry"] = { :string_value => expiry.to_s, :data_type => "Number" } if expiry
131
+ message[:delay_seconds] = delay if delay
132
+
133
+ message.delete(:message_attributes) if message[:message_attributes].size == 0
134
+
135
+ message
136
+ end
137
+
138
+
139
+ def get_queue_client
140
+ sqs_client = Aws::SQS::Client.new(
141
+ :credentials => self.app.config.metadata.get_aws_credentials
142
+ )
143
+
144
+ Aws::SQS::Queue.new(
145
+ :url => self.url,
146
+ :client => sqs_client
147
+ )
148
+ end
149
+
150
+
151
+ def self.get_delay(index)
152
+ messages_per_second = 10
153
+ send_time_per_message = 0.4
154
+
155
+ offset = (index / messages_per_second)
156
+ delay = (offset * (messages_per_second * send_time_per_message)).to_i
157
+
158
+ delay > 900 ? 900 : delay
159
+ end
160
+
161
+
162
+ end
163
+ end
164
+ end