mirror of
https://github.com/ION606/archivebot.git
synced 2026-05-14 22:16:56 +00:00
Initial commit
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
||||
.lock-wscript
|
||||
build/
|
||||
build/*
|
||||
*.swp
|
||||
*.node
|
||||
node_modules
|
||||
npm-debug.log
|
||||
fixtures/*
|
||||
fixtures/!.gitkeep
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.8"
|
||||
- "0.10"
|
||||
+19
@@ -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.
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
# daemon
|
||||
|
||||
[](http://travis-ci.org/indexzero/daemon.node)
|
||||
|
||||
Turn a node script into a daemon.
|
||||
|
||||
## install via npm
|
||||
|
||||
```
|
||||
npm install daemon
|
||||
```
|
||||
|
||||
Requires node >= 0.8
|
||||
|
||||
## examples
|
||||
|
||||
```javascript
|
||||
// this code is run twice
|
||||
// see implementation notes below
|
||||
console.log(process.pid);
|
||||
|
||||
// after this point, we are a daemon
|
||||
require('daemon')();
|
||||
|
||||
// different pid because we are now forked
|
||||
// original parent has exited
|
||||
console.log(process.pid);
|
||||
```
|
||||
|
||||
## api
|
||||
|
||||
### daemon()
|
||||
|
||||
Respawn the process (self) as a daemon. The parent process will exit at the point of this call.
|
||||
|
||||
### daemon.daemon(script, args, opt)
|
||||
|
||||
Spawn the `script` with given `args` array as a daemonized process. Return the `child` process object.
|
||||
|
||||
opt can optionally contain the following arguments:
|
||||
* stdout (file descriptor for stdout of the daemon)
|
||||
* stderr (file descriptor for stderr of the daemon)
|
||||
* env (environment for the daemon) (default: process.env)
|
||||
* cwd (current working directory for daemonized script) (default: process.cwd)
|
||||
|
||||
## implementation notes
|
||||
|
||||
Daemon actually re-spawns the current application and runs it again. The only difference between the original and the fork is that the original will not execute past the `daemon()` call whereas the fork will.
|
||||
|
||||
## node versions prior to 0.8
|
||||
|
||||
Using this module on older versions of node (or older versions of this module) are not recommended due to how node works internally and the issues it can cause for daemons.
|
||||
|
||||
## Contributors
|
||||
[Charlie Robbins](http://nodejitsu.com)
|
||||
[Pedro Teixeira](https://github.com/pgte)
|
||||
[James Halliday](https://github.com/substack)
|
||||
[Zak Taylor](https://github.com/dobl)
|
||||
[Daniel Bartlett](https://github.com/danbuk)
|
||||
[Charlie McConnell](https://github.com/AvianFlu)
|
||||
[Slashed](http://github.com/slashed)
|
||||
[Roman Shtylman](http://github.com/shtylman)
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
var cluster = require('cluster');
|
||||
var numCPUs = require('os').cpus().length;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
// Fork workers.
|
||||
for (var i = 0; i < numCPUs; ++i) {
|
||||
cluster.fork();
|
||||
}
|
||||
|
||||
cluster.on('exit', function(worker, code, signal) {
|
||||
console.log('worker ' + worker.process.pid + ' died');
|
||||
cluster.fork();
|
||||
});
|
||||
|
||||
// daemonize after setting up cluster
|
||||
return require('../')();
|
||||
}
|
||||
|
||||
var http = require('http');
|
||||
http.createServer(function(req, res) {
|
||||
res.writeHead(200);
|
||||
res.end('process: ' + process.pid);
|
||||
|
||||
// just a demo to cycle workers
|
||||
// DO NOT DO THIS IN PRODUCTION
|
||||
process.exit();
|
||||
}).listen(8000);
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
var child_process = require('child_process');
|
||||
|
||||
// daemonize ourselves
|
||||
module.exports = function(opt) {
|
||||
// we are a daemon, don't daemonize again
|
||||
if (process.env.__daemon) {
|
||||
return process.pid;
|
||||
}
|
||||
|
||||
var args = [].concat(process.argv);
|
||||
|
||||
// shift off node
|
||||
args.shift();
|
||||
|
||||
// our script name
|
||||
var script = args.shift();
|
||||
|
||||
opt = opt || {};
|
||||
var env = opt.env || process.env;
|
||||
|
||||
// the child process will have this set so we can identify it as being daemonized
|
||||
env.__daemon = true;
|
||||
|
||||
// start ourselves as a daemon
|
||||
module.exports.daemon(script, args, opt);
|
||||
|
||||
// parent is done
|
||||
return process.exit();
|
||||
};
|
||||
|
||||
// daemonizes the script and returns the child process object
|
||||
module.exports.daemon = function(script, args, opt) {
|
||||
|
||||
opt = opt || {};
|
||||
|
||||
var stdout = opt.stdout || 'ignore';
|
||||
var stderr = opt.stderr || 'ignore';
|
||||
|
||||
var env = opt.env || process.env;
|
||||
var cwd = opt.cwd || process.cwd;
|
||||
|
||||
var cp_opt = {
|
||||
stdio: ['ignore', stdout, stderr],
|
||||
env: env,
|
||||
cwd: cwd,
|
||||
detached: true
|
||||
};
|
||||
|
||||
// spawn the child using the same node process as ours
|
||||
var child = child_process.spawn(process.execPath, [script].concat(args), cp_opt);
|
||||
|
||||
// required so the parent can exit
|
||||
child.unref();
|
||||
|
||||
return child;
|
||||
};
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
{
|
||||
"name": "daemon",
|
||||
"version": "1.1.0",
|
||||
"description": "Add-on for creating *nix daemons",
|
||||
"author": "Roman Shtylman <shtylman@gmail.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"
|
||||
},
|
||||
{
|
||||
"name": "Daniel Bartlett",
|
||||
"email": "dan@f-box.org"
|
||||
},
|
||||
{
|
||||
"name": "Charlie McConnell",
|
||||
"email": "charlie@charlieistheman.com"
|
||||
},
|
||||
{
|
||||
"name": "Josh Holbrook",
|
||||
"email": "josh@nodejitsu.com"
|
||||
},
|
||||
{
|
||||
"name": "Arthur (Slashed)",
|
||||
"email": "arthur@norgic.com"
|
||||
}
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/indexzero/daemon.node.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha --ui qunit test/*.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "1.8.1",
|
||||
"after": "0.6.0"
|
||||
},
|
||||
"main": "./index.js",
|
||||
"engines": {
|
||||
"node": ">= 0.8.0"
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
var assert = require('assert');
|
||||
var http = require('http');
|
||||
var spawn = require('child_process').spawn;
|
||||
var after = require('after');
|
||||
|
||||
function launch(args) {
|
||||
var child = spawn(process.execPath, args);
|
||||
|
||||
child.stdout.pipe(process.stdout, {end: false});
|
||||
child.stderr.pipe(process.stderr, {end: false});
|
||||
|
||||
return child;
|
||||
};
|
||||
|
||||
// sanity check that a no daemon process exits
|
||||
test('no daemon', function(done) {
|
||||
var script = __dirname + '/fixtures/nodaemon.js';
|
||||
var child = launch([script]);
|
||||
child.on('exit', function(code) {
|
||||
assert.equal(code, 0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('simple', function(done) {
|
||||
var script = __dirname + '/fixtures/simple.js';
|
||||
|
||||
done = after(2, done);
|
||||
var port = 12345;
|
||||
|
||||
var child = launch([script, port]);
|
||||
|
||||
child.stdout.pipe(process.stdout, {end: false});
|
||||
child.stderr.pipe(process.stderr, {end: false});
|
||||
|
||||
// spawning child should exit
|
||||
child.on('exit', function(code) {
|
||||
assert.equal(code, 0);
|
||||
done();
|
||||
});
|
||||
|
||||
// wait for http server to start up
|
||||
setTimeout(function() {
|
||||
var opt = {
|
||||
host: 'localhost',
|
||||
port: port
|
||||
};
|
||||
|
||||
http.get(opt, function(res) {
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', function(chunk) {
|
||||
process.kill(chunk, 'SIGTERM');
|
||||
done();
|
||||
});
|
||||
});
|
||||
}, 500);
|
||||
});
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// will exit immediately
|
||||
var daemon = require('../../');
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
var http = require('http');
|
||||
var daemon = require('../../');
|
||||
|
||||
var port = process.argv[2];
|
||||
|
||||
daemon({
|
||||
stdout: process.stdout,
|
||||
stderr: process.stderr
|
||||
});
|
||||
|
||||
var server = http.createServer(function(req, res) {
|
||||
res.end('' + process.pid);
|
||||
});
|
||||
|
||||
server.listen(port);
|
||||
|
||||
// safety, kills process if test framework doesn't
|
||||
setTimeout(function() {
|
||||
process.exit();
|
||||
}, 5000);
|
||||
|
||||
Reference in New Issue
Block a user