├── package.json ├── test.js ├── LICENCE.md ├── README.md └── index.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "supervisord-eventlistener", 3 | "version": "0.2.0", 4 | "description": "Listens for events from supervisord and emits them", 5 | "keywords": ["supervisord"], 6 | "author": "Sugendran Ganess", 7 | "contributors": ["Blake Miner "], 8 | "repository": "git://github.com/sugendran/node-supervisord-eventlistener", 9 | "main": "index.js", 10 | "engine": "node >= 0.4.1" 11 | } 12 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var listener = require("./index"); 2 | 3 | listener.on('event', function(eventName, headers, data){ 4 | process.stderr.write("Event: " + eventName + "\n"); 5 | for(a in headers) { 6 | process.stderr.write( a + ": " + headers[a] + "\n"); 7 | } 8 | if(data !== "") { 9 | for(a in data) { 10 | process.stderr.write( a + ": " + data[a] + "\n"); 11 | } 12 | } 13 | process.stderr.write("\n\n"); 14 | }); 15 | 16 | listener.listen(process.stdin, process.stdout); 17 | -------------------------------------------------------------------------------- /LICENCE.md: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2013 Sugendran Ganess 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-supervisord-eventlistener 2 | 3 | This library implements a Supervisord event listener for Node.js. 4 | 5 | ## Install 6 | 7 | `npm install supervisord-eventlistener` 8 | 9 | ## Usage 10 | 11 | **Sample Node.js Program** 12 | 13 | ```javascript 14 | var supervisor = require("supervisord-eventlistener"); 15 | supervisor.on("event", function(type, headers, data) { 16 | //Gets called for all events 17 | console.error("Event type:", type); 18 | console.error("Headers:", headers); 19 | console.error("Data:", data); 20 | }); 21 | supervisor.on("PROCESS_STATE_STOPPING", function(headers, data) { 22 | //Only called for PROCESS_STATE_STOPPING events 23 | console.error("Process state stopping"); 24 | console.error("Headers:", headers); 25 | console.error("Data:", data); 26 | }); 27 | supervisor.listen(process.stdin, process.stdout); 28 | ``` 29 | 30 | **Sample Supervisor Configuration** 31 | 32 | ``` 33 | [eventlistener:monitoring] 34 | command = /usr/bin/nodejs /home/user/your_node_program.js 35 | events = PROCESS_STATE,PROCESS_COMMUNICATION,SUPERVISOR_STATE_CHANGE 36 | ``` 37 | 38 | 39 | ### More Documentation 40 | 41 | List of event types: [click here](http://supervisord.org/events.html#event-types) 42 | 43 | See [http://supervisord.org/events.html](http://supervisord.org/events.html) for more documentation. 44 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* Documentation for Supervisord's event listener protocol can be found 2 | here: http://supervisord.org/events.html */ 3 | 4 | var EventEmitter = require('events').EventEmitter, 5 | util = require('util'); 6 | 7 | function Listener() { 8 | EventEmitter.call(this); 9 | } 10 | util.inherits(Listener, EventEmitter); 11 | 12 | /* Key-value pairs are delimited by a single space in a given line. 13 | The key and value in the key-value pair are separated by a colon `:` 14 | Example line: `process_name:foo group_name:bar pid:123` 15 | */ 16 | function splitData(raw) { 17 | var lines = raw.split( "\n" ); 18 | var line = lines[0]; 19 | var vals = {}; 20 | line.split(" ").forEach(function(kvp){ 21 | var data = kvp.split(":"); 22 | vals[data[0]] = data[1]; 23 | }); 24 | if ( lines.length > 1 ) { 25 | lines.splice( 0 , 1 ); 26 | vals.body = lines; 27 | } 28 | return vals; 29 | } 30 | 31 | /* Parses a header line and returns the length of the payload to follow. */ 32 | Listener.prototype.headersReceived = function(line) { 33 | this.headers = splitData(line); 34 | return parseInt(this.headers.len, 10); 35 | }; 36 | 37 | /* Emits "event" with the event name, headers, and payload. Then, we tell 38 | Supervisord that we are ready to receive more events. */ 39 | Listener.prototype.payloadReceived = function(payload, stdout) { 40 | if(this.headers && this.headers.eventname) { 41 | stdout.write("RESULT 2\nOK"); 42 | this.emit("event", this.headers.eventname, this.headers, payload); 43 | this.emit(this.headers.eventname, this.headers, payload); 44 | stdout.write("READY\n"); 45 | } 46 | }; 47 | 48 | /* Start listening for events on `stdin` */ 49 | Listener.prototype.listen = function(stdin, stdout) { 50 | //Set initial state 51 | var self = this, 52 | data = "", 53 | payloadSize; 54 | //If I'm not waiting for headers, I'm waiting for the payload 55 | self.waitingForHeaders = true; 56 | //Start reading from stdin 57 | stdin.resume(); 58 | stdin.setEncoding('utf8'); 59 | stdin.on('data', function(str) { 60 | data += str; 61 | //Parse headers 62 | if(self.waitingForHeaders === true && str.indexOf("\n") !== -1) { 63 | //We now have the headers 64 | var br = data.indexOf("\n"), 65 | headers = data.substring(0, br), 66 | payloadSize = self.headersReceived(headers); 67 | //ignore "\n" and put remainder back into `data` 68 | data = data.substr(br + 1); 69 | if(payloadSize == 0) { 70 | //No payload; go ahead and emit "event" 71 | self.payloadReceived(null, stdout); 72 | //self.waitingForHeaders = true; 73 | //wait for next header... 74 | } else { 75 | //We need to parse the payload 76 | self.waitingForHeaders = false; 77 | } 78 | } 79 | //Parse payload 80 | if(self.waitingForHeaders !== true && data.length >= payloadSize) { 81 | //We now have the payload 82 | var payload = data.substr(0, payloadSize); 83 | //put the remainder back into `data` 84 | data = data.substr(payloadSize); 85 | //Parse payload and emit "event" 86 | self.payloadReceived(splitData(payload), stdout); 87 | self.waitingForHeaders = true; 88 | } 89 | }); 90 | //Tell Supervisord that I'm ready to receive events 91 | stdout.write("READY\n"); 92 | }; 93 | 94 | module.exports = new Listener(); 95 | --------------------------------------------------------------------------------