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

graceful-shutdown-manager

v2.3.2

Published

All in one solution to gracefully shutdown your application through a simple delayed shutdown process. Also allows code reloading and program restarting, instead of using a not so graceful process manager line nodemon.

Downloads

10

Readme

node-graceful-shutdown-manager

Revision 2 - Modular approach

All in one solution to gracefully shutdown your application through a simple delayed shutdown process. Also allows code reloading and program restarting, instead of using a not so graceful process manager line nodemon.

Use for any node.js project which might be negatively impacted by immediate termination.

Locking is a mechanism which avoids beginning the shutdown process until a block of code has finished. Wrap important code such as file writing with lock() and unlock(). This will ensure no corruption occurs or resources are freed when they might be required. Ensure the code within the lock is synchronous. I recommend the fs-extra package for a drop-in fs promise replacement.

Requirements

Autorestart on Windows will require bash added to the PATH environment variable, either through msys2 or WSL2.

Module Functions

// GSM Functions:
  gsm.exit() // Gracefully shutdown application by running free and then terminating the process.
  gsm.free() // Gracefully run free events, without exiting. (autoreload code, etc)
  gsm.isExiting() // Use when in a loop/long task, to avoid continuing
  gsm.isFreeing()

// Module Functions:
  base.lock() // Wait to free the object until it's unlocked again
  base.unlock()
  base.isLocked()
  base.setTimeout(funct, timeMs) // Locks the content, so it will not end the process until the body has finished executing
  base.setInterval(funct, timeMs) // Same as above

Template Project

Base your project around this pattern

https://github.com/c-ridgway/node-graceful-shutdown-manager/tree/main/example

// Requires bash for autorestart/autoreload
npm run development
npm run production

// Doesn't require bash
npm run standalone

FAQ

This project encompasses the app lifecycle, to break it down, it might be easier to explain what it does and does not do.

| Question | Answer | | ------------- | ------------- | | When does the app autoreload | Development mode, upon code changes | | When does the app autorestart | Production mode, when the app crashes | | How can I force close the app? | Press ctrl + c 5 times | | Does this library support awaiting dependencies | Yes | | In Windows won't it gracefully shutdown using ctrl + c? | The autorestart script loop tends to terminate for some reason. On Linux it'll work fine. Autoreload still gracefully shutsdown. | | Can I use process.exit()? | Instead use gsm.exit(), this will gracefully exit when all modules have been freed | | How do I check if the app is exiting? | Use gsm.isFreeing() or use gsm.isExiting(), especially in loops | | What's a Loader? | It's a type of parent module to load modules from a directory (in the example) | | What's a Module? | An object with lifecycle hooks init() ready() free() | | Will my app free if it only partially initialises? | Yes, it will free all loaded modules. | | Is this easy to integrate | Very easy, don't let the scaffolding fool you. This is mostly to ensure all lifecycle hooks and errors will be handled. | | Can I create a self-contained Loader | Yes, create a folder with the name of the Loader you'd like to extend. |