Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .eslintrc.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
test:
strategy:
matrix:
nodeVersion: [18, 20, 22]
nodeVersion: [20, 22, 24]

name: Tests
runs-on: ubuntu-latest
Expand All @@ -15,11 +15,11 @@ jobs:
image: node:${{ matrix.nodeVersion }}-alpine

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- name: Install dependencies
run: yarn install --frozen-lockfile

- run: yarn lint

- run: yarn test
- run: yarn test:coverage
20 changes: 11 additions & 9 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,38 @@ on:
jobs:
release:
runs-on: ubuntu-latest
concurrency: 1
concurrency:
group: release-${{ github.repository }}-${{ github.ref_name }}
cancel-in-progress: false
environment: release

steps:
- name: Checkout code
uses: actions/checkout@v4
uses: actions/checkout@v6
with:
token: ${{ secrets.RELEASE_GITHUB_TOKEN }}
persist-credentials: false

- name: Setup Node.js version
uses: actions/setup-node@v4
uses: actions/setup-node@v6
with:
node-version: 22

- name: Enable yarn
run: corepack enable

- name: Configure npm
run: npm config set //registry.npmjs.org/:_authToken ${{ secrets.RELEASE_NPM_TOKEN }}

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Configure git
run: |
git config user.name "Uphold"
git config user.email "bot@uphold.com"
git config --global url.https://${{ secrets.RELEASE_GITHUB_TOKEN }}@github.com/.insteadOf https://github.com/

- name: Generate release
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.RELEASE_NPM_TOKEN }}
run: |
echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" > .npmrc
npm whoami
npm run release -- --increment "${{ github.event.inputs.VERSION_BUMP }}" -V
run: npm run release -- --increment "${{ github.event.inputs.VERSION_BUMP }}" -V
74 changes: 42 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ A node.js process manager. This package handles a process's lifecycle, from runn
Install the package via **yarn**:

```shell
yarn add '@uphold/process-manager'
yarn add '@uphold/process-manager'
```

Or **npm**:

```shell
npm install '@uphold/process-manager' --save
npm install '@uphold/process-manager' --save
```

## Usage
Expand All @@ -28,20 +28,22 @@ To use `process-manager` simply require it in your project.
```javascript
const processManager = require('process-manager');

// async/await
// async/await.
processManager.once(async () => {
await foo.bar();
});

// Promise
processManager.once(() => new Promise((resolve, reject) => {
foo.bar(err => {
if (err) return reject();

return resolve();
});
}));

// Promise.
processManager.once(
() =>
new Promise((resolve, reject) => {
foo.bar((err) => {
if (err) return reject();

return resolve();
});
})
);
```

And it will now manage your node process.
Expand All @@ -50,43 +52,49 @@ And it will now manage your node process.

This lifecycle is used to loop over a given function.

#### Arguments
#### Arguments (`loop`)

- `fn` _(Function)_: the function to run.
- `[options]` _(object)_: the options object.
- `[options.interval=0]` _(integer)_: how long to wait (in miliseconds) before restarting the function.

#### Example
#### Example (`loop`)

```javascript
const processManager = require('process-manager');

processManager.loop(async () => {
console.log(await client.getSomeInfo());
}, { interval: 600 });
processManager.loop(
async () => {
console.log(await client.getSomeInfo());
},
{ interval: 600 }
);
```

You can also return an object with an `interval` property to override the next interval.

```javascript
const processManager = require('process-manager');

processManager.loop(async () => {
console.log(await client.getSomeInfo());
processManager.loop(
async () => {
console.log(await client.getSomeInfo());

return { interval: 1000 };
}, { interval: 600 });
return { interval: 1000 };
},
{ 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
#### Arguments (`on`)

- `fn` _(Function)_: the function to run.

#### Example
#### Example (`on`)

```javascript
const processManager = require('process-manager');
Expand All @@ -102,11 +110,11 @@ client.on('event', processManager.on(handler));

This lifecycle is used to a given function and exit.

#### Arguments
#### Arguments (`once`)

- `fn` _(Function)_: the function to run.

#### Example
#### Example (`once`)

```javascript
const processManager = require('process-manager');
Expand All @@ -122,13 +130,13 @@ This function can be called to trigger a process shutdown. If passed an error as

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

#### Arguments
#### Arguments (`shutdown`)

- `[args]` _(object)_: the arguments object.
- `[args.error]` _(Error)_: an error to add to the errors array.
- `[args.force]` _(Boolean)_: a boolean that forces the shutdown to skip waiting for running processes.

#### Example
#### Example (`shutdown`)

```javascript
const processManager = require('process-manager');
Expand Down Expand Up @@ -156,7 +164,7 @@ This hook is called right before the process exits. It passes an array of errors

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

#### Arguments
#### Arguments (`addHook`)

- `args.handler` _(Function)_: a function that returns a value or a thenable.
- `args.type` _(string)_: the hook type.
Expand All @@ -182,7 +190,8 @@ const raven = Promise.promisifyAll(require('raven'));
raven.config('https://******@sentry.io/<appId>').install();

processManager.addHook({
handler: errors => Promise.map(errors, error => raven.captureExceptionAsync(error)),
handler: (errors) =>
Promise.map(errors, (error) => raven.captureExceptionAsync(error)),
name: 'sentry',
type: 'exit'
});
Expand All @@ -194,8 +203,9 @@ Enable verbose debugging by configuring your own logger and passing it to `proce

The minimum requirements for it to work is that the logger must be Object-like and have functions assigned to properties `info`, `warn`, and `error`.
The functions should be able to handle two different argument signatures:
- log.<level>(message)
- log.<level>(fields, message)

- log.`<level>`(message)
- log.`<level>`(fields, message)

Most javascript loggers should use this format (this one was derived from [bunyan](https://www.npmjs.com/package/bunyan))

Expand All @@ -208,7 +218,7 @@ The release of a version is automated via the [release](https://github.com/uphol
To test using a local version of `node`, run:

```sh
yarn test
yarn test
```

## License
Expand Down
24 changes: 24 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

/**
* Module dependencies.
*/

import { defineConfig } from 'eslint/config';
import uphold from 'eslint-config-uphold';

/**
* Export ESLint config.
*/

export default defineConfig([
uphold,
{
name: 'process-manager/base',
rules: {
'node-plugin/no-process-env': 'off',
'node-plugin/no-process-exit': 'off',
'promise/prefer-await-to-then': 'off'
}
}
]);
37 changes: 22 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,40 @@
"main": "src/index.js",
"repository": "https://github.com/uphold/process-manager.git",
"scripts": {
"lint": "eslint src test",
"lint": "eslint",
"release": "release-it",
"test": "jest --coverage --verbose"
"test": "node --test $(find test -name '*.test.js')",
"test:coverage": "node --test --experimental-test-coverage $(find test -name '*.test.js')"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@uphold/github-changelog-generator": "^3.4.0",
"eslint": "~8.20.0",
"eslint-config-uphold": "^3.2.0",
"jest": "^29.6.4",
"pre-commit": "^1.2.2",
"prettier": "^2.7.1",
"release-it": "^17.6.0"
},
"jest": {
"restoreMocks": true,
"testEnvironment": "node"
"@fastify/pre-commit": "^2.2.1",
"@uphold/github-changelog-generator": "^4.0.2",
"eslint": "~9.39.2",
"eslint-config-uphold": "^6.7.2",
"prettier": "^3.7.4",
"release-it": "^19.2.2"
},
"engines": {
"node": ">=14"
"node": ">=20"
},
"pre-commit": {
"run": [
"lint"
]
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e",
"devEngines": {
"packageManager": {
"name": "yarn",
"version": "^1.22.22",
"onFail": "warn"
},
"runtime": {
"name": "node",
"version": ">=22.20"
}
}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ProcessManager {
* Add hook.
*/

addHook({ type, handler, name = 'a handler' }) {
addHook({ handler, name = 'a handler', type }) {
this.hooks.push({
handler,
name,
Expand Down
Loading