forked from mogadanez/sqsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
104 lines (90 loc) · 4.25 KB
/
cli.js
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
#!/usr/bin/env node
'use strict';
var dotenv = require('dotenv');
var logger = require('./lib/logger');
var sqsd = require('./lib/index').SQSProcessor;
var program = require('commander');
var _ = require('lodash');
var path = require('path');
function increaseVerbosity(v, total) {
return total + 1;
}
var pkg = require('./package.json');
program.version(pkg.version)
.option('-w, --web-hook [value]', 'The webhook url to which messages from queue be posted. Required' )
.option('-q, --queue-url [value]', 'Your queue URL.')
.option('--queue-name [value]', 'The name of the queue. Fetched from queue URL if empty')
.option('--access-key-id [value]', 'Your AWS Access Key. Leave empty if use IAM roles.')
.option('--secret-access-key [value]', 'Your AWS Secret Access Key. Leave empty if use IAM roles.')
.option('--endpoint-url [value]', 'Your endpoint Url if your using a fake service. Leave empty if using Amazon sqs.')
.option('-r, --region [value]', 'The region name of the AWS SQS queue')
.option('-m, --max-messages [value]', 'Max number of messages to retrieve per request.',parseInt )
.option('-d, --daemonized ', 'Whether to continue running with empty queue' )
.option('-s, --sleep [value]', 'Number of seconds to wait after polling empty queue when daemonized', parseInt)
.option('-t, --timeout [value]', 'Timeout for waiting response from worker, ms' )
.option('--worker-health-url [value]', 'Url for checking that worker is running, useful when running in linked containers and worker needs some time to up' )
.option('--worker-health-wait-time [value]', 'Timeout for waiting while worker become health, ms' )
.option('--wait-time [value]', 'Long polling wait time when querying the queue.', parseInt)
.option('--content-type [value]', 'Long polling wait time when querying the queue.' )
.option('--concurrency [value]', 'Long polling wait time when querying the queue.', parseInt, 3 )
.option('--user-agent [value]', 'User agent', "sqsd" )
.option('--env [value]', 'Path to .env file to load environment variables from. Optional', '.env')
.option('--ssl-enabled [value]', 'To enable ssl or not. Default is true')
.option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)
process.argv[1] = 'sqsd';
program.parse(process.argv);
var defaults = {
region: "us-east-1"
, maxMessages: 10
, daemonized: false
, sleep: 0
, waitTime: 20
, userAgent: "sqsd"
, contentType: 'application/json'
, concurrency: 3
, timeout: 60000
, workerHealthWaitTime: 10000
, sslEnabled: true
, verbose: 0
}
dotenv.config({
silent: true,
path: path.resolve(__dirname, program.env)
});
var envParams = { accessKeyId: process.env.AWS_ACCESS_KEY_ID
, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
, region: process.env.SQSD_QUEUE_REGION_NAME || process.env.AWS_DEFAULT_REGION
, queueUrl: process.env.SQSD_QUEUE_URL
, maxMessages: process.env.SQSD_MAX_MESSAGES_PER_REQUEST
, daemonized: (process.env.SQSD_RUN_DAEMONIZED || "") in { "1":1, "yes":1, "true":1 }
, sleep: process.env.SQSD_SLEEP_SECONDS
, waitTime: process.env.SQSD_WAIT_TIME_SECONDS
, webHook: process.env.SQSD_WORKER_HTTP_URL
, userAgent: process.env.SQSD_WORKER_USER_AGENT
, contentType: process.env.SQSD_WORKER_HTTP_REQUEST_CONTENT_TYPE
, concurrency: process.env.SQSD_WORKER_CONCURRENCY
, timeout: process.env.SQSD_WORKER_TIMEOUT
, workerHealthUrl: process.env.SQSD_WORKER_HEALTH_URL
, workerHealthWaitTime: process.env.SQSD_WORKER_HEALTH_WAIT_TIME
, endpointUrl: process.env.SQSD_ENDPOINT_URL
, queueName: process.env.SQSD_QUEUE_NAME
, sslEnabled: process.env.SQSD_SSL_ENABLED
, verbose: process.env.VERBOSE
}
var extractedCliArgs = _.pick(program, _.keys(envParams));
var mergedParams = _.defaults(extractedCliArgs, envParams, defaults);
if (!mergedParams.webHook) {
console.log ( "--web-hook is required", mergedParams.verbose)
//program.outputHelp();
process.exit(1);
}
logger.init( mergedParams.verbose );
logger.info('SQSD v' + pkg.version);
new sqsd(mergedParams).start()
.then(()=>{
process.exit(0);
})
.catch( err=> {
logger.error( {err:err}, "Unexpected error")
process.exit(1);
})