From 226e157f690fa79d97faaed7897b684d93ae90ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Vest=C3=B8l?= Date: Wed, 6 Jun 2018 12:54:11 +0200 Subject: [PATCH] Added onExit handler to masterPlugins (see description) To use the onExit handler, simply add a method called onExit to your exported class. If it returns a promise, the master will wait for it to resolve. --- master.js | 31 +++++++++++++++++++++++-------- package.json | 4 ++-- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/master.js b/master.js index c6756f6d..6037a450 100644 --- a/master.js +++ b/master.js @@ -242,8 +242,9 @@ db.items.removeItem = function(object) { } // store slaves and inventory in a .json full of JSON data -process.on('SIGINT', function () { +process.on('SIGINT', async function () { console.log('Ctrl-C...'); + let exitStartTime = Date.now(); // set insane limit to slave length, if its longer than this we are probably being ddosed or something if(slaves && Object.keys(slaves).length < 2000000){ console.log("saving to slaves.json"); @@ -257,6 +258,15 @@ process.on('SIGINT', function () { } else if(slaves) { console.log("Item database too large, not saving ("+Object.keys(slaves).length+")"); } + for(let i in masterPlugins){ + let plugin = masterPlugins[i]; + if(plugin.main && plugin.main.onExit && typeof plugin.main.onExit == "function"){ + let startTime = Date.now(); + await plugin.main.onExit(); + console.log("Plugin "+plugin.pluginConfig.name+" exited in "+(Date.now()-startTime)+"ms"); + } + } + console.log("Exited in "+(Date.now()-exitStartTime)+"ms"); process.exit(2); }); @@ -1083,11 +1093,13 @@ io.on('connection', function (socket) { // handle plugins on the master pluginManagement(); +masterPlugins = []; async function pluginManagement(){ let startPluginLoad = Date.now(); - let plugins = await getPlugins("sharedPlugins"); + masterPlugins = await getPlugins("sharedPlugins"); console.log("All plugins loaded in "+(Date.now() - startPluginLoad)+"ms"); } + function getPlugins(pluginDirectory){ return new Promise((resolve, reject) => { let filesLeft = 0; @@ -1115,13 +1127,16 @@ function getPlugins(pluginDirectory){ --filesLeft; if(pluginConfig.masterPlugin){ let masterPlugin = require(path.resolve(pluginPath, pluginConfig.masterPlugin)); - plugins.push(new masterPlugin({ - config, + plugins.push({ + main:new masterPlugin({ + config, + pluginConfig, + path: pluginPath, + socketio: io, + express: app, + }), pluginConfig, - path: pluginPath, - socketio: io, - express: app, - })); + }); console.log("Loaded plugin "+pluginConfig.name+" in "+(Date.now() - pluginStartedLoading)+"ms"); checkLoadComplete(); diff --git a/package.json b/package.json index 98fdc942..f40f7cb2 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,8 @@ "ejs": "^2.5.7", "express": "^4.16.2", "express-fileupload": "0.0.6", - "follow-redirects": "^1.4.1", - "fs-extra": "^1.0.0", + "follow-redirects": "^1.5.0", + "fs-extra": "^6.0.1", "getmac": "^1.4.0", "hash-files": "^1.1.1", "html5-validator": "^1.0.7",