npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mjpitz/simple-daemon-node

v0.0.3

Published

Simplistic wrapper that facilitates writing long lived NodeJS daemons with lifecycle management

Downloads

8

Readme

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.