0
# Worker Pool Management
1
2
Core worker pool lifecycle management for controlling the multi-threaded processing system used by @pnpm/worker.
3
4
## Capabilities
5
6
### Restart Worker Pool
7
8
Restarts the worker pool by finishing current workers and creating a new pool. This is useful for resetting the worker state or applying configuration changes.
9
10
```typescript { .api }
11
/**
12
* Restarts the worker pool by finishing current workers and creating a new pool
13
* @returns Promise that resolves when the worker pool has been restarted
14
*/
15
function restartWorkerPool(): Promise<void>;
16
```
17
18
**Usage Example:**
19
20
```typescript
21
import { restartWorkerPool } from "@pnpm/worker";
22
23
// Restart the worker pool (e.g., after configuration changes)
24
await restartWorkerPool();
25
console.log("Worker pool restarted successfully");
26
```
27
28
### Finish Workers
29
30
Finishes all active workers in the pool. This should be called before process termination to ensure all workers complete their current tasks gracefully.
31
32
```typescript { .api }
33
/**
34
* Finishes all active workers in the pool
35
* @returns Promise that resolves when all workers have finished
36
*/
37
function finishWorkers(): Promise<void>;
38
```
39
40
**Usage Example:**
41
42
```typescript
43
import { finishWorkers } from "@pnpm/worker";
44
45
// Gracefully shutdown all workers before process exit
46
process.on('SIGTERM', async () => {
47
await finishWorkers();
48
process.exit(0);
49
});
50
```
51
52
## Implementation Details
53
54
The worker pool is automatically created when needed and uses the following configuration:
55
56
- **Maximum Workers**: Calculated based on `availableParallelism() - 1` or configured via `PNPM_WORKERS` environment variable
57
- **Worker Script**: Uses the internal `worker.js` compiled from `worker.ts`
58
- **Global Cleanup**: Registers global cleanup handlers for proper worker termination
59
- **Worker Pool Library**: Built on `@rushstack/worker-pool` for robust worker management
60
61
The `PNPM_WORKERS` environment variable can be set to control the number of idle CPUs to reserve:
62
- If set, workers = `max(2, availableParallelism() - abs(PNPM_WORKERS)) - 1`
63
- If not set, workers = `max(1, availableParallelism() - 1)`