1 | commander = require 'commander'
|
2 | async = require 'async'
|
3 | redis = require 'redis'
|
4 | RedisNS = require '@octoblu/redis-ns'
|
5 | MeshbluConfig = require 'meshblu-config'
|
6 | JobManager = require 'meshblu-core-job-manager'
|
7 | packageJSON = require './package.json'
|
8 | QueueWorker = require './src/queue-worker'
|
9 |
|
10 | class Command
|
11 | parseInt: (str) =>
|
12 | parseInt str
|
13 |
|
14 | parseOptions: =>
|
15 | commander
|
16 | .version packageJSON.version
|
17 | .option '-n, --namespace <friendly-sharefile>', 'job handler queue namespace.', 'friendly-sharefile'
|
18 | .option '-s, --single-run', 'perform only one job.'
|
19 | .option '-t, --timeout <600>', 'seconds to wait for a next job.', @parseInt, 600
|
20 | .parse process.argv
|
21 |
|
22 | {@namespace,@singleRun,@timeout} = commander
|
23 |
|
24 | @jobLogQueue = process.env.JOB_LOG_QUEUE
|
25 | @jobLogRedisUri = process.env.JOB_LOG_REDIS_URI
|
26 | @jobLogSampleRate = parseFloat process.env.JOB_LOG_SAMPLE_RATE || '0.01'
|
27 |
|
28 | if process.env.FRIENDLY_SHAREFILE_NAMESPACE?
|
29 | @namespace = process.env.FRIENDLY_SHAREFILE_NAMESPACE
|
30 |
|
31 | if process.env.FRIENDLY_SHAREFILE_SINGLE_RUN?
|
32 | @singleRun = process.env.FRIENDLY_SHAREFILE_SINGLE_RUN == 'true'
|
33 |
|
34 | if process.env.FRIENDLY_SHAREFILE_TIMEOUT?
|
35 | @timeout = parseInt process.env.FRIENDLY_SHAREFILE_TIMEOUT
|
36 |
|
37 | if process.env.REDIS_URI
|
38 | @redisUri = process.env.REDIS_URI
|
39 | else
|
40 | @redisUri = 'redis://localhost:6379'
|
41 |
|
42 | run: =>
|
43 | console.log 'Starting...'
|
44 | @parseOptions()
|
45 | client = new RedisNS @namespace, redis.createClient @redisUri
|
46 | jobManager = new JobManager {client, timeoutSeconds: @timeout, @jobLogRedisUri, @jobLogQueue, @jobLogSampleRate }
|
47 | process.on 'SIGTERM', => @terminate = true
|
48 | process.on 'SIGINT', => @terminate = true
|
49 |
|
50 | meshbluConfig = new MeshbluConfig().toJSON()
|
51 |
|
52 | return @queueWorkerRun {jobManager, meshbluConfig}, @die if @singleRun
|
53 | async.until @terminated, async.apply(@queueWorkerRun, {jobManager, meshbluConfig}), @die
|
54 |
|
55 | terminated: => @terminate
|
56 |
|
57 | queueWorkerRun: ({jobManager, meshbluConfig}, callback) =>
|
58 | queueWorker = new QueueWorker {jobManager,meshbluConfig}
|
59 |
|
60 | queueWorker.run (error) =>
|
61 | if error?
|
62 | console.error error.stack
|
63 | process.nextTick callback
|
64 |
|
65 | die: (error) =>
|
66 | return process.exit(0) unless error?
|
67 | console.error error.stack
|
68 | process.exit 1
|
69 |
|
70 | commandWork = new Command()
|
71 | commandWork.run()
|