-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathcommand.coffee
321 lines (308 loc) · 8.03 KB
/
command.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
dashdash = require 'dashdash'
UUID = require 'uuid'
MeshbluCoreRunner = require './src/meshblu-core-runner'
packageJSON = require './package.json'
SigtermHandler = require 'sigterm-handler'
options = [
{
name: 'version'
type: 'bool'
help: 'Print tool version and exit.'
}
{
names: ['help', 'h']
type: 'bool'
help: 'Print this help and exit.'
}
{
names: ['test-start']
type: 'bool'
help: 'Verify start and then die. (used for testing)'
}
{
names: ['namespace', 'n']
type: 'string'
help: 'request/response queue namespace'
default: 'meshblu'
env: 'NAMESPACE'
}
{
names: ['request-queue-name']
type: 'string'
help: 'request queue namespace'
default: 'v2:request:queue'
env: 'REQUEST_QUEUE_NAME'
}
{
names: ['response-queue-base-name']
type: 'string'
help: 'response queue base namespace'
default: 'v2:response:queue'
env: 'RESPONSE_QUEUE_BASE_NAME'
}
{
names: ['single-run', 's']
type: 'bool'
help: 'perform only one job, then exit'
}
{
names: ['timeout', 't']
type: 'positiveInteger'
help: 'seconds to wait for the next job'
default: 15
}
{
names: ['concurrency', 'c']
type: 'positiveInteger'
help: 'number of concurrent jobs to process'
default: 3
env: 'CONCURRENCY'
}
{
name: 'redis-uri'
type: 'string'
help: 'URI for Redis'
env: 'REDIS_URI',
default: 'redis://localhost:6379'
}
{
name: 'cache-redis-uri'
type: 'string'
help: 'Cache URI for Redis'
env: 'CACHE_REDIS_URI',
default: 'redis://localhost:6379'
}
{
name: 'firehose-redis-uri'
type: 'string'
help: 'URI for Firehose redis'
env: 'FIREHOSE_REDIS_URI'
default: 'redis://localhost:6379'
}
{
name: 'firehose-port'
type: 'number'
help: 'Port for firehose service'
env: 'FIREHOSE_PORT'
default: 3080
}
{
name: 'disable-firehose'
type: 'bool'
help: 'Flag to disabled firehose'
env: 'FIREHOSE_DISABLED'
default: false
}
{
name: 'firehose-namespace'
type: 'string'
help: 'Namespace for firehose'
env: 'FIREHOSE_HOSE'
default: 'messages'
}
{
name: 'mongodb-uri'
type: 'string'
help: 'URI for MongoDB'
env: 'MONGODB_URI'
default: 'mongodb://localhost:27017/meshblu-test'
}
{
name: 'pepper'
type: 'string'
help: 'Pepper for encryption'
env: 'PEPPER'
}
{
name: 'alias-server-uri'
type: 'string'
help: 'URI for alias server'
env: 'ALIAS_SERVER_URI'
}
{
name: 'worker-name'
type: 'string'
help: 'name of this worker'
env: 'WORKER_NAME'
}
{
name: 'job-log-redis-uri'
type: 'string'
help: 'URI for job log Redis'
env: 'JOB_LOG_REDIS_URI'
default: 'redis://localhost:6379'
}
{
name: 'job-log-queue'
type: 'string'
help: 'Job log queue name'
env: 'JOB_LOG_QUEUE'
default: 'meshblu-core-log'
}
{
name: 'job-log-sample-rate'
type: 'number'
help: 'Job log sample rate (0.00 to 1.00)'
env: 'JOB_LOG_SAMPLE_RATE'
default: '0.00'
}
{
name: 'job-timeout-seconds'
type: 'positiveInteger'
help: 'Timeout for job execution'
env: 'JOB_TIMEOUT_SECONDS',
default: 30
}
{
name: 'max-connections'
type: 'positiveInteger'
help: 'Max number of redis connections of the http protocol'
env: 'CONNECTION_POOL_MAX_CONNECTIONS',
default: 50
}
{
name: 'meshblu-http-port'
type: 'positiveInteger'
help: 'Port to listen on for HTTP'
env: 'MESHBLU_HTTP_PORT',
default: 80
}
{
name: 'private-key-base64'
type: 'string'
help: 'Base64-encoded private key'
env: 'PRIVATE_KEY_BASE64'
}
{
name: 'public-key-base64'
type: 'string'
help: 'Base64-encoded public key'
env: 'PUBLIC_KEY_BASE64'
}
{
name: 'webhook-queue-name'
type: 'string'
env: 'WEBHOOK_QUEUE_NAME'
default: 'webhooks'
help: 'Name of Redis webhook work queue'
},
{
name: 'webhook-queue-timeout'
type: 'positiveInteger'
env: 'WEBHOOK_QUEUE_TIMEOUT'
default: 30
help: 'BRPOP timeout (in seconds) for webhooks'
},
{
name: 'webhook-request-timeout'
type: 'positiveInteger'
env: 'WEBHOOK_REQUEST_TIMEOUT'
default: 5
help: 'Request timeout (in seconds) for webhooks'
},
{
name: 'webhook-namespace'
type: 'string'
env: 'WEBHOOK_NAMESPACE'
default: 'meshblu-webhooks'
help: 'Redis namespace for webhooks'
},
{
name: 'disable-webhook-worker'
type: 'bool'
env: 'DISABLE_WEBHOOK_WORKER'
default: false
help: 'option for disabling webhook worker.'
}
]
parser = dashdash.createParser(options: options)
try
opts = parser.parse(process.argv)
catch error
console.error 'meshblu: error: %s', error.message
process.exit 1
if opts.version
console.log "meshblu v#{packageJSON.version}"
process.exit 0
if opts.help
help = parser.help({includeEnv: true, includeDefaults: true}).trimRight()
console.log 'usage: node command.js [OPTIONS]\n' + 'options:\n' + help
process.exit 0
if opts.private_key_base64?
privateKey = new Buffer(opts.private_key_base64, 'base64').toString('utf8')
if opts.public_key_base64?
publicKey = new Buffer(opts.public_key_base64, 'base64').toString('utf8')
meshbluConfig =
hostname: 'localhost'
port: opts.meshblu_http_port
protocol: 'http'
opts.pepper ||= process.env.TOKEN
options = {
dispatcherWorker:
namespace: opts.namespace
requestQueueName: opts.request_queue_name
timeoutSeconds: opts.timeout
redisUri: opts.redis_uri
cacheRedisUri: opts.cache_redis_uri
firehoseRedisUri: opts.firehose_redis_uri
mongoDBUri: opts.mongodb_uri
pepper: opts.pepper
workerName: opts.worker_name
aliasServerUri: opts.alias_server_uri
jobLogRedisUri: opts.job_log_redis_uri
jobLogQueue: opts.job_log_queue
jobLogSampleRate: opts.job_log_sample_rate
privateKey: privateKey
publicKey: publicKey
singleRun: opts.single_run
concurrency: opts.concurrency
meshbluHttp:
redisUri: opts.redis_uri
cacheRedisUri: opts.cache_redis_uri
requestQueueName: opts.request_queue_name
responseQueueName: "#{opts.responseQueueBaseName}:#{UUID.v1()}"
responseQueueBaseName: opts.response_queue_base_name
namespace: opts.namespace
jobLogRedisUri: opts.job_log_redis_uri
jobLogQueue: opts.job_log_queue
jobLogSampleRate: opts.job_log_sample_rate
jobTimeoutSeconds: opts.job_timeout_seconds
maxConnections: opts.max_connections
port: opts.meshblu_http_port
webhookWorker:
disable: opts.disable_webhook_worker
namespace: opts.webhook_namespace
redisUri: opts.redis_uri
queueName: opts.webhook_queue_name
queueTimeout: opts.webhook_queue_timeout
requestTimeout: opts.webhook_request_timeout
jobLogRedisUri: opts.job_log_redis_uri
jobLogQueue: opts.job_log_queue
jobLogSampleRate: opts.job_log_sample_rate
privateKey: privateKey
meshbluConfig: meshbluConfig
meshbluFirehose:
disable: opts.disable_firehose
namespace: opts.firehose_namespace
redisUri: opts.redis_uri
firehoseRedisUri: opts.firehose_redis_uri
port: opts.firehose_port
meshbluConfig: meshbluConfig
}
meshbluCoreRunner = new MeshbluCoreRunner options
sigtermHandler = new SigtermHandler({ events: ['SIGTERM', 'SIGINT'] })
sigtermHandler.register meshbluCoreRunner.stop
meshbluCoreRunner.catchErrors()
meshbluCoreRunner.on 'error', (error) -> throw error
meshbluCoreRunner.prepare (error) =>
if error
meshbluCoreRunner.reportError error
console.error error.stack
process.exit 1
meshbluCoreRunner.run (error) =>
if error
meshbluCoreRunner.reportError error
console.error error.stack
process.exit 1
if opts.test_start
return process.exit(0)