0.0.3 • Published 6 years ago

@mjpitz/simple-daemon-node v0.0.3

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
6 years ago

simple-daemon-node

Largely inspired off of Sander Marechal's code sample. I once used this nice little python shim to be able to write a python daemon with very little overhead. Since I work a lot in NodeJS, I wanted a similar wrapper, but with a few more bells and whistles built in. What I ended up with was this rather simple library that can also plug into your system's startup.

$ mydaemon
Available commands: restart|start|status|stop|version

Implementations

There are a few reference implementations within the examples folder. When assembling the project, I wanted to be able to push as much into configuration so that setup would be easy. As a result, I wound up with a configuration driven solution that can be used to manage any process.

PropertyTypeDescription
namestringThe name the process should run under.
versionstring=The version of the program. Typically taken from package.json.
logFilestring=Path to the log file where your daemon output should go.
daemonfunction=When 'command' is not specified, this function is invoked as the daemon process instead.
commandstring=The binary that should be executed as the daemon process. The 'daemon' function is invoked if not specified.
mainScriptstring=Path to the main script being executed. Popular for languages like NodeJs, Python, and PHP.
argsArray.<string>=Additional arguments you want to pass along to the daemon script.

Embedding a daemon process

Not preferred, but added out of convenience.

const SimpleDaemon = require('@mjpitz/simple-daemon-node');

const mydaemon = new SimpleDaemon({
    name: 'mydaemon',
    logFile: '/path/to/daemon.log',
    version: '1.0.0',
    daemon: () => {
        const express = require('express');
        const app = express();
        
        app.get('/', (req, res) => {
            res.send('Hello World!');
        });
        
        app.listen(3000, () => {
            console.log('Example daemon listening on port 3000!');
        });
    }
});

mydaemon.run(); // this will parse process.argv and run the proper target

Delegating to a daemon process

This approach makes it easy to call out to a separate script. Below, I demonstrate using a separate NodeJS script as an example.

const SimpleDaemon = require('@mjpitz/simple-daemon-node');

const mydaemon = new SimpleDaemon({
    name: 'mydaemon',
    logFile: '/path/to/daemon.log',
    version: '1.0.0',
    command: '/full/path/to/node',
    mainScript: require.resolve('./daemon-script.js')
});

mydaemon.run(); // this will parse process.argv and run the proper target

The contents of daemon-script.js would then be:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen(3000, () => {
    console.log('Example daemon listening on port 3000!');
});

When a command is not specified and a mainScript is, we default to using process.argv[0]. This value ends up being the runtime that this command is executed under.

Alternatively, this can be used to start other languages up as a daemon as well. Below is an example that invokes a python script as a daemon.

const SimpleDaemon = require('@mjpitz/simple-daemon-node');

const mydaemon = new SimpleDaemon({
    name: 'mydaemon',
    logFile: '/path/to/daemon.log',
    version: '1.0.0',
    command: '/full/path/to/python',
    mainScript: require.resolve('./daemon-script.py')
});

mydaemon.run();

Or a compiled go binary:

const SimpleDaemon = require('@mjpitz/simple-daemon-node');

const mydaemon = new SimpleDaemon({
    name: 'mydaemon',
    logFile: '/path/to/daemon.log',
    version: '1.0.0',
    command: '/full/path/to/gobinary'
});

mydaemon.run();

The nice thing about this approach is that it decouples your daemon process from it's lifecycle management. You can run the process directly, which makes testing and familiarization easy. Then, you can install it as a daemon process and let your system manage it's lifecycle for you.

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago