Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Based on jshint defaults: http://goo.gl/OpjUs

{
"esversion": 6,
// If the scan should stop on first error.
"passfail": false,
// Maximum errors before stopping.
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ Synchronously removes a pid file. Does not throw if the pid file is missing or
if the removal fails. Returns a boolean indicating whether the pid file removal
succeeded.

#### npid.isRunning(pid): bool

- pid: pid number

Make a test if the pid process is still running

#### npid.exists(path): bool

- path: pid file path

Synchronously evaluate is file already exists and pid process is still o execution

### Class Pid

Represents a handle to a pid file and expose an API to remove it either
Expand All @@ -73,7 +85,7 @@ removal succeeded.

#### pid.removeOnExit()

Removes the pid file on normal process exit.
Removes the pid file on normal process exit or if process got SIGTERM signal.

## License

Expand Down
136 changes: 95 additions & 41 deletions lib/pid.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,111 @@
* Licensed under the MIT license.
*/

var fs = require('fs');
const fs = require('node:fs');

/**
* Pid file handle which can be used to remove the pid file.
* @param path The pid file's path.
*/
function Pid(path) {
this.path_ = path;
}
const actions = {
/**
* Check if the PID process is still running
*
* @param {String|Number} pid
* @returns {Boolean}
*/
isRunning (pid) {
try {
return process.kill(pid, 0);
}
catch (e) {
return e.code === 'EPERM';
}
},
/**
* Check if PID file exists and is running
*
* @param {String} path
* @returns {Booelan}
*/
exists (path) {
let result = false;

/** Removes the PID file synchronously. Does not throw. */
Pid.prototype.remove = function() {
return module.exports.remove(this.path_);
};
try {
const st = fs.statSync(path);

/** Removes the PID file on normal process exit. */
Pid.prototype.removeOnExit = function() {
process.on('exit', this.remove.bind(this));
};
if (st.isFile()) {
const pid = fs.readFileSync(path, {encoding: 'utf-8'});

/**
* Creates a pid file synchronously.
* @param path Path to the pid file.
* @param force Whether and existing pid file should be overwritten.
*/
function create(path, force) {
var pid = new Buffer(process.pid + '\n');
var fd = fs.openSync(path, force ? 'w' : 'wx');
var offset = 0;
result = this.isRunning(pid);
}
} catch (err) {
result = true;
}

while (offset < pid.length) {
offset += fs.writeSync(fd, pid, offset, pid.length - offset);
}
return result;
},
/**
* Create a file PID
*
* @param {String} path
* @param {Booelan} force
*/
create (path, force) {
const pid = Buffer.from(`${process.pid}\n`);
const fd = fs.openSync(path, force ? 'w' : 'wx');
let offset = 0;

fs.closeSync(fd);
while (offset < pid.length) {
offset += fs.writeSync(fd, pid, offset, pid.length - offset);
}

return new Pid(path);
}
fs.closeSync(fd);

return new Pid(path);
},
/**
* Remove the pid file
*
* @param {String} path
* @return {Booelan}
*/
remove (path) {
try {
fs.unlinkSync(path);
return true;
} catch (err) {
return false;
}
},
/**
* Terminate process linked in pid file
*
* @param {String} path
* @param {String|Number} signal
*/
terminate (path, signal) {
const pid = fs.readFileSync(path, {encoding: 'utf-8'});

return process.kill(pid, signal);
}
};

/**
* Removes a pid file synchronously. Does not throw.
* Pid file handle which can be used to remove the pid file.
*
* @param path The pid file's path.
*/
function remove(path) {
try {
fs.unlinkSync(path);
return true;
} catch (err) {
return false;
}
function Pid (path) {
/** @var {String} path_ */
this.path_ = path;

/** Removes the PID file synchronously. Does not throw. */
this.remove = function () {
return actions.remove(this.path_);
};

/** Removes the PID file on normal process exit. */
this.removeOnExit = function() {
process.on('exit', this.remove.bind(this));
process.on('SIGTERM', this.remove.bind(this));
};
}

module.exports.create = create;
module.exports.remove = remove;
module.exports = actions;
Loading