This repository demonstrates the organization and management of NodeJS child processes, referred to as "workers", within dedicated pools. It provides callback handling and load balancing. The config/default.json includes a working configuration for two different worker pools to test over provided http endpoints.
The examples/example_node_red_flow.json
file provides a Node-RED flow to send example requests to the server.
- Worker Pools: Organize workers into dedicated pools.
- One-shot Workers: Execute tasks in one-shot processes that terminate after task completion.
- HTTP Endpoints:
/example/pool
: Dispatch tasks to workers in a pool./example/oneShot
: Execute tasks in one-shot processes.
- Ensure you have Node.js installed.
- Clone the repository.
- Run
npm install
to install the required dependencies. - Start the server using
node app.js
.
The configuration file (config/default.json
) has two predefined worker pools:
-
CPU Worker Pool:
- Pool Name: CPU
- Worker Script:
./workers/exampleWorker_CPULoad.js
- Worker Count: 2
- Memory Limit: 4048 MB
-
Memory Worker Pool:
- Pool Name: MEM
- Worker Script:
./workers/exampleWorker_MemoryUsage.js
- Worker Count: 2
- Memory Limit: 4048 MB
./workers/exampleWorker_MemoryUsage.js
This worker script simulates memory usage of a given amount for a given duration.
{
"poolName": "<Name of the worker pool>",
"workerTask": {<Task data>}
}
{
"poolName": "CPU",
"workerTask": { "duration": 3000 }
}
{
"poolName": "MEM",
"workerTask": { "duration": 3000, "mb": 300 }
}
{
"workerScript": "<Path to the worker's JavaScript file>",
"workerTask": {<Task data>},
"workerMemoryLimit": "<Memory limit in MB, optional, default 4096 MB>"
}
{
"workerScript": "./workers/exampleWorker_CPULoad.js",
"workerTask": { "duration": 3000 },
"workerMemoryLimit": 2048
}
{
"workerScript": "./workers/exampleWorker_MemoryUsage.js",
"workerTask": { "duration": 3000, "mb": 300 },
"workerMemoryLimit": 4096
}