Permalink
Browse files

Improves logging to create new fs.WriteStream only on controller logP…

…ath change
  • Loading branch information...
tsmith committed Jul 17, 2011
1 parent 79690a5 commit 639edb54931c1eeb11c5b101ce6d9201c6be1070
Showing with 60 additions and 22 deletions.
  1. +4 −1 CHANGELOG
  2. +39 −14 example/controls.js
  3. +4 −4 lib/controller.js
  4. +13 −3 lib/log.js
View
@@ -1,3 +1,6 @@
+0.2.2
+- Improve logging to create new fs.WriteStream only on controller logPath change
+
0.2.1
- Add remote process stdout/stderr custom listener support for ssh() and scp()
- Add ability to write to remote process stdin
@@ -28,7 +31,7 @@
0.1.6
- Document sshOptions and reimplement to allow setting in config or host object
- Remove undocumented host() method for setting up a single host from config
-- Add engines specfication to package.json for node >=0.1.99
+- Add engines specification to package.json for node >=0.1.99
0.1.5
- Remove errant console.log statement in ssh command
View
@@ -13,24 +13,24 @@
// 'remote' machine.
// Run like:
-// node mycontroller.js myhost test 0
-// node mycontroller.js myhost test 64
-// node mycontroller.js mycluster test 0
-// node mycontroller.js mycluster test 64
-// node mycontroller.js mycluster scp
-// node mycontroller.js mycluster clean
-// node mycontroller.js myhost listeners
-// node mycontroller.js myhost stdin
-// node mycontroller.js myclusterarray test 0
-// node mycontroller.js myclusterjson test 0
-// node mycontroller.js mymachine 127.0.0.1 test 0
+// node controls.js myhost test 0
+// node controls.js myhost test 64
+// node controls.js mycluster test 0
+// node controls.js mycluster test 64
+// node controls.js mycluster scp
+// node controls.js mycluster clean
+// node controls.js myhost listeners
+// node controls.js myhost stdin
+// node controls.js myclusterarray test 0
+// node controls.js myclusterjson test 0
+// node controls.js mymachine 127.0.0.1 test 0
var control = require('../'),
task = control.task,
script = process.argv[1],
scpTest = 'controlScpTest';
-task('mycluster', 'Prototype config for cluster of two or more', function () {
+task('mycluster', 'Prototype config for cluster of two', function () {
var controllers = [],
local, controller;
@@ -54,15 +54,15 @@ task('mycluster', 'Prototype config for cluster of two or more', function () {
return controllers;
});
-task('myclusterarray', 'Array config for cluster of two or more', function () {
+task('myclusterarray', 'Array config for cluster of two', function () {
var controller = Object.create(control.controller);
controller.user = process.env.USER;
controller.scpOptions = ['-v'];
return control.controllers(['localhost', '127.0.0.1'], controller);
});
-task('myclusterjson', 'JSON Config for my cluster of two or more', function () {
+task('myclusterjson', 'JSON Config for my cluster of two', function () {
// Demonstrates JSON configuration usage
var addresses = {
@@ -97,6 +97,23 @@ task('mymachine', 'Config for single host from command line', function (args) {
return configure([args.shift()]); // From command line arguments rewriting
});
+// note that many sshd configs default to a low number of allowed connections
+// run like: node controls.js many 5 test
+task('many', 'Config n controllers', function (args) {
+ var controllers = [], shared, controller, i, l = args.shift();
+
+ shared = Object.create(control.controller);
+ shared.user = process.env.USER;
+
+ for (i = 0; i < l; i += 1) {
+ controller = Object.create(shared);
+ controller.address = 'localhost';
+ controllers.push(controller);
+ }
+
+ return controllers;
+});
+
function doTest(controller, code, callback, exitCallback) {
code = code || 0;
controller.ssh('node ' + script + ' myhost arbexit ' + code,
@@ -205,4 +222,12 @@ task('ondate', 'Different logic paths based on date', function (controller) {
});
});
+task('logchange', 'Call date two times, changing log path before second call',
+ function (controller) {
+ controller.ssh('date', function () {
+ controller.logPath = 'alt.log';
+ controller.ssh('date -r 1');
+ });
+});
+
control.begin();
View
@@ -19,11 +19,11 @@ prototype.scpOptions = [];
// Support logging
function log(message, prefix) {
+ if (!this.logger) {
+ this.logger = new Log(this.address + ':', this.logPath, true);
+ }
- // TODO Modify Log module to support passing a filename with puts()
- // so logger only opens a new WriteStream if logPath changes.
- var logger = new Log(this.address + ':', this.logPath, true);
- logger.puts(message, prefix);
+ this.logger.puts(message, prefix, this.logPath);
}
prototype.logPath = 'control.log'; // Default
prototype.log = log;
View
@@ -7,8 +7,12 @@
var sys = require('sys'),
fs = require('fs');
+function createWriteStream(path) {
+ return fs.createWriteStream(path, { flags: 'a', mode: '0600' });
+}
+
// prefix: added to log prefix to appear on each line if message has new lines
-function puts(message, prefix) {
+function puts(message, prefix, path) {
var filestream = this.filestream,
logPrefix = this.prefix,
echo = this.echo,
@@ -19,8 +23,13 @@ function puts(message, prefix) {
logPrefix += prefix;
}
+ if (path && path !== this.path) {
+ this.path = path;
+ filestream = this.filestream = createWriteStream(path);
+ }
+
// Message may contain leading, interstitial, or trailing carriage
- // returns and new lines. Carriage returns, when outputted to the
+ // returns and new lines. Carriage returns, when output to the
// console (terminal) will act as a literal carriage return, going back to
// the beginning of the line and overwriting content laid down in a
// previous line, which makes the console (terminal) log look corrupted.
@@ -58,13 +67,14 @@ function Log(prefix, path, echo, timestamp) {
var filestream;
if (path) {
- filestream = fs.createWriteStream(path, { flags: 'a', mode: '0600' });
+ filestream = createWriteStream(path);
}
timestamp = timestamp || require('./timestamp');
return {
prefix: prefix,
+ path: path,
filestream: filestream,
echo: echo,
timestamp: timestamp,

0 comments on commit 639edb5

Please sign in to comment.