uphold/process-manager

Name: process-manager

Owner: Uphold

Description: A node.js process manager using co.

Created: 2017-01-05 18:46:10.0

Updated: 2017-03-23 17:20:35.0

Pushed: 2018-03-09 17:00:32.0

Homepage:

Size: 62

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

process-manager

A node.js process manager. This package handles a process's lifecycle, from running to exiting, by handling errors and exceptions, as well as graceful shutdowns.

Status

npm version build status

Installation

Install the package via yarn:

rn add '@uphold/process-manager'

Or npm:

m install '@uphold/process-manager' --save
Usage

To use process-manager simply require it in your project.

t processManager = require('process-manager');

sync/await
essManager.once(async () => {
ait foo.bar();


romise
essManager.once(() => new Promise((resolve, reject) => {
o.bar(err => {
if (err) return reject();

return resolve();
;

And it will now manage your node process.

loop(fn, [options])

This lifecycle is used to loop over a given function.

Arguments Example
t processManager = require('process-manager');

essManager.loop(async () => {
nsole.log(await client.getSomeInfo());
 interval: 600 });
on(fn)

This lifecycle is used to get a function suited for using with an event emitter. It does not exit unless something goes wrong.

Arguments Example
t processManager = require('process-manager');

c function handler(value) {
nsole.log(await client.getInfo(value));


nt.on('event', processManager.on(handler));
once(fn)

This lifecycle is used to a given function and exit.

Arguments Example
t processManager = require('process-manager');

essManager.once(async () => {
ait client.doWork();

shutdown([args])

This function can be called to trigger a process shutdown. If passed an error as an optional argument, it will save it to the errors array. This function will only start the shutdown process once, any extra calls will be ignored, although it will still save the error if one is passed.

If called with { force: true } it will skip waiting for running processes and immediately start disconnecting.

Arguments Example
t processManager = require('process-manager');

essManager.shutdown({ error: new Error('Error') });
Hooks

Currently there are three hooks that can be used to call external code during the shutdown process. If a hook takes longer than 30 seconds to return, it will timeout and continue with the shutdown process.

drain

This hook is called during shutdown, after all running processes have stopped. It should be used to drain connections if the process is running a server.

disconnect

This hook is called after drain and it's where handlers should be added to close running services (ex.: database connections, persistent connections, etc).

exit

This hook is called right before the process exits. It passes an array of errors as an argument to the handler function, and should be used to handle errors before exiting.

addHook({ handler, type, [name='a handler'] })

This function is used to add a hook for one of the types described above.

Arguments
t processManager = require('process-manager');

essManager.addHook({ handler: () => 'result', type: <hook-type> });
essManager.addHook({ handler: () => Promise.resolve('result'), type: <hook-type> });
Integrations
sentry.io

The recommended way to report errors to sentry is by adding an exit hook and sending each error using a promisified captureException.

t Promise = require('bluebird');
t raven = Promise.promisifyAll(require('raven'));

n.config('https://******@sentry.io/<appId>').install();

essManager.addHook({
ndler: errors => Promise.map(errors, error => raven.captureExceptionAsync(error)),
me: 'sentry',
pe: 'exit'

Debug

Enable verbose debugging by setting the DEBUG environment variable to DEBUG=process-manager.

Release
m version [<new version> | major | minor | patch] -m "Release %s"`
Test

To test using a local version of node, run:

rn test
License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.