herdst_worker 0.1.5 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/herdst_worker_console +24 -0
- data/lib/herdst_worker/adapters/facade.rb +20 -20
- data/lib/herdst_worker/application/facade.rb +98 -89
- data/lib/herdst_worker/autoload/facade.rb +67 -67
- data/lib/herdst_worker/configuration/metadata.rb +5 -1
- data/lib/herdst_worker/configuration/paths.rb +41 -41
- data/lib/herdst_worker/log/facade.rb +87 -87
- data/lib/herdst_worker/queue/facade.rb +164 -162
- data/lib/herdst_worker/queue/processor.rb +225 -223
- data/lib/herdst_worker/queue/runner.rb +137 -137
- data/lib/herdst_worker/signals/facade.rb +35 -35
- data/lib/herdst_worker/version.rb +1 -1
- metadata +8 -6
@@ -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.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
message
|
77
|
-
message[:
|
78
|
-
message[:message_body]
|
79
|
-
message[:message_body][:
|
80
|
-
message[:message_body][:
|
81
|
-
message[:message_body][:
|
82
|
-
message[:message_body][:
|
83
|
-
|
84
|
-
message[:message_body]
|
85
|
-
message[:
|
86
|
-
|
87
|
-
message
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
data
|
102
|
-
data[:
|
103
|
-
data[:
|
104
|
-
data[:
|
105
|
-
data[:
|
106
|
-
data[:
|
107
|
-
data[:
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
message
|
113
|
-
message
|
114
|
-
message[:
|
115
|
-
message
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
message
|
124
|
-
message[:
|
125
|
-
message[:message_body] =
|
126
|
-
message[:
|
127
|
-
message[:
|
128
|
-
message[:
|
129
|
-
|
130
|
-
message
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
delay
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
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
|