2018-09-02 12:59:02 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-09-02 18:17:42 +00:00
|
|
|
const config = require('config');
|
2018-09-02 12:59:02 +00:00
|
|
|
const fork = require('child_process').fork;
|
|
|
|
const log = require('npmlog');
|
|
|
|
const path = require('path');
|
2018-09-02 18:17:42 +00:00
|
|
|
const knex = require('../lib/knex');
|
2018-09-02 12:59:02 +00:00
|
|
|
|
|
|
|
let messageTid = 0;
|
|
|
|
let workerProcesses = new Map();
|
|
|
|
|
|
|
|
let running = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
const path = require('path');
|
|
|
|
const log = require('npmlog');
|
|
|
|
const fsExtra = require('fs-extra-promise');
|
|
|
|
const {ImportSource, MappingType, ImportStatus, RunStatus} = require('../shared/imports');
|
|
|
|
const imports = require('../models/imports');
|
|
|
|
const fields = require('../models/fields');
|
|
|
|
const subscriptions = require('../models/subscriptions');
|
|
|
|
const { Writable } = require('stream');
|
|
|
|
const { cleanupFromPost, enforce } = require('../lib/helpers');
|
|
|
|
const contextHelpers = require('../lib/context-helpers');
|
|
|
|
const tools = require('../lib/tools');
|
|
|
|
const shares = require('../models/shares');
|
|
|
|
const _ = require('../lib/translate')._;
|
|
|
|
*/
|
|
|
|
|
2018-09-02 18:17:42 +00:00
|
|
|
|
|
|
|
async function processCampaign(campaignId) {
|
|
|
|
const campaignSubscribersTable = 'campaign__' + campaignId;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-02 12:59:02 +00:00
|
|
|
async function spawnWorker(workerId) {
|
|
|
|
return await new Promise((resolve, reject) => {
|
2018-09-02 18:17:42 +00:00
|
|
|
log.verbose('Senders', `Spawning worker process ${workerId}`);
|
2018-09-02 12:59:02 +00:00
|
|
|
|
|
|
|
const senderProcess = fork(path.join(__dirname, 'sender-worker.js'), [workerId], {
|
|
|
|
cwd: path.join(__dirname, '..'),
|
|
|
|
env: {NODE_ENV: process.env.NODE_ENV}
|
|
|
|
});
|
|
|
|
|
|
|
|
senderProcess.on('message', msg => {
|
|
|
|
if (msg) {
|
|
|
|
if (msg.type === 'worker-started') {
|
|
|
|
log.info('Senders', `Worker process ${workerId} started`);
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
senderProcess.on('close', (code, signal) => {
|
|
|
|
log.error('Senders', `Worker process ${workerId} exited with code %s signal %s`, code, signal);
|
|
|
|
});
|
|
|
|
|
|
|
|
workerProcesses.set(workerId, senderProcess);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function run() {
|
|
|
|
if (running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
running = true;
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendToWorker(workerId, msgType, data) {
|
|
|
|
workerProcesses.get(workerId).send({
|
|
|
|
type: msgType,
|
|
|
|
data,
|
|
|
|
tid: messageTid
|
|
|
|
});
|
|
|
|
|
|
|
|
messageTid++;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function init() {
|
|
|
|
const spawnWorkerFutures = [];
|
|
|
|
let workerId;
|
2018-09-02 18:17:42 +00:00
|
|
|
for (workerId = 0; workerId < config.queue.processes; workerId++) {
|
2018-09-02 12:59:02 +00:00
|
|
|
spawnWorkerFutures.push(spawnWorker(workerId));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(spawnWorkerFutures);
|
|
|
|
|
|
|
|
process.on('message', msg => {
|
|
|
|
if (msg) {
|
|
|
|
const type = msg.type;
|
|
|
|
|
|
|
|
if (type === 'scheduleCheck') {
|
|
|
|
// FIXME
|
|
|
|
|
|
|
|
} else if (type === 'reloadConfig') {
|
|
|
|
for (const worker of workerProcesses.keys()) {
|
|
|
|
sendToWorker(workerId, 'reloadConfig', msg.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
process.send({
|
|
|
|
type: 'sender-started'
|
|
|
|
});
|
|
|
|
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
|
|
|
init();
|
|
|
|
|