|
4 | 4 |
|
5 | 5 | Adds a new polling worker for a queue and executes the provided callback function when jobs are found. Each call to work() will add a new worker and resolve a unqiue worker id. |
6 | 6 |
|
7 | | -Workers can be stopped via `offWork()` all at once by queue name or individually by using the worker id. Worker activity may be monitored by listening to the `wip` event. |
| 7 | +Workers can be stopped via `offWork()` all at once by queue name or individually by using the worker id. Worker activity may be monitored by listening to the `wip` event or by polling [`getWipData()`](#getwipdataoptions). |
8 | 8 |
|
9 | 9 | The default options for `work()` is 1 job every 2 seconds. |
10 | 10 |
|
@@ -217,6 +217,33 @@ await boss.work('process-video', async ([ job ]) => { |
217 | 217 | }) |
218 | 218 | ``` |
219 | 219 |
|
| 220 | +### `getWipData(options)` |
| 221 | + |
| 222 | +Returns a snapshot of all workers in this instance of pg-boss with state `created`, `active`, or `stopping`. This is the same data payload emitted by the `wip` event, but available on-demand without waiting for a job transition. |
| 223 | + |
| 224 | +Use this for continuous monitoring of worker utilization — for example, driving metrics or autoscaling signals when jobs are long-running and the `wip` event may not fire frequently enough. |
| 225 | + |
| 226 | +**Arguments** |
| 227 | +- `options`: object *(optional)* |
| 228 | + |
| 229 | +**Options** |
| 230 | + |
| 231 | +* **includeInternal**, bool, *(default=false)* |
| 232 | + |
| 233 | + If true, includes workers for pg-boss internal queues (e.g., scheduling). |
| 234 | + |
| 235 | +**Returns**: `WipData[]` |
| 236 | + |
| 237 | +```js |
| 238 | +// Poll worker utilization every 2 seconds for metrics |
| 239 | +setInterval(() => { |
| 240 | + const workers = boss.getWipData() |
| 241 | + const working = workers.filter(w => w.state === 'active' && w.count > 0).length |
| 242 | + const idle = workers.filter(w => w.state === 'active' && w.count === 0).length |
| 243 | + console.log(`working: ${working}, idle: ${idle}`) |
| 244 | +}, 2000) |
| 245 | +``` |
| 246 | + |
220 | 247 | ### `notifyWorker(id)` |
221 | 248 |
|
222 | 249 | Notifies a worker by id to bypass the job polling interval (see `pollingIntervalSeconds`) for this iteration in the loop. |
|
0 commit comments