Skip to content

Commit ab5c1af

Browse files
committed
[doc dist api] Version bump. Added docs and a javascript wrapper to the add-on
1 parent 57f2e0c commit ab5c1af

File tree

11 files changed

+292
-48
lines changed

11 files changed

+292
-48
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.lock-wscript
2+
build/
3+
build/*

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2009 Arthur (Slashed), Pedro Teixeira, James Halliday, Zak Taylor, Charlie Robbins
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README

Lines changed: 0 additions & 6 deletions
This file was deleted.

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# daemon.node
2+
3+
A C++ add-on for Node.js to enable simple daemons in Javascript plus some useful wrappers in Javascript.
4+
5+
## Installation
6+
7+
### Installing npm (node package manager)
8+
<pre>
9+
curl http://npmjs.org/install.sh | sh
10+
</pre>
11+
12+
### Installing daemon.node with npm
13+
<pre>
14+
[sudo] npm install daemon
15+
</pre>
16+
17+
### Installing daemon.node locally
18+
<pre>
19+
node-waf configure build
20+
</pre>
21+
22+
## Usage
23+
24+
There is a great getting started article on daemons and node.js by Slashed that you [can read here][0]. The API has changed slightly from that version thanks to contributions from ptge and [fugue][1]; there is no longer a daemon.closeIO() method, this is done automatically for you.
25+
26+
### Starting a daemon:
27+
Starting a daemon is easy, just call daemon.start() and daemon.lock().
28+
<pre>
29+
var daemon = require('daemon');
30+
31+
// Your awesome code here
32+
33+
fs.open('somefile.log', 'w+', function (err, fd) {
34+
daemon.start();
35+
daemon.lock('/tmp/yourprogram.pid');
36+
});
37+
</pre>
38+
39+
This library also exposes a higher level facility through javascript for starting daemons:
40+
<pre>
41+
var sys = require('sys'),
42+
daemon = require('daemon');
43+
44+
// Your awesome code here
45+
46+
daemon.run('somefile.log', '/tmp/yourprogram.pid', function (err, started) {
47+
// We are now in the daemon process
48+
if (err) return sys.puts('Error starting daemon: ' + err);
49+
50+
sys.puts('Daemon started successfully');
51+
});
52+
</pre>
53+
54+
### The Fine Print
55+
This library is available under the MIT LICENSE. See the LICENSE file for more details. It was created by [Slashed][2] and [forked][3] / [improved][4] / [hacked upon][1] by a lot of good people. Special thanks to [Isaacs][5] for npm and a great example in [glob][6].
56+
57+
#### Author: [Slashed](http://github.com/slashed)
58+
#### Contributors: [Charlie Robbins](http://nodejitsu.com), [Pedro Teixeira](https://github.com/pgte), [James Halliday](https://github.com/substack), [Zak Taylor](https://github.com/dobl)
59+
60+
[0]: http://slashed.posterous.com/writing-daemons-in-javascript-with-nodejs-0
61+
[1]: https://github.com/pgte/fugue/blob/master/deps/daemon.cc
62+
[2]: https://github.com/slashed/daemon.node
63+
[3]: https://github.com/substack/daemon.node/
64+
[4]: https://github.com/dobl/daemon.node
65+
[5]: https://github.com/isaacs/npm
66+
[6]:

example.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

example/bindings.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* bindings.js: Example for running daemons directly using methods exposed by add-on bindings.
3+
*
4+
* (C) 2010, Charlie Robbins.
5+
*
6+
*/
7+
8+
var sys = require('sys'),
9+
fs = require('fs'),
10+
http = require('http');
11+
12+
var daemon;
13+
try {
14+
daemon = require('../lib/daemon');
15+
}
16+
catch (ex) {
17+
sys.puts("Couldn't find 'daemon' add-on, did you install it yet?");
18+
process.exit(0);
19+
}
20+
21+
var config = {
22+
lockFile: '/tmp/testd.pid', // Location of lockFile
23+
logFile: '/tmp/testd.log' // Location of logFile
24+
};
25+
26+
var args = process.argv;
27+
28+
// Handle start stop commands
29+
switch(args[2]) {
30+
case "stop":
31+
process.kill(parseInt(fs.readFileSync(config.lockFile)));
32+
process.exit(0);
33+
break;
34+
35+
case "start":
36+
fs.open(config.logFile, 'w+', function (err, fd) {
37+
if (err) return sys.puts('Error starting daemon: ' + err);
38+
39+
daemon.start(fd);
40+
daemon.lock(config.lockFile);
41+
});
42+
break;
43+
44+
default:
45+
sys.puts('Usage: [start|stop]');
46+
process.exit(0);
47+
}
48+
49+
// Start HTTP Server
50+
http.createServer(function(req, res) {
51+
res.writeHead(200, { 'Content-Type': 'text/html' });
52+
res.write('<h1>Hello, World!</h1>');
53+
res.end();
54+
}).listen(8000);

example/wrapper.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* wrapper.js: Example for running daemons using friendly wrapper methods exposed in Javascript.
3+
*
4+
* (C) 2010, Charlie Robbins.
5+
*
6+
*/
7+
8+
var sys = require('sys'),
9+
fs = require('fs'),
10+
http = require('http');
11+
12+
var daemon;
13+
try {
14+
daemon = require('../lib/daemon');
15+
}
16+
catch (ex) {
17+
sys.puts("Couldn't find 'daemon' add-on, did you install it yet?");
18+
process.exit(0);
19+
}
20+
21+
var config = {
22+
lockFile: '/tmp/testd.pid', // Location of lockFile
23+
logFile: '/tmp/testd.log' // Location of logFile
24+
};
25+
26+
var args = process.argv;
27+
28+
// Handle start stop commands
29+
switch(args[2]) {
30+
case "stop":
31+
daemon.stop(config.lockFile, function (err, pid) {
32+
if (err) return sys.puts('Error stopping daemon: ' + err);
33+
sys.puts('Successfully stopped daemon with pid: ' + pid);
34+
});
35+
break;
36+
37+
case "start":
38+
// Start HTTP Server
39+
http.createServer(function(req, res) {
40+
// sys.puts('Incoming request for: ' + req.url);
41+
res.writeHead(200, { 'Content-Type': 'text/html' });
42+
res.write('<h1>Hello, World!</h1>');
43+
res.end();
44+
}).listen(8000);
45+
46+
daemon.run(config.logFile, config.lockFile, function (err, started) {
47+
if (err) return sys.puts('Error starting daemon: ' + err);
48+
sys.puts('Successfully started daemon');
49+
});
50+
break;
51+
52+
default:
53+
sys.puts('Usage: [start|stop]');
54+
break;
55+
}
56+

lib/daemon.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* daemon.js: Wrapper for C++ bindings
3+
*
4+
* (C) 2010 and Charlie Robbins
5+
* MIT LICENCE
6+
*
7+
*/
8+
9+
var fs = require('fs'),
10+
binding = require('../build/default/daemon'),
11+
daemon = exports;
12+
13+
//
14+
// Export the raw bindings directly
15+
//
16+
Object.keys(binding).forEach(function (k) { daemon[k] = binding[k] });
17+
18+
//
19+
// function run (out, lock, callback)
20+
// Run is designed to encapsulate the basic daemon operation in a single async call.
21+
// When the callback returns you are in the the child process.
22+
//
23+
daemon.run = function (out, lock, callback) {
24+
fs.open(out, 'w+', function (err, fd) {
25+
if (err) return callback(err);
26+
27+
try {
28+
daemon.start(fd);
29+
daemon.lock(lock);
30+
callback(null, true);
31+
}
32+
catch (ex) {
33+
callback(ex);
34+
}
35+
});
36+
};
37+
38+
//
39+
// function lock (lock, callback)
40+
// Asynchronously stop the process in the lock file and
41+
// remove the lock file
42+
//
43+
daemon.stop = function (lock, callback) {
44+
fs.readFile(lock, function (err, data) {
45+
if (err) return callback(err);
46+
47+
try {
48+
// Stop the process with the pid in the lock file
49+
var pid = parseInt(data.toString());
50+
process.kill(pid);
51+
52+
// Remove the lock file
53+
fs.unlink(lock, function (err) {
54+
if (err) return callback(err);
55+
callback(null, pid);
56+
});
57+
}
58+
catch (ex) {
59+
callback(ex);
60+
}
61+
});
62+
};

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name" : "daemon",
3+
"version" : "0.1.0",
4+
"description" : "Add-on for creating *nix daemons",
5+
"author": "Arthur (Slashed) <arthur@norgic.com>",
6+
"contributors": [
7+
{ "name": "Pedro Teixeira", "email": "pedro.teixeira@gmail.com" },
8+
{ "name": "Charlie Robbins", "email": "charlie.robbins@gmail.com" },
9+
{ "name": "James Halliday", "email": "mail@substack.net" },
10+
{ "name": "Zak Taylor", "email": "zak@dobl.com" }
11+
],
12+
"repository" : {
13+
"type" : "git",
14+
"url" : "http://github.com/indexzero/daemon.node.git"
15+
},
16+
"main": "./lib/daemon",
17+
"scripts" : {
18+
"preinstall" : "node-waf configure build"
19+
},
20+
"engines" : {
21+
"node" : ">= 0.1.97"
22+
}
23+
}

daemon.cc renamed to src/daemon.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* Daemon.node
3-
*** A node.JS addon that allows creating Unix/Linux Daemons in pure Javascript.
4-
*** Copyright 2010 (c) <arthur@norgic.com>
2+
* Daemon.node: A node.JS addon that allows creating Unix/Linux Daemons in pure Javascript.
3+
*
4+
* Copyright 2010 (c) <arthur@norgic.com>
55
* Under MIT License. See LICENSE file.
66
*/
77

0 commit comments

Comments
 (0)