Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
make Kardia singleton, so that it is accessible from all files in an …
Browse files Browse the repository at this point in the history
…uniform way
  • Loading branch information
martintajur committed Jun 15, 2015
1 parent aa4abe2 commit 871fcc1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
36 changes: 35 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
"use strict";

module.exports = require('./lib/kardia');
var currentStatus = null,
cluster = require("cluster"),
Status = require(__dirname + "/lib/status.js");

// if running on a cluster worker, we assume the master already has Kardia started, thus we
// instantiate a wrapper instance right-away
if (!cluster.isMaster) {
currentStatus = new Status({});
module.exports = currentStatus;
} else {
var startHandler = function(config) {
if (currentStatus === null) {
currentStatus = new Status(config);

if (!currentStatus._instanceId) {
currentStatus._instanceId = Date.now()+'.'+Math.round(Math.random()*1000);
}

currentStatus.on("serverStopped", function() {
currentStatus = null;
module.exports = { start: startHandler };
});

if (cluster.isMaster) {
currentStatus.startServer();
}

module.exports = currentStatus;
}
return currentStatus;
}

// otherwise only expose the .start() command, which in turn returns the currentStatus.
module.exports.start = startHandler;
}
21 changes: 0 additions & 21 deletions lib/kardia.js

This file was deleted.

42 changes: 29 additions & 13 deletions lib/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ var http = require("http"),
os = require("os"),
cluster = require("cluster"),
url = require("url"),
Worker = require('./worker.js'),
Worker = require(__dirname + "/worker.js"),
formatTime = require(__dirname + "/formatTime.js"),
formatMemory = require(__dirname + "/formatMemory.js");

function Status(config) {
if (!config) {
if (cluster.isMaster && !config) {
this.log("error", "Kardia cannot start - configuration not supplied");
return false;
}
if (!config.name) {
if (cluster.isMaster && !config.name) {
this.log("error", "Kardia cannot start - service name must be supplied");
return false;
}

this.config = config;
config.debug = !!config.debug;

if (!config.port) {
if (cluster.isMaster && !config.port) {
this.log("info", "Kardia starting on default port (12900)");
config.port = 12900;
}
if (!config.host) {
if (cluster.isMaster && !config.host) {
this.log("info", "Kardia starting on default host (0.0.0.0)");
config.host = '0.0.0.0';
}
Expand All @@ -46,7 +46,7 @@ function Status(config) {

this.measureFallBehind();

if (config.healthcheck) {
if (cluster.isMaster && config.healthcheck) {
this.registerHealthcheck(config.healthcheck);
}

Expand Down Expand Up @@ -227,14 +227,20 @@ Status.prototype.getConsulHealthcheck = function(options) {
return consulParams;
};

Status.prototype._onClusterFork = function() {
this.addWorker(worker);
}

Status.prototype._onClusterExit = function() {
this.removeWorker(worker.process.pid);
}

Status.prototype.startServer = function() {
cluster.on("fork", (function(worker) {
this.addWorker(worker);
}).bind(this));
this.onClusterFork = this._onClusterFork.bind(this);
this.onClusterExit = this._onClusterExit.bind(this);

cluster.on("exit", (function(worker) {
this.removeWorker(worker.process.pid);
}).bind(this));
cluster.on("fork", this.onClusterFork);
cluster.on("exit", this.onClusterExit);

this.server = http.createServer((function (req, res) {
this.emit('serverRequest', { req: req });
Expand Down Expand Up @@ -318,7 +324,13 @@ Status.prototype.stopServer = function() {
if (!cluster.isMaster) {
return false;
}
this.server.close();

try {
this.server.close();
cluster.removeListener("fork", this.onClusterFork);
cluster.removeListener("exit", this.onClusterExit);
} catch (e) {}

this.emit("serverStopped");

return true;
Expand Down Expand Up @@ -406,4 +418,8 @@ Status.prototype.removeListener = function(eventName, handler) {
return true;
};

Status.prototype.start = function() {
// dummy wrapper for catching subsequent start calls made by the underlying service using Kardia.
}

module.exports = Status;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kardia",
"version": "0.4.2",
"version": "0.5.0",
"description": "A humane process status API module to expose any operational/internal indicators of any Node.js process for status aggregation and monitoring. JSON format over HTTP protocol.",
"main": "index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions test/tests/test_that_name_must_be_supplied.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var fail = require(__dirname + "/../lib/fail.js");
module.exports = function(next) {
var errStr = "Kardia cannot start - service name must be supplied";
console.log("Expecting "+errStr);

try {
var Kardia = require("../..");
Kardia.start({});
Expand Down

0 comments on commit 871fcc1

Please sign in to comment.