var jobs = require('./server/kue').jobs;
var csw = require('./server/workers/harvest-csw');
var wfs = require('./server/workers/lookup-wfs');
var processRecord = require('./server/workers/process-record');

// To remove in the future
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

require('ssl-root-cas/latest').inject();

jobs.process('harvest-csw', 4, csw.harvest);
jobs.process('lookup-wfs', 10, wfs.lookup);
jobs.process('process-record', 20, processRecord);

require('kue').app.listen(process.env.PORT || 3000);

@@ -1,16 +1,41 @@
 var 
-jobs
+_ = require('lodash');%0A%0Avar q
  = requi
@@ -343,20 +343,17 @@
 ect();%0A%0A
-jobs
+q
 .process
@@ -385,20 +385,17 @@
 rvest);%0A
-jobs
+q
 .process
@@ -426,20 +426,17 @@
 ookup);%0A
-jobs
+q
 .process
@@ -528,8 +528,482 @@
  3000);%0A
+%0Avar gracefulShutdown = _.once(function () %7B%0A    q.shutdown(function (err) %7B%0A        console.log( 'Job queue is shut down. ', err %7C%7C '');%0A        process.exit(0);%0A    %7D, 5000);%0A%7D);%0A%0Aprocess.on('message', function (msg) %7B%0A    if (msg === 'shutdown') %7B%0A        gracefulShutdown();%0A    %7D%0A%7D);%0A%0Aprocess.on('SIGTERM', gracefulShutdown);%0A%0Aprocess.on('uncaughtException', function (err) %7B%0A    console.log('Uncaught exception!!');%0A    console.trace(err);%0A    gracefulShutdown();%0A%7D);%0A

Implement graceful shutdown in worker process