Skip to content
Browse files

First commit.

  • Loading branch information...
0 parents commit 97726a156ee9dc67e529accd89005acd7ab4a4ee @vesln committed
Showing with 520 additions and 0 deletions.
  1. +13 −0 .gitignore
  2. +9 −0 Makefile
  3. +105 −0 Readme.md
  4. +18 −0 examples/index.js
  5. +22 −0 lib/moni.js
  6. +170 −0 lib/watcher.js
  7. +26 −0 package.json
  8. +23 −0 test/moni.test.js
  9. +8 −0 test/support/spawn.js
  10. +126 −0 test/watcher.test.js
13 .gitignore
@@ -0,0 +1,13 @@
+node_modules/
+*._
+*.tmp
+.monitor
+*.diff
+*.err
+*.orig
+*.rej
+*.swo
+*.swp
+*.vi
+*~
+.DS_Store
9 Makefile
@@ -0,0 +1,9 @@
+TESTS = $(shell find test/ -iname \*.test.js)
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --require should \
+ --reporter spec \
+ $(TESTS)
+
+.PHONY: test
105 Readme.md
@@ -0,0 +1,105 @@
+[![Build Status](https://secure.travis-ci.org/vesln/moni.png)](http://travis-ci.org/vesln/moni)
+
+# word - Text this.
+
+![screenshot](http://img254.imageshack.us/img254/3719/monilogo.png)
+
+
+http://github.com/vesln/moni
+
+## Description
+
+Process monitoring with node.
+
+## Synopsis
+
+### Pid:
+
+```js
+
+var Moni = require('moni');
+var watcher = new Moni(44444);
+
+watcher.on('end', function() {
+ console.log('dead');
+});
+
+watcher.watch(1000);
+
+```
+
+### Group of pids:
+
+```js
+
+var Moni = require('moni');
+var watcher = new Moni([44444, 33333, 12345]);
+
+watcher.on('end', function() {
+ console.log('dead');
+});
+
+watcher.watch(1000);
+
+```
+
+### Command:
+
+```js
+
+var Moni = require('moni');
+var watcher = new Moni('redis-server');
+
+watcher.on('start', function() {
+ console.log('up');
+});
+
+watcher.on('end', function() {
+ console.log('dead');
+});
+
+watcher.watch(1000);
+
+```
+
+## Requirements
+
+- NPM (http://npmjs.org/)
+- Node.js 0.6 (http://nodejs.org/)
+
+## Install
+
+```
+$ npm install moni
+```
+
+## Tests
+
+```
+$ npm install
+$ make test
+```
+
+## License
+
+MIT License
+
+Copyright (C) 2012 Veselin Todorov
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
18 examples/index.js
@@ -0,0 +1,18 @@
+/*!
+ * moni - Process monitoring with node.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+var Watcher = require('../');
+var watcher = new Watcher('redis-server');
+
+watcher.on('start', function() {
+ console.log('redis is up.');
+});
+
+watcher.on('end', function() {
+ console.log('redis is down.');
+});
+
+watcher.watch(100);
22 lib/moni.js
@@ -0,0 +1,22 @@
+/*!
+ * moni - Process monitoring with node.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var package = require('package')(module);
+var Watcher = require('./watcher');
+
+/**
+ * Exposing `Watcher`.
+ */
+module.exports = Watcher;
+
+/**
+ * Exposing version.
+ */
+module.exports.version = package.version;
170 lib/watcher.js
@@ -0,0 +1,170 @@
+/*!
+ * moni - Process monitoring with node.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var events = require('events');
+var exec = require('child_process').exec;
+
+/**
+ * toString alias.
+ *
+ * @type {Function}
+ */
+var toString = Object.prototype.toString;
+
+/**
+ * Watcher constructor.
+ *
+ * @param {String|Number|Array} pid or process name.
+ */
+function Watcher(pids) {
+ this._processes = {};
+ this.pids(pids);
+};
+
+/**
+ * Extends EventEmitter.
+ */
+Watcher.prototype.__proto__ = events.EventEmitter.prototype;
+
+/**
+ * Parses pids.
+ *
+ * @param {String|Number|Array} pid or process name.
+ * @param {Function} callback.
+ */
+Watcher.prototype.parse = function(pids, cb) {
+ var self = this;
+
+ switch(true) {
+ case Array.isArray(pids):
+ cb.call(this, this.pids(pids));
+ break;
+
+ case toString.call(pids) === '[object Number]':
+ cb.call(this, this.pids([pids]));
+ break;
+
+ default:
+ this.pidof(pids, function(pid) {
+ cb.call(self, self.pids(pid));
+ });
+ }
+};
+
+/**
+ * Pids accessor.
+ *
+ * @param {Array} pids
+ * @returns {Mixed}
+ */
+Watcher.prototype.pids = function(pids) {
+ if (arguments.length === 1) {
+ if (!Array.isArray(pids)) pids = [pids];
+ this._pids = pids;
+ }
+ return this._pids;
+};
+
+/**
+ * Interval accessor.
+ *
+ * @param {Array} pids
+ * @returns {Mixed}
+ */
+Watcher.prototype.interval = function(interval) {
+ if (arguments.length === 1) {
+ this._interval = interval;
+ }
+ return this._interval;
+};
+
+/**
+ * Find pidof process.
+ *
+ * @param {String} process
+ * @param {Function} cb
+ */
+Watcher.prototype.pidof = function(str, cb) {
+ var pids = null;
+ var pid = null;
+ exec('pidof ' + str, function (error, stdout, stderr) {
+ pids = stdout.split(/\s+/);
+ pids.forEach(function(pid, i) {
+ if (!pid) pids.splice(i, 1);
+ });
+ if (pids[0]) pid = pids[0];
+ cb(pid);
+ });
+};
+
+/**
+ * Starts to monitor processes.
+ *
+ * @param {Number} interval.
+ */
+Watcher.prototype.watch = function(interval) {
+ var self = this;
+ this.interval(interval);
+
+ this.pids().forEach(function(pid) {
+ self.check(pid, function(live) {
+ self._processes[pid] = live;
+ self.start(pid, interval);
+ });
+ });
+};
+
+/**
+ * Initializes the continious monitoring.
+ *
+ * @parm {Number} pid
+ * @param {Number} interval
+ */
+Watcher.prototype.start = function(pid, interval) {
+ var self = this;
+ setInterval(function() {
+ self.check(pid, function(live) {
+ if (live !== self._processes[pid]) {
+ self._processes[pid] = live;
+ self.notify(pid, live);
+ }
+ });
+ }, interval);
+};
+
+/**
+ * Emits a start/end event.
+ *
+ * @parm {Number} pid
+ * @param {Boolean} state
+ */
+Watcher.prototype.notify = function(pid, state) {
+ var name = (state) ? 'start' : 'end';
+ this.emit(name, pid);
+};
+
+/**
+ * Checks if process is live.
+ *
+ * @param {Number} pid
+ * @param {Function} cb
+ */
+Watcher.prototype.check = function(pid, cb) {
+ this.parse(pid, function(p) {
+ exec('kill -0 ' + p, function(error, stdout, stderr) {
+ cb(!(stderr));
+ });
+ });
+};
+
+/**
+ * Exposing `Watcher`.
+ */
+module.exports = Watcher;
26 package.json
@@ -0,0 +1,26 @@
+{
+ "name": "moni"
+ , "version": "0.0.1"
+ , "description": "Process monitoring with node."
+ , "keywords": ["monitoring", "process monitoring"]
+ , "author": "Veselin Todorov <hi@vesln.com>"
+ , "dependencies": {
+ "package": "1.0.0"
+ }
+ , "devDependencies": {
+ "mocha": "0.3.3"
+ , "should": "0.3.2"
+ }
+ , "repository" : {
+ "type" : "git"
+ , "url" : "http://github.com/vesln/moni.git"
+ }
+ , "homepage": "http://github.com/vesln/moni"
+ , "scripts": {
+ "test": "make test"
+ }
+ , "main": "./lib/moni"
+ , "engines": {
+ "node": ">= 0.6.0 < 0.7.0"
+ }
+}
23 test/moni.test.js
@@ -0,0 +1,23 @@
+/*!
+ * moni - Process monitoring with node.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var moni = require('../');
+var exec = require('child_process').exec;
+var Watcher = require('../lib/watcher');
+
+describe('moni', function() {
+ it('should expose Watcher', function() {
+ moni.should.eql(Watcher);
+ });
+
+ it('should expose version', function() {
+ moni.version.should.be.ok;
+ });
+});
8 test/support/spawn.js
@@ -0,0 +1,8 @@
+/*!
+ * moni - Process monitoring with node.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+setInterval(function() {}, 5000);
126 test/watcher.test.js
@@ -0,0 +1,126 @@
+/*!
+ * moni - Process monitoring with node.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var Watcher = require('../lib/watcher');
+var spawn = require('child_process').spawn;
+var exec = require('child_process').exec;
+
+describe('Watcher', function() {
+ describe('constructor', function() {
+ it('should trigger the supplied cb', function() {
+ var watcher = new Watcher(3333);
+ watcher.pids().should.eql([3333]);
+ });
+ });
+
+ describe('parse', function() {
+ it('should convert number to array', function(done) {
+ var watcher = new Watcher;
+ watcher.parse(3333, function(pids) {
+ pids.should.eql([3333]);
+ done();
+ })
+ });
+
+ it('should convert keep the array', function(done) {
+ var watcher = new Watcher;
+ watcher.parse([3333], function(pids) {
+ pids.should.eql([3333]);
+ done();
+ })
+ });
+
+ it('should convert string to number', function(done) {
+ var watcher = new Watcher;
+ watcher.parse('node', function(pids) {
+ pids.should.be.ok;
+ done();
+ })
+ });
+ });
+
+ describe('pids', function() {
+ it('should be accessor', function() {
+ var watcher = new Watcher;
+ watcher.pids(3333).should.eql([3333]);
+ });
+ });
+
+ describe('interval', function() {
+ it('should be accessor', function() {
+ var watcher = new Watcher;
+ watcher.interval(3333).should.eql(3333);
+ });
+ });
+
+ describe('pidof', function() {
+ it('should return pidof process', function(done) {
+ var watcher = new Watcher;
+ watcher.pidof('node', function(pid) {
+ pid.should.be.ok;
+ done();
+ });
+ });
+ });
+
+ describe('notify', function() {
+ it('should emit events', function(done) {
+ var watcher = new Watcher;
+ watcher.pids(3333);
+ watcher.on('end', function(pid) {
+ pid.should.eql(3333);
+ done();
+ });
+ watcher.notify(3333, false);
+ });
+
+ it('should emit events', function(done) {
+ var watcher = new Watcher;
+ watcher.pids(3333);
+ watcher.on('start', function(pid) {
+ pid.should.eql(3333);
+ done();
+ });
+ watcher.notify(3333, true);
+ });
+ });
+
+ describe('check', function() {
+ it('should check if process is live', function(done) {
+ var watcher = new Watcher;
+ watcher.check(process.pid, function(live) {
+ live.should.be.ok;
+ done();
+ });
+ });
+
+ it('should check if process is live or not', function(done) {
+ var watcher = new Watcher;
+ watcher.check(312321321, function(live) {
+ live.should.not.ok;
+ done();
+ });
+ });
+ });
+
+ describe('watch, start', function() {
+ it('should notify if process is dead', function(done) {
+ var child = spawn('node', [__dirname + '/support/spawn']);
+ var watcher = new Watcher(child.pid);
+ watcher.on('end', function() {
+ done();
+ });
+ watcher.watch(5);
+ setTimeout(function() {
+ child.kill();
+ }, 20);
+ });
+ });
+});

0 comments on commit 97726a1

Please sign in to comment.
Something went wrong with that request. Please try again.