Re: [TW5] central control for Node.js consoles

121 views
Skip to first unread message
Message has been deleted

Eucaly J

unread,
Oct 4, 2014, 3:21:59 AM10/4/14
to tiddly...@googlegroups.com
sorry for the broken image, please see below instead
https://www.dropbox.com/s/xjhf88wmgpah5tc/2014-10-04_115002.png

Hi All:

With no experience on Node.js, I follow the official guide to start my TiddlyWiki Node.js instance.
I am doing that for one month since 5.0.16-beta.

While, working with more than one Node.js consoles sometimes makes me puzzle.
So I create an AutoHotKey script to help that (sorry, only for Windows).

Now I can have a central control as the screenshot below, to :
  • have a ListView with summarized information
    • port
    • editions
    • path
    • console up-time
  • choose to open different browser
  • auto popup "central control" for new consoles
  • open Node.js console from "central control"
  • copy path / open in Explorer
The detection is done by adding special title to Node.js console with Windows command line
(the first parameter of -- start "xxx" )

start "%tw-path% * 127.0.0.1:%tw-port% * %tw-msg%" cmd /C node tiddlywiki.js ^
   
%tw-path% ^
   
--verbose ^
   
--server %tw-port% $:/core/save/all text/plain text/html %1 %2^
   
|| exit 1

Now, I am wondering if some of you will be interested in this AutoHotKey script.
Or, could this be implemented by Node.js to benefit more community members.

So far, I have collected some possible resource as below. But still have no idea if I have the ability to do this in Node.js.

https://github.com/oOthkOo/supervizer    -- A NodeJS daemon process manager to spawn/start/stop node app
https://github.com/joyent/node/wiki/modules    -- keyword "restarting"
https://www.npmjs.org/search?q=monitor%20console
https://www.npmjs.org/search?q=restart%20console





Message has been deleted

Hans Wobbe

unread,
Oct 4, 2014, 10:00:36 AM10/4/14
to tiddly...@googlegroups.com
Thanks for sharing this idea.  It sparked several interesting thoughts for me.


On Saturday, October 4, 2014 3:21:59 AM UTC-4, Eucaly J wrote:
sorry for the broken image, please see below instead
https://www.dropbox.com/s/xjhf88wmgpah5tc/2014-10-04_115002.png

Hi All:

Jeremy Ruston

unread,
Oct 4, 2014, 1:46:03 PM10/4/14
to TiddlyWikiDev
Hi Eucaly

Good stuff, your console looks useful, and I'd encourage you to share your script so that others can try it out,

Best wishes

Jeremy



--
You received this message because you are subscribed to the Google Groups "TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywikide...@googlegroups.com.
To post to this group, send email to tiddly...@googlegroups.com.
Visit this group at http://groups.google.com/group/tiddlywikidev.
For more options, visit https://groups.google.com/d/optout.



--
Jeremy Ruston
mailto:jeremy...@gmail.com

Eucaly J

unread,
Oct 5, 2014, 7:26:05 AM10/5/14
to tiddly...@googlegroups.com, jeremy...@gmail.com
Hi Jeremy, and all:

I am not sure if the AutoHotKey stuff suits for others. While I am willing to make a public version upon requests.

Jeremy Ruston於 2014年10月5日星期日UTC+8上午1時46分03秒寫道:
Hi Eucaly

Good stuff, your console looks useful, and I'd encourage you to share your script so that others can try it out,

Best wishes

Jeremy

In addition, I don't think detecting by console title is a good approach.
So below is a different approach (still in proof-of-concept stage)

In short, tiddlywiki.js will save a file in some folder for "central control".
for example: save C:\Users\username\tiddlywiki\hook\pidxxxx
other instances will have different pid in separate file.

{"pid":6112,"argv":["C:\\Program Files\\nodejs\\node.exe","D:\\Dropbox\\0U\\Data2014\\TiddlyWiki\\node-tw5100\\tiddlywiki.js","editions\\develop","--verbose","--server","10001","$:/core/save/all","text/plain","text/html","EucalyJ"],"initTime":"2014-10-05T10:37:40.739Z"}

With this, the central control can make a list of available tiddlywiki on different ports.
And polling until pid not alive to delete the pidxxx file.

The next step for me, might be extending this "central control" and launch for different browser features on TiddlyDesktop

below is the new module and the modification on tiddlywiki.js
or, can we "overload" console.log to saving status to pidxxx file too ?

tiddlywiki.js
var $tw = require("./boot/boot.js").TiddlyWiki();

// Pass the command line arguments to the boot kernel
$tw
.boot.argv = Array.prototype.slice.call(process.argv,2);

// Save process info for central control
require("./hook.js").init();

// Boot the TW5 app
$tw
.boot.boot();

hook.js
(function(){

exports
.init = function() {
   
var spy = {},
        userHome
= process.env.USERPROFILE || process.env.HOME || process.env.HOMEPATH,
        fs
= require("fs"),
        path
= require("path"),
        fp
, fn;
    fp
= path.join(userHome, "tiddlywiki", "hook");
    mkdirSync_p
(fp);
    fn
= path.join(fp, "pid" + process.pid);
    spy
.pid = process.pid;
    spy
.argv = process.argv;
    spy
.initTime = new Date();
    fs
.writeFileSync(fn, JSON.stringify(spy));

// source: https://github.com/bpedro/node-fs/blob/master/lib/fs.js    
function mkdirSync_p(path, mode, position) {
var fs = require('fs');
var osSep = process.platform === 'win32' ? '\\' : '/';
var parts = require('path').normalize(path).split(osSep);
mode
= mode || process.umask();
position
= position || 0;
if (position >= parts.length) {
return true;
}
var directory = parts.slice(0, position + 1).join(osSep) || osSep;
try {
fs
.statSync(directory);
mkdirSync_p
(path, mode, position + 1);
} catch (e) {
try {
fs
.mkdirSync(directory, mode);
mkdirSync_p
(path, mode, position + 1);
} catch (e) {
if (e.code != 'EEXIST') {
throw e;
}
mkdirSync_p
(path, mode, position + 1);
}
}
}
   
};



})();









Jeremy Ruston

unread,
Oct 5, 2014, 7:34:23 AM10/5/14
to Eucaly J, TiddlyWikiDev
Hi Eucaly

In short, tiddlywiki.js will save a file in some folder for "central control".
for example: save C:\Users\username\tiddlywiki\hook\pidxxxx
other instances will have different pid in separate file.

Would it be possible to save the central control file from a startup module? That would make it possible for your extension to reside in a plugin.

Alternatively, perhaps you'd be better off creating a tiddlywiki-wrapper.js file for your scripts to invoke, and have it in turn invoke tiddlywiki.js
 

function mkdirSync_p(path, mode, position) {

If you were able to run as a startup module then you could use the core function `$tw.utils.createDirectory` instead.

Best wishes

Jeremy








Eucaly J

unread,
Oct 5, 2014, 8:54:15 AM10/5/14
to tiddly...@googlegroups.com, byja...@gmail.com, jeremy...@gmail.com
Hi Jeremy:

Your suggestion is very valuable.

I create two start up modules. One before "command", and another after "command".
(also untilizing $tw.utils.createDirectory)
While, afterCmd (code as below) is not called. Is this by design?

In addition, if assigned below, then the startup module is only for node.js ?

exports.platforms = ["node"];



Jeremy Ruston:

Would it be possible to save the central control file from a startup module? That would make it possible for your extension to reside in a plugin.


function mkdirSync_p(path, mode, position) {

If you were able to run as a startup module then you could use the core function `$tw.utils.createDirectory` instead.

Best wishes

Jeremy


/*\
title: $:/plugins/eucaly/cchook/startup/afterCmd.js
type: application/javascript
module-type: startup

save central control hook after "command" module

\*/

(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

// Export name and synchronous status
exports
.name = "afterCmd";
exports
.platforms = ["node"];
exports
.before = [];
exports
.after = ["command"];
exports
.synchronous = false;

exports
.startup = function(callback) {

   
var spy = {},
        userHome
= process.env.USERPROFILE || process.env.HOME || process.env.HOMEPATH,
        fs
= require("fs"),
        path
= require("path"),
        fp
, fn;
    fp
= path.join(userHome, "tiddlywiki", "hook");

    $tw
.utils.createDirectory(fp);
    fn
= path.join(fp, "pid" + process.pid + "OK");

    spy
.pid = process.pid;
    spy
.argv = process.argv;
    spy
.initTime = new Date();
    fs
.writeFileSync(fn, JSON.stringify(spy));

    callback
();
};

})();

 

Jeremy Ruston

unread,
Oct 5, 2014, 12:22:51 PM10/5/14
to Eucaly J, TiddlyWikiDev
Hi Eucaly

While, afterCmd (code as below) is not called. Is this by design?

The problem is that the startup module is called "commands", not "command":

exports.after = ["commands"];
 

In addition, if assigned below, then the startup module is only for node.js ?

exports.platforms = ["node"];


That's correct, yes.

Best wishes

Jeremy.

Eucaly J

unread,
Oct 5, 2014, 1:17:11 PM10/5/14
to tiddly...@googlegroups.com, jeremy...@gmail.com
Hi Jeremy:

It's working. (Trivial things always beat people ...)

Now, I can image that TiddlyDesktop will do most of the rest,
except the red item (to bring a running console to focus).
  • have a List with summarized information
    • port
    • editions
    • path
    • console up-time
  • choose to open different browser
  • auto popup "central control" for new consoles
  • open Node.js console from "central control"
  • copy path / open in Explorer



    Jeremy Ruston於 2014年10月6日星期一UTC+8上午12時22分51秒寫道:
    Reply all
    Reply to author
    Forward
    0 new messages