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

@tpp/pm2

v2.1.0

Published

A PM2 Replacement that works with Electron

Downloads

13

Readme

PM2 for Electron

Process Manager

PM2 is a project that is stable, robust, and well designed. However, when running for Electron, it has two issues:

  1. Electron has an internal filesystem format called ASAR which improves the performance of Electron apps. PM2 is unable to work with this format.
  2. Electron distributes it's own (embedded) NodeJS. However, PM2 ignores this and requires the end user to install another version of NodeJS.

Hence I created this new "Process Manager". It can be used without Electron of course but will also work perfectly with Electron without any of the existing PM2 issues.

Features

  1. Start multiple processes
  2. Start Python and NodeJS processes
  3. Understands NodeJS modules (you can just point to the module folder)
  4. WORKS WITH EMBEDDED Electron NodeJS
  5. Stop any started process whenever you want
  6. Direct the output to a log file
  7. Set the working directory and environment if needed
  8. Pass any required arguments to the scripts
  9. KEEP-ALIVE: Automatically restarts the process if crashes (completely configurable)

Usage

Getting started

const pm2 = require('@tpp/pm2')

pm2.start({
    script: 'path/to/script.py',
})

Checking for errors with a callback

pm2 functions take an optional callback that is invoked when there are errors.

pm2.start({
    script: 'path/to/script.py',
}, (err, pid) => {
    ...
})

The start callback can be invoked multiple times - for each error in the process and when the process restarts. The callback also contains the PID of the process when it starts. A callback without pid or err means it has terminated cleanly.

Passing arguments to scripts

pm2.start({
    script: 'path/to/script.py',
    args: ['say','hello'],
})

Setting Current Working Directory

pm2.start({
    script: 'script.py',
    cwd: '/path/to/folder',
})

Using Node modules

Because pm2 understands Node modules we can simply point to the module folder and it will pick up the correct starting point.

pm2.start({
    cwd: '/path/to/npm/module/',
})

Direct output to log file

pm2.start({
    cwd: '/path/to/npm/module/',
    log: 'mylog.log',
})

Stopping a process

In order to stop a process it needs to be given a name:

pm2.start({
    name: 'my-process',
    cwd: '/path/to/npm/module/',
})

pm2.stop('my-process')

stop also takes an optional callback that will be called when stopped/error.

pm2.stop('my-process', (err) => {
    ...
})

Stopping all processes

You can stop all running processes (named or un-named).

pm2.stopAll()

Restart a process

You can also restart a named process.

pm2.restart('my-process')

Note that restart does not take a callback. It uses the same callback you provided to start.

Setting the environment

You can set the environment of the running process:

pm2.start({
    script: 'path/to/script.py',
    env: ENVIRONMENT_OBJECT,
})

Stopping Hook

If you want to clean up your sub-processes or simply stop cleanly in a sub-process you can use the onstopping event handler:

const pm2 = require('@tpp/pm2')
pm2.onstopping(() => {
    ...
})

Of course this only works if you're sub-process is a NodeJS module.

Crashing & Restarts

Crash

By default pm2 will restart any crashed processes. To prevent 'spinning' it will restart each time with a delay:

  • First crash? Wait 100ms before restarting
  • Crashed again? Wait 500ms before restarting
  • Crashed again? Wait 1 second, then 10 seconds, then 30 seconds, then 1 minute, then 5 minutes before restarting
  • Still crashing? Re-try every 15 minutes

If it has been running successfully for 30 minutes assume started ok.

This is all configurable using the restartAt and restartOk parameters. The default parameters for the behaviour given above is:

pm2.start({
    cwd: '/path/to/npm/module/',
    restartAt: [100, 500, 1*1000, 10*1000, 30*1000, 60*1000, 5*60*1000],
    restartOk: 30 * 60 * 1000,
})

You can design any restart strategy you want using the above parameters.

No Restart

You can turn the restarting behaviour off completely by setting restartAt to [] or [0].

TODO

[ ] Add Support for more process types (besides just python and node)