thart
thart
is a Node.js library for managing the lifecycle of multi-process applications. It provides a simple, flexible API for spawning and managing worker processes, handling graceful shutdowns, and coordinating between primary and worker processes.
Acknowledgements:
thart
was inspired by throng and shamelessly uses a lot of code from async-cleanup to manage process cleanup.
Features
- Spawn and manage multiple worker processes
- Support for both
node:cluster
and node:child_process
worker types - Graceful and coordinated shutdown handling
- Configurable startup and shutdown behaviors
- Timeout handling for worker startup
- Flexible API supporting various application structures
Installation
npm
npm install thart
pnpm
pnpm add thart
yarn
yarn add thart
Usage
thart
can be imported via ES6 imports or CommonJS require as a default or named import.
import thart from 'thart';
import { thart } from 'thart';
const thart = require('thart');
const { thart } = require('thart');
View more examples in the examples directory.
Basic Example
import thart from 'thart';
await thart({
worker: {
count: 4,
type: 'cluster',
start: async (id) => {
console.log(`Worker ${id} starting`);
},
stop: async () => {
console.log('Worker stopping');
}
}
});
Primary and Worker Processes
import thart from 'thart';
await thart({
primary: {
start: async () => {
console.log('Primary process started');
},
stop: async () => {
console.log('Primary process stopping');
}
},
worker: {
count: 2,
type: 'childProcess',
start: async (id) => {
console.log(`Worker ${id} started`);
}
}
});
Multiple Worker Types
A powerful feature of thart
is the ability to spawn multiple types of workers in the same application. This lets you seperate out,
e.g., cron job processes from a cluster handling HTTP requests.
import thart from 'thart';
await thart({
worker: [
{
count: 2,
type: 'cluster',
start: async (id) => {
console.log(`Cluster worker ${id} started`);
}
},
{
count: 1,
type: 'childProcess',
start: async (id) => {
console.log(`Child process worker ${id} started`);
}
}
]
});
Notes on Process Groups
When using thart
to start up your node application, ensure you execute
node entrypoint.js
directly instead of using package managers like pnpm
to start your application.
Package managers like pnpm seem to change the way signals are propagated, interfering with the libraries ability to shut down gracefully.
API
thart(options)
The main function to start your application.
Options
grace
(optional): Grace period in milliseconds for shutdown. Default: 10000 (10 seconds)primary
(optional): Configuration for the primary process
start
: Function to run when the primary process startsstop
(optional): Function to run when the primary process is shutting down
worker
: Configuration for worker processes. Can be a single object or an array of objects
count
: Number of worker processes to spawn (when using an array of objects, this is optional and defaults to 1)type
: Type of worker process ('cluster' or 'childProcess')start
: Function to run in each worker processstop
(optional): Function to run when a worker process is shutting downstartupTimeoutMs
(optional): Timeout for worker startup in millisecondskillAfterCompleted
(optional): If true, kills the worker after the start function completes
Types & Overloads
Types
interface CommonThartOptions {
grace?: number;
}
type PrimaryFunction = {
start: () => Promise<void> | void;
stop?: () => Promise<void> | void;
};
type WorkerCount = {
count: number;
};
type WorkerFunction = {
start: (id: number) => Promise<void> | void;
type: "childProcess" | "cluster";
killAfterCompleted?: boolean;
stop?: () => Promise<void> | void;
startupTimeoutMs?: number;
};
Signatures
export async function thart(opts: PrimaryThartOptions): Promise<void>;
export async function thart(opts: WorkerThartOptions): Promise<void>;
export async function thart(opts: WorkerArrayThartOptions): Promise<void>;
export async function thart(opts: PrimaryAndSingleWorkerOptions): Promise<void>;
export async function thart(opts: PrimaryAndArrayWorkerOptions): Promise<void>;
License
thart
is licensed under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.