Skip to content
Browse files

[doc dist api] Version bump. Added docs and a javascript wrapper to t…

…he add-on
  • Loading branch information...
1 parent 57f2e0c commit ab5c1af2dbc59d42b3fbce5c02d83da1fbbce6db @indexzero indexzero committed Nov 14, 2010
Showing with 292 additions and 48 deletions.
  1. +3 −0 .gitignore
  2. +19 −0 LICENSE
  3. +0 −6 README
  4. +66 −0 README.md
  5. +0 −37 example.js
  6. +54 −0 example/bindings.js
  7. +56 −0 example/wrapper.js
  8. +62 −0 lib/daemon.js
  9. +23 −0 package.json
  10. +3 −3 { → src}/daemon.cc
  11. +6 −2 wscript
View
3 .gitignore
@@ -0,0 +1,3 @@
+.lock-wscript
+build/
+build/*
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2009 Arthur (Slashed), Pedro Teixeira, James Halliday, Zak Taylor, Charlie Robbins
+
+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:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+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.
View
6 README
@@ -1,6 +0,0 @@
-Daemon Addon for Node.js
-
-To build this module, type:
-> node-waf configure build
-
-For more examples, read here: http://slashed.posterous.com/writing-daemons-in-javascript-with-nodejs-0
View
66 README.md
@@ -0,0 +1,66 @@
+# daemon.node
+
+A C++ add-on for Node.js to enable simple daemons in Javascript plus some useful wrappers in Javascript.
+
+## Installation
+
+### Installing npm (node package manager)
+<pre>
+ curl http://npmjs.org/install.sh | sh
+</pre>
+
+### Installing daemon.node with npm
+<pre>
+ [sudo] npm install daemon
+</pre>
+
+### Installing daemon.node locally
+<pre>
+ node-waf configure build
+</pre>
+
+## Usage
+
+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.
+
+### Starting a daemon:
+Starting a daemon is easy, just call daemon.start() and daemon.lock().
+<pre>
+ var daemon = require('daemon');
+
+ // Your awesome code here
+
+ fs.open('somefile.log', 'w+', function (err, fd) {
+ daemon.start();
+ daemon.lock('/tmp/yourprogram.pid');
+ });
+</pre>
+
+This library also exposes a higher level facility through javascript for starting daemons:
+<pre>
+ var sys = require('sys'),
+ daemon = require('daemon');
+
+ // Your awesome code here
+
+ daemon.run('somefile.log', '/tmp/yourprogram.pid', function (err, started) {
+ // We are now in the daemon process
+ if (err) return sys.puts('Error starting daemon: ' + err);
+
+ sys.puts('Daemon started successfully');
+ });
+</pre>
+
+### The Fine Print
+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].
+
+#### Author: [Slashed](http://github.com/slashed)
+#### Contributors: [Charlie Robbins](http://nodejitsu.com), [Pedro Teixeira](https://github.com/pgte), [James Halliday](https://github.com/substack), [Zak Taylor](https://github.com/dobl)
+
+[0]: http://slashed.posterous.com/writing-daemons-in-javascript-with-nodejs-0
+[1]: https://github.com/pgte/fugue/blob/master/deps/daemon.cc
+[2]: https://github.com/slashed/daemon.node
+[3]: https://github.com/substack/daemon.node/
+[4]: https://github.com/dobl/daemon.node
+[5]: https://github.com/isaacs/npm
+[6]:
View
37 example.js
@@ -1,37 +0,0 @@
-var daemon = require('./daemon');
-var fs = require('fs');
-var http = require('http');
-var sys = require('sys');
-
-var config = {
- lockFile: '/tmp/testd.pid' //Location of lockFile
-};
-
-var args = process.argv;
-var dPID;
-
-// Handle start stop commands
-switch(args[2]) {
- case "stop":
- process.kill(parseInt(fs.readFileSync(config.lockFile)));
- process.exit(0);
- break;
-
- case "start":
- dPID = daemon.start();
- daemon.lock(config.lockFile);
- daemon.closeIO();
- break;
-
- default:
- sys.puts('Usage: [start|stop]');
- process.exit(0);
-}
-
-// Start HTTP Server
-http.createServer(function(req, res) {
- res.writeHead(200, {'Content-Type': 'text/html'});
- res.write('<h1>Hello, World!</h1>');
- res.close();
-}).listen(8000);
-
View
54 example/bindings.js
@@ -0,0 +1,54 @@
+/*
+ * bindings.js: Example for running daemons directly using methods exposed by add-on bindings.
+ *
+ * (C) 2010, Charlie Robbins.
+ *
+ */
+
+var sys = require('sys'),
+ fs = require('fs'),
+ http = require('http');
+
+var daemon;
+try {
+ daemon = require('../lib/daemon');
+}
+catch (ex) {
+ sys.puts("Couldn't find 'daemon' add-on, did you install it yet?");
+ process.exit(0);
+}
+
+var config = {
+ lockFile: '/tmp/testd.pid', // Location of lockFile
+ logFile: '/tmp/testd.log' // Location of logFile
+};
+
+var args = process.argv;
+
+// Handle start stop commands
+switch(args[2]) {
+ case "stop":
+ process.kill(parseInt(fs.readFileSync(config.lockFile)));
+ process.exit(0);
+ break;
+
+ case "start":
+ fs.open(config.logFile, 'w+', function (err, fd) {
+ if (err) return sys.puts('Error starting daemon: ' + err);
+
+ daemon.start(fd);
+ daemon.lock(config.lockFile);
+ });
+ break;
+
+ default:
+ sys.puts('Usage: [start|stop]');
+ process.exit(0);
+}
+
+// Start HTTP Server
+http.createServer(function(req, res) {
+ res.writeHead(200, { 'Content-Type': 'text/html' });
+ res.write('<h1>Hello, World!</h1>');
+ res.end();
+}).listen(8000);
View
56 example/wrapper.js
@@ -0,0 +1,56 @@
+/*
+ * wrapper.js: Example for running daemons using friendly wrapper methods exposed in Javascript.
+ *
+ * (C) 2010, Charlie Robbins.
+ *
+ */
+
+var sys = require('sys'),
+ fs = require('fs'),
+ http = require('http');
+
+var daemon;
+try {
+ daemon = require('../lib/daemon');
+}
+catch (ex) {
+ sys.puts("Couldn't find 'daemon' add-on, did you install it yet?");
+ process.exit(0);
+}
+
+var config = {
+ lockFile: '/tmp/testd.pid', // Location of lockFile
+ logFile: '/tmp/testd.log' // Location of logFile
+};
+
+var args = process.argv;
+
+// Handle start stop commands
+switch(args[2]) {
+ case "stop":
+ daemon.stop(config.lockFile, function (err, pid) {
+ if (err) return sys.puts('Error stopping daemon: ' + err);
+ sys.puts('Successfully stopped daemon with pid: ' + pid);
+ });
+ break;
+
+ case "start":
+ // Start HTTP Server
+ http.createServer(function(req, res) {
+ // sys.puts('Incoming request for: ' + req.url);
+ res.writeHead(200, { 'Content-Type': 'text/html' });
+ res.write('<h1>Hello, World!</h1>');
+ res.end();
+ }).listen(8000);
+
+ daemon.run(config.logFile, config.lockFile, function (err, started) {
+ if (err) return sys.puts('Error starting daemon: ' + err);
+ sys.puts('Successfully started daemon');
+ });
+ break;
+
+ default:
+ sys.puts('Usage: [start|stop]');
+ break;
+}
+
View
62 lib/daemon.js
@@ -0,0 +1,62 @@
+/*
+ * daemon.js: Wrapper for C++ bindings
+ *
+ * (C) 2010 and Charlie Robbins
+ * MIT LICENCE
+ *
+ */
+
+var fs = require('fs'),
+ binding = require('../build/default/daemon'),
+ daemon = exports;
+
+//
+// Export the raw bindings directly
+//
+Object.keys(binding).forEach(function (k) { daemon[k] = binding[k] });
+
+//
+// function run (out, lock, callback)
+// Run is designed to encapsulate the basic daemon operation in a single async call.
+// When the callback returns you are in the the child process.
+//
+daemon.run = function (out, lock, callback) {
+ fs.open(out, 'w+', function (err, fd) {
+ if (err) return callback(err);
+
+ try {
+ daemon.start(fd);
+ daemon.lock(lock);
+ callback(null, true);
+ }
+ catch (ex) {
+ callback(ex);
+ }
+ });
+};
+
+//
+// function lock (lock, callback)
+// Asynchronously stop the process in the lock file and
+// remove the lock file
+//
+daemon.stop = function (lock, callback) {
+ fs.readFile(lock, function (err, data) {
+ if (err) return callback(err);
+
+ try {
+ // Stop the process with the pid in the lock file
+ var pid = parseInt(data.toString());
+ process.kill(pid);
+
+ // Remove the lock file
+ fs.unlink(lock, function (err) {
+ if (err) return callback(err);
+ callback(null, pid);
+ });
+ }
+ catch (ex) {
+ callback(ex);
+ }
+ });
+};
View
23 package.json
@@ -0,0 +1,23 @@
+{
+ "name" : "daemon",
+ "version" : "0.1.0",
+ "description" : "Add-on for creating *nix daemons",
+ "author": "Arthur (Slashed) <arthur@norgic.com>",
+ "contributors": [
+ { "name": "Pedro Teixeira", "email": "pedro.teixeira@gmail.com" },
+ { "name": "Charlie Robbins", "email": "charlie.robbins@gmail.com" },
+ { "name": "James Halliday", "email": "mail@substack.net" },
+ { "name": "Zak Taylor", "email": "zak@dobl.com" }
+ ],
+ "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/indexzero/daemon.node.git"
+ },
+ "main": "./lib/daemon",
+ "scripts" : {
+ "preinstall" : "node-waf configure build"
+ },
+ "engines" : {
+ "node" : ">= 0.1.97"
+ }
+}
View
6 daemon.cc → src/daemon.cc
@@ -1,7 +1,7 @@
/*
-* Daemon.node
-*** A node.JS addon that allows creating Unix/Linux Daemons in pure Javascript.
-*** Copyright 2010 (c) <arthur@norgic.com>
+* Daemon.node: A node.JS addon that allows creating Unix/Linux Daemons in pure Javascript.
+ *
+* Copyright 2010 (c) <arthur@norgic.com>
* Under MIT License. See LICENSE file.
*/
View
8 wscript
@@ -1,6 +1,10 @@
+import Options
+from os import unlink, symlink
+from os.path import exists
+
srcdir = "."
blddir = "build"
-VERSION = "0.0.1"
+VERSION = "0.1.0"
def set_options(opt):
opt.tool_options("compiler_cxx")
@@ -12,4 +16,4 @@ def configure(conf):
def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon")
obj.target = "daemon"
- obj.source = "daemon.cc"
+ obj.source = "src/daemon.cc"

0 comments on commit ab5c1af

Please sign in to comment.
Something went wrong with that request. Please try again.