mathieuturcotte / node-pid Goto Github PK
View Code? Open in Web Editor NEWManage a pid file in Node.js
License: MIT License
Manage a pid file in Node.js
License: MIT License
The PID file isn't deleted when I use ctrl+c to end node on Windows.
Please add license to package.json: https://docs.npmjs.com/cli/v9/configuring-npm/package-json#license
Would it be worth supporting cleanup on non-normal process exit, such that:
/** Removes the PID file on normal process exit. */
Pid.prototype.removeOnExit = function() {
// do something when app is closing
process.on('exit', this.remove.bind(this));
// catches ctrl+c event
process.on('SIGINT', this.remove.bind(this));
// catches uncaught exceptions
process.on('uncaughtException', this.remove.bind(this));
};
When doing development I find that the process file remains when I hit ctrl-c.
var Promise = require('bluebird');
var micro = require('micro');
var fs = require('fs');
var os = require('os');
var cluster = require('cluster');
global.config = require(process.env.ENGINE_PATH + '/configs.js')[process.env.ENGINE_ENVIRONMENT];
var logger = require(process.env.ENGINE_PATH + '/lib/logger.js');
var router = require(process.env.ENGINE_PATH + '/lib/router.js');
var routes = require(process.env.ENGINE_PATH + '/routes/index.js');
var spawnThreads = function() {
for (var i = 0; i < os.cpus().length; ++i) {
var worker = cluster.fork();
logger.log({level: 'info', message: `Spawned worker ${worker.id}...`});
}
};
var bindServers = function() {
var server = micro(router(routes));
server.listen(global.config.port, function() {
logger.log({level: 'info', message: 'Engine server listening...', port: global.config.port});
});
};
(async function() {
if (cluster.isMaster) {
process.on('exit', function() {
console.error('HERE');
fs.unlinkSync(process.env.ENGINE_PATH + '/engine.pid');
});
spawnThreads();
} else {
bindServers();
}
})();
The process.on('exit' never gets called from the master... do you have any advice on this?
Take a look at this article: http://www.guido-flohr.net/never-delete-your-pid-file/
Removing a pid file when exiting can cause race conditions because unlinking a file doesn't cause other file handles to be closed immediately. Leaving a file around and getting an exclusive write lock is better.
It might be worth considering if there is a way for this library to reduce the likelihood of race conditions around the PID file, and handle PIDs with an exclusive lock instead of file presence. Additionally, the OS can re-use a PID, so even if a PID is in use, it may belong to a completely different process. However, if a lock is detected on the PID file, then it is more likely that the PID in the file belongs to process that last wrote the PID.
index.js installs new SIGTERM
and SIGINT
handlers that may override your own custom work. Modules should not quietly install signal handlers in this fashion, especially ones that don't provide hooking and always exit with a zero (normal) status.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.