mirror of
https://github.com/ION606/archivebot.git
synced 2026-06-06 07:52:57 +00:00
Initial commit
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2011 Paul Driver
|
||||
|
||||
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.
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
Init
|
||||
====
|
||||
|
||||
Turn your node daemon into an LSB-compatible init script.
|
||||
|
||||
For the impatient
|
||||
-----------------
|
||||
|
||||
init = require('init');
|
||||
|
||||
init.simple({
|
||||
pidfile : '/var/run/myprog.pid',
|
||||
logfile : '/var/log/myprog.log',
|
||||
command : process.argv[3],
|
||||
run : function () {
|
||||
doWhateverMyDaemonDoes();
|
||||
}
|
||||
})
|
||||
|
||||
init.simple() doesn't do what I want
|
||||
------------------------------------
|
||||
You're in luck (maybe). simple() just makes the easy case easy. See the api
|
||||
methods below for more flexible ways to use this module.
|
||||
|
||||
API
|
||||
---
|
||||
|
||||
### init.start(options)
|
||||
|
||||
Starts your service. This function will not return, and takes the following
|
||||
keyword arguments:
|
||||
|
||||
#### pidfile
|
||||
|
||||
Required. This should be a path to a file to lock and store the daemon pid in.
|
||||
If the daemon is already running according to this pidfile, start succeeds
|
||||
without doing anything.
|
||||
|
||||
#### logfile
|
||||
|
||||
Path to a file to redirect your daemon's stdout and stderr to. Defaults to
|
||||
/dev/null.
|
||||
|
||||
#### run
|
||||
|
||||
Required. A function to be called after daemon setup is complete. Do your
|
||||
daemon work here.
|
||||
|
||||
#### success (pid, wasRunning)
|
||||
|
||||
A function to be called when the start action succeeded (already running or
|
||||
about to daemonize). 'pid' will be the id of the running process, and
|
||||
'wasRunning' is true if the process was already running.
|
||||
|
||||
#### failure(error)
|
||||
|
||||
A function to be called if the start action cannot be performed. Error will be
|
||||
some sort of stringifiable error object. Defaults to init.startFailed.
|
||||
|
||||
### init.stop(pidfile, cb, killer)
|
||||
|
||||
Stops your service with one of shutdown functions. Default is
|
||||
`init.hardKiller(2000)`, but you may pass your own.
|
||||
|
||||
### init.status(pidfile, cb)
|
||||
|
||||
Gets the status of your service. The status is not returned, but rather will
|
||||
be passed to cb if you provide it (defaults to init.printStatus). It is an
|
||||
object of the form: { running: true, pid: 3472, exists: true }.
|
||||
|
||||
### init.simple(options)
|
||||
|
||||
Higher level method that leaves all the callbacks as defaults and dispatches
|
||||
to calling the right function depending on the string you provide. Takes the
|
||||
following keyword arguments:
|
||||
|
||||
#### pidfile
|
||||
#### run
|
||||
#### logfile
|
||||
As in init.start()
|
||||
|
||||
### killer
|
||||
As in init.stop()
|
||||
|
||||
#### command
|
||||
A string on which to dispatch. Defaults to your program's first argument
|
||||
(process.argv[2]). Recognized actions are "start", "stop", "restart",
|
||||
"try-restart", "force-reload", and "status".
|
||||
|
||||
#### killer
|
||||
As in init.stop()
|
||||
|
||||
Shutdown functions
|
||||
-----------------
|
||||
|
||||
### init.hardKiller(delay = 2000)
|
||||
|
||||
Sends your service TERM, INT, QUIT, in that order (with 2000 ms delays) and
|
||||
then KILL until the process is no longer running, then calls cb (defaults to
|
||||
init.stopped). If the process was running, cb's first argument will be true.
|
||||
This is the default shutdown function.
|
||||
|
||||
### init.softKiller(delay = 2000)
|
||||
|
||||
Sends your service TERM and waits until it dies with 2000 ms delays. If it is
|
||||
more important that your service shutdown gracefully (to preserve data
|
||||
integrity, etc) than that it exits promptly, this is a good choice.
|
||||
|
||||
|
||||
Default Actions
|
||||
---------------
|
||||
These functions are the defaults for various callbacks, but you can call them
|
||||
from your own custom callbacks if you want to augment them instead of
|
||||
replacing them.
|
||||
|
||||
### init.startSucceeded(pid, wasRunning)
|
||||
|
||||
Prints "Started with PID n" or "Already running with PID n" and exits with a 0
|
||||
status code.
|
||||
|
||||
### init.startFailed(error)
|
||||
|
||||
Prints error and exits with a 1 status code.
|
||||
|
||||
### init.stopped(killed)
|
||||
|
||||
Prints "Stopped" or "Not running" and exits with a 0 status code.
|
||||
|
||||
### init.printStatus (status)
|
||||
|
||||
Prints a human-readable message and exits with an LSB-appropriate error code.
|
||||
|
||||
Program is running:
|
||||
|
||||
Process is already running with pid N.
|
||||
exit 0
|
||||
|
||||
Program is dead (exited without removing pid file)
|
||||
|
||||
Pidfile exists, but process is dead.
|
||||
exit 2
|
||||
|
||||
Program is not running:
|
||||
|
||||
Not running.
|
||||
exit 3
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
(function() {
|
||||
var daemon, fs;
|
||||
fs = require('fs');
|
||||
daemon = require('daemon');
|
||||
exports.printStatus = function(st) {
|
||||
if (st.pid) {
|
||||
console.log('Process running with pid %d.', st.pid);
|
||||
return process.exit(0);
|
||||
} else if (st.exists) {
|
||||
console.log('Pidfile exists, but process is dead.');
|
||||
return process.exit(1);
|
||||
} else {
|
||||
console.log('Not running.');
|
||||
return process.exit(3);
|
||||
}
|
||||
};
|
||||
exports.status = function(pidfile, cb) {
|
||||
if (cb == null) {
|
||||
cb = exports.printStatus;
|
||||
}
|
||||
return fs.readFile(pidfile, 'utf8', function(err, data) {
|
||||
var match, pid;
|
||||
if (err) {
|
||||
return cb({
|
||||
exists: err.code !== 'ENOENT'
|
||||
});
|
||||
} else if (match = /^\d+/.exec(data)) {
|
||||
pid = parseInt(match[0]);
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return cb({
|
||||
pid: pid
|
||||
});
|
||||
} catch (e) {
|
||||
return cb({
|
||||
exists: true
|
||||
});
|
||||
}
|
||||
} else {
|
||||
return cb({
|
||||
exists: true
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
exports.startSucceeded = function(pid) {
|
||||
if (pid) {
|
||||
return console.log('Process already running with pid %d.', pid);
|
||||
} else {
|
||||
return console.log('Started.');
|
||||
}
|
||||
};
|
||||
exports.startFailed = function(err) {
|
||||
console.log(err);
|
||||
return process.exit(1);
|
||||
};
|
||||
exports.start = function(_arg) {
|
||||
var failure, logfile, pidfile, run, start, success;
|
||||
pidfile = _arg.pidfile, logfile = _arg.logfile, run = _arg.run, success = _arg.success, failure = _arg.failure;
|
||||
success || (success = exports.startSucceeded);
|
||||
failure || (failure = exports.startFailed);
|
||||
logfile || (logfile = '/dev/null');
|
||||
start = function(err) {
|
||||
if (err) {
|
||||
return failure(err);
|
||||
}
|
||||
return fs.open(logfile, 'a+', 0666, function(err, fd) {
|
||||
var pid;
|
||||
if (err) {
|
||||
return failure(err);
|
||||
}
|
||||
success();
|
||||
pid = daemon.start(fd);
|
||||
daemon.lock(pidfile);
|
||||
return run();
|
||||
});
|
||||
};
|
||||
return exports.status(pidfile, function(st) {
|
||||
if (st.pid) {
|
||||
return success(st.pid, true);
|
||||
} else if (st.exists) {
|
||||
return fs.unlink(pidfile, start);
|
||||
} else {
|
||||
return start();
|
||||
}
|
||||
});
|
||||
};
|
||||
exports.stopped = function(killed) {
|
||||
if (killed) {
|
||||
console.log('Stopped.');
|
||||
} else {
|
||||
console.log('Not running.');
|
||||
}
|
||||
return process.exit(0);
|
||||
};
|
||||
exports.hardKiller = function(timeout) {
|
||||
if (timeout == null) {
|
||||
timeout = 2000;
|
||||
}
|
||||
return function(pid, cb) {
|
||||
var signals, tryKill;
|
||||
signals = ['TERM', 'INT', 'QUIT', 'KILL'];
|
||||
tryKill = function() {
|
||||
var sig;
|
||||
sig = "SIG" + signals[0];
|
||||
try {
|
||||
process.kill(pid, sig);
|
||||
if (signals.length > 1) {
|
||||
signals.shift();
|
||||
}
|
||||
return setTimeout((function() {
|
||||
return tryKill(sig);
|
||||
}), timeout);
|
||||
} catch (e) {
|
||||
return cb(signals.length < 4);
|
||||
}
|
||||
};
|
||||
return tryKill();
|
||||
};
|
||||
};
|
||||
exports.softKiller = function(timeout) {
|
||||
if (timeout == null) {
|
||||
timeout = 2000;
|
||||
}
|
||||
return function(pid, cb) {
|
||||
var sig, tryKill;
|
||||
sig = "SIGTERM";
|
||||
tryKill = function() {
|
||||
var first;
|
||||
try {
|
||||
process.kill(pid, sig);
|
||||
console.log("Waiting for pid " + pid);
|
||||
if (sig !== 0) {
|
||||
sig = 0;
|
||||
}
|
||||
first = false;
|
||||
return setTimeout(tryKill, timeout);
|
||||
} catch (e) {
|
||||
return cb(sig === 0);
|
||||
}
|
||||
};
|
||||
return tryKill();
|
||||
};
|
||||
};
|
||||
exports.stop = function(pidfile, cb, killer) {
|
||||
if (cb == null) {
|
||||
cb = exports.stopped;
|
||||
}
|
||||
if (killer == null) {
|
||||
killer = exports.hardKiller(2000);
|
||||
}
|
||||
return exports.status(pidfile, function(_arg) {
|
||||
var pid;
|
||||
pid = _arg.pid;
|
||||
if (pid) {
|
||||
return killer(pid, function(killed) {
|
||||
return fs.unlink(pidfile, function() {
|
||||
return cb(killed);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return cb(false);
|
||||
}
|
||||
});
|
||||
};
|
||||
exports.simple = function(_arg) {
|
||||
var command, killer, logfile, pidfile, run, start;
|
||||
pidfile = _arg.pidfile, logfile = _arg.logfile, command = _arg.command, run = _arg.run, killer = _arg.killer;
|
||||
command || (command = process.argv[2]);
|
||||
killer || (killer = null);
|
||||
start = function() {
|
||||
return exports.start({
|
||||
pidfile: pidfile,
|
||||
logfile: logfile,
|
||||
run: run
|
||||
});
|
||||
};
|
||||
switch (command) {
|
||||
case 'start':
|
||||
return start();
|
||||
case 'stop':
|
||||
return exports.stop(pidfile, null, killer);
|
||||
case 'status':
|
||||
return exports.status(pidfile);
|
||||
case 'restart':
|
||||
case 'force-reload':
|
||||
return exports.stop(pidfile, start, killer);
|
||||
case 'try-restart':
|
||||
return exports.stop(pidfile, function(killed) {
|
||||
if (killed) {
|
||||
return exports.start({
|
||||
pidfile: pidfile,
|
||||
logfile: logfile,
|
||||
run: run
|
||||
});
|
||||
} else {
|
||||
console.log('Not running.');
|
||||
return process.exit(1);
|
||||
}
|
||||
});
|
||||
default:
|
||||
console.log('Command must be one of: ' + 'start|stop|status|restart|force-reload|try-restart');
|
||||
return process.exit(1);
|
||||
}
|
||||
};
|
||||
}).call(this);
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"engines" : {
|
||||
"node" : ">=0.4.7"
|
||||
},
|
||||
"repository" : {
|
||||
"url" : "git://github.com/frodwith/node-init.git",
|
||||
"type" : "git"
|
||||
},
|
||||
"maintainers" : [
|
||||
{
|
||||
"email" : "frodwith@gmail.com",
|
||||
"name" : "Paul Driver"
|
||||
}
|
||||
],
|
||||
"version" : "0.1.2",
|
||||
"licences" : [
|
||||
{
|
||||
"url" : "http://www.opensource.org/licenses/mit-license",
|
||||
"type" : "MIT License"
|
||||
}
|
||||
],
|
||||
"dependencies" : {
|
||||
"daemon" : ">=0.3.0"
|
||||
},
|
||||
"name" : "init",
|
||||
"bugs" : "https://github.com/frodwith/node-init/issues",
|
||||
"author" : "Paul Driver <frodwith@gmail.com>",
|
||||
"description" : "Turn your node daemon into an LSB-like init script",
|
||||
"main" : "./init.js",
|
||||
"keywords" : [
|
||||
"daemon",
|
||||
"init",
|
||||
"service",
|
||||
"LSB"
|
||||
],
|
||||
"devDependencies" : {},
|
||||
"contributors" : [
|
||||
{
|
||||
"email" : "frodwith@gmail.com",
|
||||
"name" : "Paul Driver"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user