Skip to content

Commit d519bfa

Browse files
authored
Add jobId as param to Worker execution (#81)
1 parent 37026ef commit d519bfa

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/Queue.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ export interface QueueOptions {
2929
* queue.configure({onQueueFinish:(executedJobs:Job[])=>{
3030
* console.log("Queue stopped and executed",executedJobs)
3131
* }});
32-
* queue.addWorker(new Worker("testWorker",async(payload)=>{
32+
* queue.addWorker(new Worker("testWorker",async(payload, id)=>{
3333
* return new Promise((resolve) => {
3434
* setTimeout(() => {
35-
* console.log(payload.text);
35+
* console.log('Executing jobId', id, 'with:', payload.text);
3636
* resolve();
3737
* }, payload.delay);});
3838
* }))
39-
* queue.addJob("testWorker",{text:"Job example palyoad content text",delay:5000})
39+
* queue.addJob("testWorker",{text:"Job example payload content text",delay:5000})
4040
* ```
4141
*/
4242
export class Queue {

src/Worker.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class Worker<P extends object> {
2121
public readonly concurrency: number;
2222

2323
private executionCount: number;
24-
private executer: (payload: P) => CancellablePromise<any>;
24+
private executer: (payload: P, id: string) => CancellablePromise<any>;
2525

2626
private onStart: (job: Job<P>) => void;
2727
private onSuccess: (job: Job<P>) => void;
@@ -35,7 +35,7 @@ export class Worker<P extends object> {
3535
* @param executer function to run jobs
3636
* @param options to configure worker
3737
*/
38-
constructor(name: string, executer: (payload: P) => Promise<any>, options: WorkerOptions<P> = {}) {
38+
constructor(name: string, executer: (payload: P, id: string) => Promise<any>, options: WorkerOptions<P> = {}) {
3939
const {
4040
onStart = (job: Job<P>) => {},
4141
onSuccess = (job: Job<P>) => {},
@@ -81,7 +81,7 @@ export class Worker<P extends object> {
8181
if (timeout > 0) {
8282
return this.executeWithTimeout(job, timeout);
8383
} else {
84-
return this.executer(payload);
84+
return this.executer(payload, job.id);
8585
}
8686
}
8787
private executeWithTimeout(job: Job<P>, timeout: number) {
@@ -92,7 +92,7 @@ export class Worker<P extends object> {
9292
reject(new Error(`Job ${job.id} timed out`));
9393
}, timeout);
9494
});
95-
const executerPromise = this.executer(job.payload);
95+
const executerPromise = this.executer(job.payload, job.id);
9696
if (executerPromise) {
9797
cancel = executerPromise[CANCEL];
9898
try {

0 commit comments

Comments
 (0)