diff --git a/server/index.js b/server/index.js index 949fffd4..8ec45b4d 100644 --- a/server/index.js +++ b/server/index.js @@ -38,17 +38,6 @@ if (config.title) { process.title = config.title; } -/* -const cleanExit = () => process.exit(); -process.on('SIGINT', cleanExit); // catch ctrl-c -process.on('SIGTERM', cleanExit); // catch kill - -process.on('exit', function() { - // TODO -}); - */ - - async function startHTTPServer(appType, appName, port) { const app = await appBuilder.createApp(appType); app.set('port', port); diff --git a/server/lib/builtin-zone-mta.js b/server/lib/builtin-zone-mta.js index 53e31ccc..06db0660 100644 --- a/server/lib/builtin-zone-mta.js +++ b/server/lib/builtin-zone-mta.js @@ -1,7 +1,7 @@ 'use strict'; const config = require('config'); -const fork = require('child_process').fork; +const fork = require('./fork').fork; const log = require('./log'); const path = require('path'); const fs = require('fs-extra') diff --git a/server/lib/executor.js b/server/lib/executor.js index ae914648..43e66749 100644 --- a/server/lib/executor.js +++ b/server/lib/executor.js @@ -1,6 +1,6 @@ 'use strict'; -const fork = require('child_process').fork; +const fork = require('./fork').fork; const log = require('./log'); const path = require('path'); const bluebird = require('bluebird'); diff --git a/server/lib/feedcheck.js b/server/lib/feedcheck.js index c8cb80d4..22e7f8ed 100644 --- a/server/lib/feedcheck.js +++ b/server/lib/feedcheck.js @@ -1,6 +1,6 @@ 'use strict'; -const fork = require('child_process').fork; +const fork = require('./fork').fork; const log = require('./log'); const path = require('path'); const senders = require('./senders'); diff --git a/server/lib/fork.js b/server/lib/fork.js new file mode 100644 index 00000000..505111dc --- /dev/null +++ b/server/lib/fork.js @@ -0,0 +1,31 @@ +'use strict'; + +const builtinFork = require('child_process').fork; + +const cleanExit = () => process.exit(); +process.on('SIGINT', cleanExit); // catch ctrl-c +process.on('SIGTERM', cleanExit); // catch kill + +const children = []; + +process.on('message', msg => { + if (msg === 'exit') { + cleanExit(); + } +}); + + +process.on('exit', function() { + for (const child of children) { + child.send('exit'); + } +}); + +function fork(path, args, opts) { + const child = builtinFork(path, args, opts); + + children.push(child); + return child; +} + +module.exports.fork = fork; diff --git a/server/lib/importer.js b/server/lib/importer.js index 11576633..f3a2b54b 100644 --- a/server/lib/importer.js +++ b/server/lib/importer.js @@ -1,7 +1,7 @@ 'use strict'; const knex = require('./knex'); -const fork = require('child_process').fork; +const fork = require('./fork').fork; const log = require('./log'); const path = require('path'); const {ImportStatus, RunStatus} = require('../../shared/imports'); diff --git a/server/lib/senders.js b/server/lib/senders.js index da48803f..7ea15a28 100644 --- a/server/lib/senders.js +++ b/server/lib/senders.js @@ -1,6 +1,6 @@ 'use strict'; -const fork = require('child_process').fork; +const fork = require('./fork').fork; const log = require('./log'); const path = require('path'); const knex = require('./knex'); diff --git a/server/services/executor.js b/server/services/executor.js index ba08bab2..f772d0d5 100644 --- a/server/services/executor.js +++ b/server/services/executor.js @@ -6,7 +6,7 @@ const config = require('config'); const reportHelpers = require('../lib/report-helpers'); -const fork = require('child_process').fork; +const fork = require('../lib/fork').fork; const path = require('path'); const log = require('../lib/log'); const fs = require('fs'); diff --git a/server/services/feedcheck.js b/server/services/feedcheck.js index 15f5749c..170b65d7 100644 --- a/server/services/feedcheck.js +++ b/server/services/feedcheck.js @@ -8,6 +8,7 @@ const { CampaignType, CampaignStatus, CampaignSource } = require('../../shared/c const util = require('util'); const campaigns = require('../models/campaigns'); const contextHelpers = require('../lib/context-helpers'); +require('../lib/fork'); const { tLog } = require('../lib/translate'); diff --git a/server/services/importer.js b/server/services/importer.js index 31992955..48aa8a8c 100644 --- a/server/services/importer.js +++ b/server/services/importer.js @@ -17,6 +17,7 @@ const shares = require('../models/shares'); const { tLog } = require('../lib/translate'); const {ListActivityType} = require('../../shared/activity-log'); const activityLog = require('../lib/activity-log'); +require('../lib/fork'); const csvparse = require('csv-parse'); diff --git a/server/services/sender-master.js b/server/services/sender-master.js index 4cd03a2c..1bec702d 100644 --- a/server/services/sender-master.js +++ b/server/services/sender-master.js @@ -1,7 +1,7 @@ 'use strict'; const config = require('config'); -const fork = require('child_process').fork; +const fork = require('../lib/fork').fork; const log = require('../lib/log'); const path = require('path'); const knex = require('../lib/knex'); @@ -11,6 +11,7 @@ const campaigns = require('../models/campaigns'); const builtinZoneMta = require('../lib/builtin-zone-mta'); const {CampaignActivityType} = require('../../shared/activity-log'); const activityLog = require('../lib/activity-log'); +require('../lib/fork'); let messageTid = 0; diff --git a/server/services/sender-worker.js b/server/services/sender-worker.js index c96cb3a0..cbe7750f 100644 --- a/server/services/sender-worker.js +++ b/server/services/sender-worker.js @@ -5,6 +5,7 @@ const log = require('../lib/log'); const mailers = require('../lib/mailers'); const CampaignSender = require('../lib/campaign-sender'); const {enforce} = require('../lib/helpers'); +require('../lib/fork'); const workerId = Number.parseInt(process.argv[2]); let running = false; diff --git a/server/services/workers/reports/report-processor.js b/server/services/workers/reports/report-processor.js index 80c48134..495e878e 100644 --- a/server/services/workers/reports/report-processor.js +++ b/server/services/workers/reports/report-processor.js @@ -12,6 +12,7 @@ const knex = require('../../../lib/knex'); const contextHelpers = require('../../../lib/context-helpers'); const {renderCsvFromStream} = require('../../../lib/report-helpers'); const stream = require('stream'); +require('../../../lib/fork'); async function main() { try { diff --git a/zone-mta/plugins/mailtrain-main.js b/zone-mta/plugins/mailtrain-main.js index b4fcd460..45494b9b 100644 --- a/zone-mta/plugins/mailtrain-main.js +++ b/zone-mta/plugins/mailtrain-main.js @@ -10,5 +10,10 @@ module.exports.init = (app, done) => { type: 'zone-mta-started' }); + process.on('message', msg => { + if (msg === 'exit') { + process.exit(); } + }); + done(); };