2.3.2 • Published 1 year ago

graceful-shutdown-manager v2.3.2

Weekly downloads
2
License
ISC
Repository
github
Last release
1 year ago

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.

QuestionAnswer
When does the app autoreloadDevelopment mode, upon code changes
When does the app autorestartProduction mode, when the app crashes
How can I force close the app?Press ctrl + c 5 times
Does this library support awaiting dependenciesYes
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 integrateVery 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 LoaderYes, create a folder with the name of the Loader you'd like to extend.
2.3.0

1 year ago

2.2.0

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.0.9

1 year ago

2.1.0

1 year ago

2.0.8

2 years ago

2.0.2

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.3

3 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago