   run: =>
     @parseOptions()
 
+    process.on 'SIGTERM', => @terminate = true
+
+    return @doSingleRun @tentativePanic if @singleRun
+    async.until @terminated, @doSingleRun, @tentativePanic
+
+  doSingleRun: (callback) =>
     dispatcher = new Dispatcher
       client:  @getDispatchClient()
       timeout:   @timeout
       jobHandlers: @assembleJobHandlers()
-
-    dispatcher.on 'job', (job) =>
-      debug 'doing a job: ', JSON.stringify job
 
     queueWorker = new QueueWorker
       pepper:    @pepper

      cacheFactory:     new CacheFactory client: redis.createClient(@redisUri)
      datastoreFactory: new DatastoreFactory database: mongojs(@mongoDBUri)

    if @singleRun
      async.parallel [
        async.apply dispatcher.dispatch
        async.apply queueWorker.run
      ], @tentativePanic
      return

    async.forever queueWorker.run, @panic
    async.forever dispatcher.dispatch, @panic

  assembleJobHandlers: =>
    jobAssembler = new JobAssembler

class CommandDispatch
  @ALL_JOBS: ['Authenticate', 'SubscriptionList', 'Idle']

  parseInt: (int) =>
    parseInt int

  parseList: (val) =>
    val.split ','

