-
Notifications
You must be signed in to change notification settings - Fork 405
/
queue.ts
894 lines (813 loc) · 23.5 KB
/
queue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
import { v4 } from 'uuid';
import {
BaseJobOptions,
BulkJobOptions,
IoredisListener,
QueueOptions,
RepeatableJob,
RepeatOptions,
} from '../interfaces';
import { FinishedStatus, JobsOptions, MinimalQueue } from '../types';
import { Job } from './job';
import { QueueGetters } from './queue-getters';
import { Repeat } from './repeat';
import { RedisConnection } from './redis-connection';
import { SpanKind, TelemetryAttributes } from '../enums';
import { JobScheduler } from './job-scheduler';
import { version } from '../version';
export interface ObliterateOpts {
/**
* Use force = true to force obliteration even with active jobs in the queue
* @defaultValue false
*/
force?: boolean;
/**
* Use count with the maximum number of deleted keys per iteration
* @defaultValue 1000
*/
count?: number;
}
export interface QueueListener<DataType, ResultType, NameType extends string>
extends IoredisListener {
/**
* Listen to 'cleaned' event.
*
* This event is triggered when the queue calls clean method.
*/
cleaned: (jobs: string[], type: string) => void;
/**
* Listen to 'error' event.
*
* This event is triggered when an error is thrown.
*/
error: (err: Error) => void;
/**
* Listen to 'paused' event.
*
* This event is triggered when the queue is paused.
*/
paused: () => void;
/**
* Listen to 'progress' event.
*
* This event is triggered when the job updates its progress.
*/
progress: (
job: Job<DataType, ResultType, NameType>,
progress: number | object,
) => void;
/**
* Listen to 'removed' event.
*
* This event is triggered when a job is removed.
*/
removed: (job: Job<DataType, ResultType, NameType>) => void;
/**
* Listen to 'resumed' event.
*
* This event is triggered when the queue is resumed.
*/
resumed: () => void;
/**
* Listen to 'waiting' event.
*
* This event is triggered when the queue creates a new job.
*/
waiting: (job: Job<DataType, ResultType, NameType>) => void;
}
/**
* Queue
*
* This class provides methods to add jobs to a queue and some other high-level
* administration such as pausing or deleting queues.
*
*/
export class Queue<
DataType = any,
ResultType = any,
NameType extends string = string,
> extends QueueGetters<DataType, ResultType, NameType> {
token = v4();
jobsOpts: BaseJobOptions;
opts: QueueOptions;
protected libName = 'bullmq';
private _repeat?: Repeat; // To be deprecated in v6 in favor of JobScheduler
private _jobScheduler?: JobScheduler;
constructor(
name: string,
opts?: QueueOptions,
Connection?: typeof RedisConnection,
) {
super(
name,
{
blockingConnection: false,
...opts,
},
Connection,
);
this.jobsOpts = opts?.defaultJobOptions ?? {};
this.waitUntilReady()
.then(client => {
if (!this.closing && !opts?.skipMetasUpdate) {
return client.hmset(this.keys.meta, this.metaValues);
}
})
.catch(err => {
// We ignore this error to avoid warnings. The error can still
// be received by listening to event 'error'
});
}
emit<U extends keyof QueueListener<DataType, ResultType, NameType>>(
event: U,
...args: Parameters<QueueListener<DataType, ResultType, NameType>[U]>
): boolean {
return super.emit(event, ...args);
}
off<U extends keyof QueueListener<DataType, ResultType, NameType>>(
eventName: U,
listener: QueueListener<DataType, ResultType, NameType>[U],
): this {
super.off(eventName, listener);
return this;
}
on<U extends keyof QueueListener<DataType, ResultType, NameType>>(
event: U,
listener: QueueListener<DataType, ResultType, NameType>[U],
): this {
super.on(event, listener);
return this;
}
once<U extends keyof QueueListener<DataType, ResultType, NameType>>(
event: U,
listener: QueueListener<DataType, ResultType, NameType>[U],
): this {
super.once(event, listener);
return this;
}
/**
* Returns this instance current default job options.
*/
get defaultJobOptions(): JobsOptions {
return { ...this.jobsOpts };
}
get metaValues(): Record<string, string | number> {
return {
'opts.maxLenEvents': this.opts?.streams?.events?.maxLen ?? 10000,
version: `${this.libName}:${version}`,
};
}
/**
* Get library version.
*
* @returns the content of the meta.library field.
*/
async getVersion(): Promise<string> {
const client = await this.client;
return await client.hget(this.keys.meta, 'version');
}
get repeat(): Promise<Repeat> {
return new Promise<Repeat>(async resolve => {
if (!this._repeat) {
this._repeat = new Repeat(this.name, {
...this.opts,
connection: await this.client,
});
this._repeat.on('error', e => this.emit.bind(this, e));
}
resolve(this._repeat);
});
}
get jobScheduler(): Promise<JobScheduler> {
return new Promise<JobScheduler>(async resolve => {
if (!this._jobScheduler) {
this._jobScheduler = new JobScheduler(this.name, {
...this.opts,
connection: await this.client,
});
this._jobScheduler.on('error', e => this.emit.bind(this, e));
}
resolve(this._jobScheduler);
});
}
/**
* Get global concurrency value.
* Returns null in case no value is set.
*/
async getGlobalConcurrency(): Promise<number | null> {
const client = await this.client;
const concurrency = await client.hget(this.keys.meta, 'concurrency');
if (concurrency) {
return Number(concurrency);
}
return null;
}
/**
* Enable and set global concurrency value.
* @param concurrency - Maximum number of simultaneous jobs that the workers can handle.
* For instance, setting this value to 1 ensures that no more than one job
* is processed at any given time. If this limit is not defined, there will be no
* restriction on the number of concurrent jobs.
*/
async setGlobalConcurrency(concurrency: number) {
const client = await this.client;
return client.hset(this.keys.meta, 'concurrency', concurrency);
}
/**
* Adds a new job to the queue.
*
* @param name - Name of the job to be added to the queue.
* @param data - Arbitrary data to append to the job.
* @param opts - Job options that affects how the job is going to be processed.
*/
async add(
name: NameType,
data: DataType,
opts?: JobsOptions,
): Promise<Job<DataType, ResultType, NameType>> {
return this.trace<Job<DataType, ResultType, NameType>>(
SpanKind.PRODUCER,
'add',
`${this.name}.${name}`,
async (span, srcPropagationMedatada) => {
if (srcPropagationMedatada) {
opts = { ...opts, telemetryMetadata: srcPropagationMedatada };
}
if (opts && opts.repeat) {
if (opts.repeat.endDate) {
if (+new Date(opts.repeat.endDate) < Date.now()) {
throw new Error(
'End date must be greater than current timestamp',
);
}
}
return (await this.repeat).updateRepeatableJob<
DataType,
ResultType,
NameType
>(name, data, { ...this.jobsOpts, ...opts }, { override: true });
} else {
const jobId = opts?.jobId;
if (jobId == '0' || jobId?.startsWith('0:')) {
throw new Error("JobId cannot be '0' or start with 0:");
}
const job = await this.Job.create<DataType, ResultType, NameType>(
this as MinimalQueue,
name,
data,
{
...this.jobsOpts,
...opts,
jobId,
},
);
this.emit('waiting', job);
span?.setAttributes({
[TelemetryAttributes.JobId]: job.id,
});
return job;
}
},
);
}
/**
* Adds an array of jobs to the queue. This method may be faster than adding
* one job at a time in a sequence.
*
* @param jobs - The array of jobs to add to the queue. Each job is defined by 3
* properties, 'name', 'data' and 'opts'. They follow the same signature as 'Queue.add'.
*/
async addBulk(
jobs: { name: NameType; data: DataType; opts?: BulkJobOptions }[],
): Promise<Job<DataType, ResultType, NameType>[]> {
return this.trace<Job<DataType, ResultType, NameType>[]>(
SpanKind.PRODUCER,
'addBulk',
this.name,
async (span, srcPropagationMedatada) => {
if (span) {
span.setAttributes({
[TelemetryAttributes.BulkNames]: jobs.map(job => job.name),
[TelemetryAttributes.BulkCount]: jobs.length,
});
}
return await this.Job.createBulk<DataType, ResultType, NameType>(
this as MinimalQueue,
jobs.map(job => ({
name: job.name,
data: job.data,
opts: {
...this.jobsOpts,
...job.opts,
jobId: job.opts?.jobId,
tm: span && srcPropagationMedatada,
},
})),
);
},
);
}
/**
* Upserts a scheduler.
*
* A scheduler is a job factory that creates jobs at a given interval.
* Upserting a scheduler will create a new job scheduler or update an existing one.
* It will also create the first job based on the repeat options and delayed accordingly.
*
* @param key - Unique key for the repeatable job meta.
* @param repeatOpts - Repeat options
* @param jobTemplate - Job template. If provided it will be used for all the jobs
* created by the scheduler.
*
* @returns The next job to be scheduled (would normally be in delayed state).
*/
async upsertJobScheduler(
jobSchedulerId: NameType,
repeatOpts: Omit<RepeatOptions, 'key'>,
jobTemplate?: {
name?: NameType;
data?: DataType;
opts?: Omit<JobsOptions, 'jobId' | 'repeat' | 'delay'>;
},
) {
if (repeatOpts.endDate) {
if (+new Date(repeatOpts.endDate) < Date.now()) {
throw new Error('End date must be greater than current timestamp');
}
}
return (await this.jobScheduler).upsertJobScheduler<
DataType,
ResultType,
NameType
>(
jobSchedulerId,
repeatOpts,
jobTemplate?.name ?? jobSchedulerId,
jobTemplate?.data ?? <DataType>{},
{ ...this.jobsOpts, ...jobTemplate?.opts },
{ override: true },
);
}
/**
* Pauses the processing of this queue globally.
*
* We use an atomic RENAME operation on the wait queue. Since
* we have blocking calls with BRPOPLPUSH on the wait queue, as long as the queue
* is renamed to 'paused', no new jobs will be processed (the current ones
* will run until finalized).
*
* Adding jobs requires a LUA script to check first if the paused list exist
* and in that case it will add it there instead of the wait list.
*/
async pause(): Promise<void> {
await this.trace<void>(SpanKind.INTERNAL, 'pause', this.name, async () => {
await this.scripts.pause(true);
this.emit('paused');
});
}
/**
* Close the queue instance.
*
*/
async close(): Promise<void> {
await this.trace<void>(SpanKind.INTERNAL, 'close', this.name, async () => {
if (!this.closing) {
if (this._repeat) {
await this._repeat.close();
}
}
await super.close();
});
}
/**
* Resumes the processing of this queue globally.
*
* The method reverses the pause operation by resuming the processing of the
* queue.
*/
async resume(): Promise<void> {
await this.trace<void>(SpanKind.INTERNAL, 'resume', this.name, async () => {
await this.scripts.pause(false);
this.emit('resumed');
});
}
/**
* Returns true if the queue is currently paused.
*/
async isPaused(): Promise<boolean> {
const client = await this.client;
const pausedKeyExists = await client.hexists(this.keys.meta, 'paused');
return pausedKeyExists === 1;
}
/**
* Returns true if the queue is currently maxed.
*/
isMaxed(): Promise<boolean> {
return this.scripts.isMaxed();
}
/**
* Get all repeatable meta jobs.
*
*
* @deprecated This method is deprecated and will be removed in v6. Use getJobSchedulers instead.
*
* @param start - Offset of first job to return.
* @param end - Offset of last job to return.
* @param asc - Determine the order in which jobs are returned based on their
* next execution time.
*/
async getRepeatableJobs(
start?: number,
end?: number,
asc?: boolean,
): Promise<RepeatableJob[]> {
return (await this.repeat).getRepeatableJobs(start, end, asc);
}
/**
* Get Job Scheduler by id
*
* @param id - identifier of scheduler.
*/
async getJobScheduler(id: string): Promise<RepeatableJob> {
return (await this.jobScheduler).getJobScheduler(id);
}
/**
* Get all Job Schedulers
*
* @param start - Offset of first scheduler to return.
* @param end - Offset of last scheduler to return.
* @param asc - Determine the order in which schedulers are returned based on their
* next execution time.
*/
async getJobSchedulers(
start?: number,
end?: number,
asc?: boolean,
): Promise<RepeatableJob[]> {
return (await this.jobScheduler).getJobSchedulers(start, end, asc);
}
/**
* Removes a repeatable job.
*
* Note: you need to use the exact same repeatOpts when deleting a repeatable job
* than when adding it.
*
* @deprecated This method is deprecated and will be removed in v6. Use removeJobScheduler instead.
*
* @see removeRepeatableByKey
*
* @param name - Job name
* @param repeatOpts -
* @param jobId -
* @returns
*/
async removeRepeatable(
name: NameType,
repeatOpts: RepeatOptions,
jobId?: string,
): Promise<boolean> {
return this.trace<boolean>(
SpanKind.INTERNAL,
'removeRepeatable',
`${this.name}.${name}`,
async span => {
span?.setAttributes({
[TelemetryAttributes.JobName]: name,
[TelemetryAttributes.JobId]: jobId,
});
const repeat = await this.repeat;
const removed = await repeat.removeRepeatable(name, repeatOpts, jobId);
return !removed;
},
);
}
/**
*
* Removes a job scheduler.
*
* @param jobSchedulerId
*
* @returns
*/
async removeJobScheduler(jobSchedulerId: string): Promise<boolean> {
const jobScheduler = await this.jobScheduler;
const removed = await jobScheduler.removeJobScheduler(jobSchedulerId);
return !removed;
}
/**
* Removes a debounce key.
* @deprecated use removeDeduplicationKey
*
* @param id - identifier
*/
async removeDebounceKey(id: string): Promise<number> {
return this.trace<number>(
SpanKind.INTERNAL,
'removeDebounceKey',
`${this.name}`,
async span => {
span?.setAttributes({
[TelemetryAttributes.JobKey]: id,
});
const client = await this.client;
return await client.del(`${this.keys.de}:${id}`);
},
);
}
/**
* Removes a deduplication key.
*
* @param id - identifier
*/
async removeDeduplicationKey(id: string): Promise<number> {
return this.trace<number>(
SpanKind.INTERNAL,
'removeDeduplicationKey',
`${this.name}`,
async span => {
span?.setAttributes({
[TelemetryAttributes.DeduplicationKey]: id,
});
const client = await this.client;
return client.del(`${this.keys.de}:${id}`);
},
);
}
/**
* Removes a repeatable job by its key. Note that the key is the one used
* to store the repeatable job metadata and not one of the job iterations
* themselves. You can use "getRepeatableJobs" in order to get the keys.
*
* @see getRepeatableJobs
*
* @deprecated This method is deprecated and will be removed in v6. Use removeJobScheduler instead.
*
* @param repeatJobKey - To the repeatable job.
* @returns
*/
async removeRepeatableByKey(key: string): Promise<boolean> {
return this.trace<boolean>(
SpanKind.INTERNAL,
'removeRepeatableByKey',
`${this.name}`,
async span => {
span?.setAttributes({
[TelemetryAttributes.JobKey]: key,
});
const repeat = await this.repeat;
const removed = await repeat.removeRepeatableByKey(key);
return !removed;
},
);
}
/**
* Removes the given job from the queue as well as all its
* dependencies.
*
* @param jobId - The id of the job to remove
* @param opts - Options to remove a job
* @returns 1 if it managed to remove the job or 0 if the job or
* any of its dependencies were locked.
*/
async remove(jobId: string, { removeChildren = true } = {}): Promise<number> {
return this.trace<number>(
SpanKind.INTERNAL,
'remove',
this.name,
async span => {
span?.setAttributes({
[TelemetryAttributes.JobId]: jobId,
[TelemetryAttributes.JobOptions]: JSON.stringify({
removeChildren,
}),
});
return await this.scripts.remove(jobId, removeChildren);
},
);
}
/**
* Updates the given job's progress.
*
* @param jobId - The id of the job to update
* @param progress - Number or object to be saved as progress.
*/
async updateJobProgress(
jobId: string,
progress: number | object,
): Promise<void> {
await this.trace<void>(
SpanKind.INTERNAL,
'updateJobProgress',
this.name,
async span => {
span?.setAttributes({
[TelemetryAttributes.JobId]: jobId,
[TelemetryAttributes.JobProgress]: JSON.stringify(progress),
});
await this.scripts.updateProgress(jobId, progress);
},
);
}
/**
* Logs one row of job's log data.
*
* @param jobId - The job id to log against.
* @param logRow - String with log data to be logged.
* @param keepLogs - Max number of log entries to keep (0 for unlimited).
*
* @returns The total number of log entries for this job so far.
*/
async addJobLog(
jobId: string,
logRow: string,
keepLogs?: number,
): Promise<number> {
return Job.addJobLog(this, jobId, logRow, keepLogs);
}
/**
* Drains the queue, i.e., removes all jobs that are waiting
* or delayed, but not active, completed or failed.
*
* @param delayed - Pass true if it should also clean the
* delayed jobs.
*/
async drain(delayed = false): Promise<void> {
await this.trace<void>(
SpanKind.INTERNAL,
'drain',
this.name,
async span => {
span?.setAttributes({
[TelemetryAttributes.QueueDrainDelay]: delayed,
});
await this.scripts.drain(delayed);
},
);
}
/**
* Cleans jobs from a queue. Similar to drain but keeps jobs within a certain
* grace period.
*
* @param grace - The grace period in milliseconds
* @param limit - Max number of jobs to clean
* @param type - The type of job to clean
* Possible values are completed, wait, active, paused, delayed, failed. Defaults to completed.
* @returns Id jobs from the deleted records
*/
async clean(
grace: number,
limit: number,
type:
| 'completed'
| 'wait'
| 'active'
| 'paused'
| 'prioritized'
| 'delayed'
| 'failed' = 'completed',
): Promise<string[]> {
return this.trace<string[]>(
SpanKind.INTERNAL,
'clean',
this.name,
async span => {
const maxCount = limit || Infinity;
const maxCountPerCall = Math.min(10000, maxCount);
const timestamp = Date.now() - grace;
let deletedCount = 0;
const deletedJobsIds: string[] = [];
while (deletedCount < maxCount) {
const jobsIds = await this.scripts.cleanJobsInSet(
type,
timestamp,
maxCountPerCall,
);
this.emit('cleaned', jobsIds, type);
deletedCount += jobsIds.length;
deletedJobsIds.push(...jobsIds);
if (jobsIds.length < maxCountPerCall) {
break;
}
}
span?.setAttributes({
[TelemetryAttributes.QueueGrace]: grace,
[TelemetryAttributes.JobType]: type,
[TelemetryAttributes.QueueCleanLimit]: maxCount,
[TelemetryAttributes.JobIds]: deletedJobsIds,
});
return deletedJobsIds;
},
);
}
/**
* Completely destroys the queue and all of its contents irreversibly.
* This method will the *pause* the queue and requires that there are no
* active jobs. It is possible to bypass this requirement, i.e. not
* having active jobs using the "force" option.
*
* Note: This operation requires to iterate on all the jobs stored in the queue
* and can be slow for very large queues.
*
* @param opts - Obliterate options.
*/
async obliterate(opts?: ObliterateOpts): Promise<void> {
await this.trace<void>(
SpanKind.INTERNAL,
'obliterate',
this.name,
async () => {
await this.pause();
let cursor = 0;
do {
cursor = await this.scripts.obliterate({
force: false,
count: 1000,
...opts,
});
} while (cursor);
},
);
}
/**
* Retry all the failed or completed jobs.
*
* @param opts: { count: number; state: FinishedStatus; timestamp: number}
* - count number to limit how many jobs will be moved to wait status per iteration,
* - state failed by default or completed.
* - timestamp from which timestamp to start moving jobs to wait status, default Date.now().
*
* @returns
*/
async retryJobs(
opts: { count?: number; state?: FinishedStatus; timestamp?: number } = {},
): Promise<void> {
await this.trace<void>(
SpanKind.PRODUCER,
'retryJobs',
this.name,
async span => {
span?.setAttributes({
[TelemetryAttributes.QueueOptions]: JSON.stringify(opts),
});
let cursor = 0;
do {
cursor = await this.scripts.retryJobs(
opts.state,
opts.count,
opts.timestamp,
);
} while (cursor);
},
);
}
/**
* Promote all the delayed jobs.
*
* @param opts: { count: number }
* - count number to limit how many jobs will be moved to wait status per iteration
*
* @returns
*/
async promoteJobs(opts: { count?: number } = {}): Promise<void> {
await this.trace<void>(
SpanKind.INTERNAL,
'promoteJobs',
this.name,
async span => {
span?.setAttributes({
[TelemetryAttributes.QueueOptions]: JSON.stringify(opts),
});
let cursor = 0;
do {
cursor = await this.scripts.promoteJobs(opts.count);
} while (cursor);
},
);
}
/**
* Trim the event stream to an approximately maxLength.
*
* @param maxLength -
*/
async trimEvents(maxLength: number): Promise<number> {
return this.trace<number>(
SpanKind.INTERNAL,
'trimEvents',
this.name,
async span => {
span?.setAttributes({
[TelemetryAttributes.QueueEventMaxLength]: maxLength,
});
const client = await this.client;
return await client.xtrim(this.keys.events, 'MAXLEN', '~', maxLength);
},
);
}
/**
* Delete old priority helper key.
*/
async removeDeprecatedPriorityKey(): Promise<number> {
const client = await this.client;
return client.del(this.toKey('priority'));
}
}