Skip to content
Open
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
27 changes: 27 additions & 0 deletions docs/gitbook/guide/workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,33 @@ It is also possible to specify the data types for the Job data and return value
const worker = new Worker<MyData, MyReturn>(queueName, async (job: Job) => {});
```

By default the job progress is typed as `string | boolean | number | object`. If you report progress with a specific shape, you can also specify a progress type so that `updateProgress` and the `progress` event are type-safe:

```typescript
import { Worker, RedisQueueBackend } from 'bullmq';

type MyProgress = { percentage: number; message: string };

const worker = new Worker<
MyData,
MyReturn,
string,
RedisQueueBackend,
MyProgress
>(queueName, async job => {
// progress is checked against MyProgress
await job.updateProgress({ percentage: 42, message: 'halfway' });
});

worker.on('progress', (job, progress) => {
console.log(progress.percentage, progress.message);
});
```

{% hint style="info" %}
The progress type must be JSON-serializable, as progress is persisted in the queue's backend. Sandboxed processors always receive the default progress type, since the value crosses a process boundary.
{% endhint %}

## Read more:

- 💡 [Worker API Reference](https://docs.bullmq.io/api/classes/v6.Worker.html)
Expand Down
7 changes: 4 additions & 3 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export class Job<
DataType = any,
ReturnType = any,
NameType extends string = string,
> implements MinimalJob<DataType, ReturnType, NameType> {
ProgressType extends JobProgress = JobProgress,
> implements MinimalJob<DataType, ReturnType, NameType, ProgressType> {
/**
* It includes the prefix, the namespace separator :, and queue name.
* @see {@link https://www.gnu.org/software/gawk/manual/html_node/Qualified-Names.html}
Expand All @@ -66,7 +67,7 @@ export class Job<
* The progress a job has performed so far.
* @defaultValue 0
*/
progress: JobProgress = 0;
progress: ProgressType = 0 as ProgressType;

/**
* The value returned by the processor when processing this job.
Expand Down Expand Up @@ -539,7 +540,7 @@ export class Job<
*
* @param progress - number or object to be saved as progress.
*/
async updateProgress(progress: JobProgress): Promise<void> {
async updateProgress(progress: ProgressType): Promise<void> {
this.progress = progress;
await this.backend.updateProgress(this.id, progress);
this.queue.emit('progress', this, progress);
Expand Down
Loading
Loading