0.1.0 • Published 7 years ago
proctrap v0.1.0
proctrap
Node.js process exceeding CPU & memory trap
Installation
Via npm:
$ npm install --save proctrapAPI
Declaration
var ProcessTrap = require('proctrap');Constructor
new ProcessTrap(args)
var trap = new ProcessTrap(args);args is an object that includes:
args.duration- the time during which the CPU or memory usage exceeds limitation (default:5 * 60 * 1000ms ~ 5 minutes).args.interval- the break time between two checking actions (default5 * 1000ms ~ 5 seconds).args.cpuLimit- a limit value of CPU usage in percent (default:99~ 99%).args.memLimit- a limit value of memory usage in byte (default:undefined).args.autostart- a boolean value determines whethertrapwill be launched automatically or not (default:true).args.onExceeding- an exceeding limit handling function (default:null).args.logger- a logger object that containshas(level)andlog(level, ...)methods (default: a built-in simple logger).
This constructor will throw Error objects in the following cases:
- both
args.cpuLimitandargs.memLimitare not provided:Error('Criteria not found, one of CPU or Memory limit should be provided'). args.durationvalue is a negative number or not a number:Error('[duration] should be a positive integer or 0').args.intervalvalue is not a number or not a positive number:Error('[interval] should be a positive integer').
Properties
trap.enable- a property is used to enable/disable the trap (default:true).
Methods
trap.start()- starts theintervalchecking task. Theconstructorcalls this method automatically by default (excepts the case ofautostartisfalse).trap.stop()- stops theintervalchecking task and resets the state.
Example
var proctrap = require('proctrap');
// ...
var trap = new proctrap({
cpuLimit: 90, // => 90%
memLimit: 100 * 1024 * 1024, // => 100 MB
duration: 2 * 60 * 1000, // => 2 minutes
interval: 3 * 1000, // 3 seconds
onExceeding: function() {
trap.stop();
process.exit(2);
}
});
// ...License
MIT