Skip to content

Commit

Permalink
Refactored child process management
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmerfield committed Oct 10, 2018
1 parent 6f91b66 commit 213ee02
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 80 deletions.
17 changes: 11 additions & 6 deletions app/build/index.js
Expand Up @@ -7,14 +7,21 @@ var Prepare = require("./prepare");
var Thumbnail = require("./thumbnail");
var DateStamp = require("./prepare/dateStamp");
var moment = require("moment");
var converters = require('./converters');
var converters = require("./converters");

var exitHook = require("async-exit-hook");

process.on("message", function(message) {
build(message.blog, message.path, message.options, function(err, entry) {
process.send({ err: err, entry: entry, id: message.id });
});
});

console.log("Started build:", process.pid);

exitHook(function() {
console.log("Shutting down build:", process.pid);
});

// This file cannot become a blog post because it is not
// a type that Blot can process properly.
Expand All @@ -28,12 +35,10 @@ function isWrongType(path) {
return isWrong;
}


function build(blog, path, options, callback) {

if (isWrongType(path)) {
var err = new Error('Path is wrong type to convert');
err.code = 'WRONGTYPE';
var err = new Error("Path is wrong type to convert");
err.code = "WRONGTYPE";
return callback(err);
}

Expand Down Expand Up @@ -105,4 +110,4 @@ function build(blog, path, options, callback) {
});
}

module.exports = build;
module.exports = build;
20 changes: 7 additions & 13 deletions app/sync/index.js
Expand Up @@ -19,14 +19,6 @@ var DEFAULT_TTL = 10 * 60 * 1000;
var locks = {};

exitHook(function(callback) {
unlock(callback);
});

exitHook.uncaughtExceptionHandler(function(err, callback) {
unlock(callback);
});

function unlock(callback) {
console.log("Unlocking all locks...");

async.eachOf(
Expand All @@ -35,12 +27,14 @@ function unlock(callback) {
console.log("Unlocking", blogID, "...");
lock.unlock(next);
},
function(){
console.log('Unlocked all locks...');
callback();
}
callback
);
}
});

// exitHook.uncaughtExceptionHandler(function(err, callback) {
// unlock(callback);
// });

function sync(blogID, options, callback) {
var redlock, resource, ttl, folder;

Expand Down
2 changes: 1 addition & 1 deletion app/sync/tests/index.js
Expand Up @@ -30,7 +30,7 @@ describe("sync", function() {
});
});

xit("will release a lock when the process dies due to an uncaught exception", function(testDone) {
it("will release a lock when the process dies due to an uncaught exception", function(testDone) {
var child = require("child_process").fork(__dirname + "/error", {silent: true});
var blog = this.blog;

Expand Down
106 changes: 46 additions & 60 deletions app/sync/update/set.js
Expand Up @@ -21,66 +21,52 @@ function isTemplate(path) {
return normalize(path).indexOf("/templates/") === 0;
}

// var child_process = require("child_process");
// var numCPUs = require("os").cpus().length;
// var uuid = require("uuid/v4");
// var exitHook = require("async-exit-hook");

// exitHook(function(callback) {
// killWorkers(callback);
// });

// exitHook.uncaughtExceptionHandler(function(err, callback) {
// killWorkers(callback);
// });

// function killWorkers(callback) {
// console.log("Unlocking all locks...");

// async.each(
// workers,
// function(worker, next) {
// console.log("Killing worker...");
// worker.kill();
// next();
// },
// function() {
// console.log("Killed all workers...");
// callback();
// }
// );
// }

// var workers = [];
// var worker;
// var jobs = {};

// function triggerCallback(message) {
// jobs[message.id].callback(message.err, message.entry);
// }

// for (var i = 0; i < numCPUs; i++) {
// worker = child_process.fork(__dirname + "/../../build");
// worker.on("message", triggerCallback);
// workers.push(worker);
// }

// function build(blog, path, options, callback) {
// var worker = workers[Math.floor(Math.random() * workers.length)];
// var id = uuid();

// jobs[id] = {
// blog: blog,
// id: id,
// path: path,
// options: options,
// callback: callback
// };

// worker.send({ blog: blog, path: path, id: id, options: options });
// }

var build = require('../../build');
var child_process = require("child_process");
var numCPUs = require("os").cpus().length;
var uuid = require("uuid/v4");
var exitHook = require("async-exit-hook");

var workers = [];
var worker;
var jobs = {};


exitHook(killWorkers);

function killWorkers() {
console.log("Unlocking all locks...");
workers.forEach(function(worker){
worker.kill();
});
}


function triggerCallback(message) {
jobs[message.id].callback(message.err, message.entry);
}

for (var i = 0; i < numCPUs; i++) {
worker = child_process.fork(__dirname + "/../../build");
worker.on("message", triggerCallback);
workers.push(worker);
}

function build(blog, path, options, callback) {
var worker = workers[Math.floor(Math.random() * workers.length)];
var id = uuid();

jobs[id] = {
blog: blog,
id: id,
path: path,
options: options,
callback: callback
};

worker.send({ blog: blog, path: path, id: id, options: options });
}

// var build = require('../../build');

module.exports = function(blog, path, options, callback) {
var queue;
Expand Down

0 comments on commit 213ee02

Please sign in to comment.