From 213ee02f09f6e3ceffb42550f6952f614611f2cb Mon Sep 17 00:00:00 2001 From: David Merfield Date: Wed, 10 Oct 2018 17:44:47 -0400 Subject: [PATCH] Refactored child process management --- app/build/index.js | 17 ++++--- app/sync/index.js | 20 +++----- app/sync/tests/index.js | 2 +- app/sync/update/set.js | 106 +++++++++++++++++----------------------- 4 files changed, 65 insertions(+), 80 deletions(-) diff --git a/app/build/index.js b/app/build/index.js index 45de310e462..c1bed5486ae 100644 --- a/app/build/index.js +++ b/app/build/index.js @@ -7,7 +7,9 @@ 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) { @@ -15,6 +17,11 @@ process.on("message", function(message) { }); }); +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. @@ -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); } @@ -105,4 +110,4 @@ function build(blog, path, options, callback) { }); } -module.exports = build; \ No newline at end of file +module.exports = build; diff --git a/app/sync/index.js b/app/sync/index.js index 5784e7da731..82937763609 100644 --- a/app/sync/index.js +++ b/app/sync/index.js @@ -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( @@ -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; diff --git a/app/sync/tests/index.js b/app/sync/tests/index.js index 2a39253b5c9..a2a169aab19 100644 --- a/app/sync/tests/index.js +++ b/app/sync/tests/index.js @@ -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; diff --git a/app/sync/update/set.js b/app/sync/update/set.js index a49457e90ee..c24977a72a8 100644 --- a/app/sync/update/set.js +++ b/app/sync/update/set.js @@ -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;