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

balaur

v0.2.0

Published

A daemonizing manager for Node.js applications

Downloads

1

Readme

Balaur

Balaur is a daemonizing manager for Node.js applications written in order to be able to create systemctl services for *nix systems. It allows you to run the applications as services (daemons) on *nix systems, with all the features attached to that (start, stop, restart and status).

Installing Balaur

Globally

npm install -g balaur

Locally

npm install balaur

Configuration

Create a file in your project named balaur.config.mjs

Alternatively you can export an environment variable called BALAUR_CONFIG_FILE with the path.

In this file you can specify the following values:

export default {
  main: "index.mjs",
  workers: 1,
  pidfilePath: "pidfile.pid",
  stdOutPath: "out.log",
  stdErrPath: "err.log"
}
  • main - default index.mjs - represents the file that exports the default function that will be daemonized
  • workers - default 1 on NODE_ENV === development and cpu count on other values - the number of spawned processes (see threads vs process Node.js and C10K problem)
  • pidfilePath - default pidfile.pid - the file which maintains the pid of the master process
  • stdOutPath - default out.log - the file (or socket) where the stdout will be redirected
  • stdErrPath - default err.log - the file (or socket) where the stderr will be redirected

Commands

  • start starts a daemon and detaches it creating an IPC Channel for its stderr and stdout
  • stop stops the daemon by sending a unix signal, can only be used on started daemons
  • restart restarts the daemon by sending a unix signal, can only be used on started daemons

All daemons respect unix signals.

Usage

Execute remote with NPX

npx balaur [command]

Bin

balaur [command]

Custom execution

NOTE: On custom execution the config file does not apply.

Create a index.mjs file with the code similar to the following:

import Balaur from 'balaur';

const config = {
  workers: process.env.NODE_ENV !== 'development' ? cpus().length : 1,
  pidfilePath: 'pidfile.pid',
  stdOutPath: 'out.log',
  stdErrPath: 'err.log'
};

const balaur = new Balaur(() => {
  // Your daemonized code goes here
  console.log('Hello, World!');
}, config);

balaur.processArgs();

systemctl service creation

In your project edit the package.json and modify the following scripts:

{
  "scripts": {
    "start": "balaur start",
    "stop": "balaur stop",
    "restart": "balaur restart"
  }
}

If you used custom execution

{
  "scripts": {
    "start": "node ./ start",
    "stop": "node ./ stop",
    "restart": "node ./ restart"
  }
}

Config the service

Create a user to run your service. This is important to protect your system in case the service can be hacked.

sudo adduser \
   --system \
   --shell /bin/bash \
   --gecos 'node' \
   --disabled-password \
   --home /srv/www \
   node

Create a file in /lib/systemd/system/myservice.service where myservice is the name of your service

Paste the following inside:

[Unit]
Description=My Service
After=network-online.target
Wants=network-online.target

[Service]
User=node
Group=nogroup
WorkingDirectory=/srv/www/path/to/your/project
Type=forking
ExecStart=/usr/bin/npm start
ExecStop=/usr/bin/npm stop
LimitCPU=infinity


[Install]
WantedBy=multi-user.target

Reload the daemons

sudo systemctl daemon-reload

Start/stop/restart daemons

sudo service myservice [start | stop | restart | status]

or

sudo systemctl [start | stop | restart | status] myservice

Enable auto-running on system restart

sudo systemctl enable myservice

Disable auto-running on system restart

sudo systemctl disable myservice