Git Product home page Git Product logo

simple-daemon-node's Introduction

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.

Property Type Description
name string The name the process should run under.
version string= The version of the program. Typically taken from package.json.
logFile string= Path to the log file where your daemon output should go.
daemon function= When 'command' is not specified, this function is invoked as the daemon process instead.
command string= The binary that should be executed as the daemon process. The 'daemon' function is invoked if not specified.
mainScript string= Path to the main script being executed. Popular for languages like NodeJs, Python, and PHP.
args Array.<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.

simple-daemon-node's People

Contributors

mjpitz avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.