Skip to content

Commit

Permalink
Improving queue type on processor to avoid any casting (#394)
Browse files Browse the repository at this point in the history
* Trying to improve queue type on processor

* Lint fix
  • Loading branch information
CarlosGamero authored Dec 2, 2024
1 parent 102e5d3 commit 1e852ef
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Queue, QueueOptions, Worker, WorkerOptions } from 'bullmq'
import type { BullmqProcessor, SafeJob } from '../types'

export abstract class AbstractBullmqFactory<
QueueType extends Queue<JobPayload, JobReturn>,
QueueType extends Queue<JobPayload, JobReturn, string, JobPayload, JobReturn, string>,
QueueOptionsType extends QueueOptions,
WorkerType extends Worker<JobPayload, JobReturn>,
WorkerOptionsType extends WorkerOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { AbstractBullmqFactory } from './AbstractBullmqFactory'
export class CommonBullmqFactory<JobPayload extends object, JobReturn = void>
implements
AbstractBullmqFactory<
Queue<JobPayload, JobReturn>,
Queue<JobPayload, JobReturn, string, JobPayload, JobReturn, string>,
QueueOptions,
Worker<JobPayload, JobReturn>,
WorkerOptions,
Expand All @@ -18,7 +18,10 @@ export class CommonBullmqFactory<JobPayload extends object, JobReturn = void>
JobReturn
>
{
buildQueue(queueId: string, options: QueueOptions): Queue<JobPayload, JobReturn> {
buildQueue(
queueId: string,
options: QueueOptions,
): Queue<JobPayload, JobReturn, string, JobPayload, JobReturn, string> {
return new Queue(queueId, options)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ export abstract class AbstractBackgroundJobProcessor<
JobReturn = void,
ExecutionContext = undefined,
JobType extends SafeJob<JobPayload, JobReturn> = Job<JobPayload, JobReturn>,
QueueType extends Queue<JobPayload, JobReturn> = Queue<JobPayload, JobReturn>,
QueueType extends Queue<JobPayload, JobReturn, string, JobPayload, JobReturn, string> = Queue<
JobPayload,
JobReturn,
string,
JobPayload,
JobReturn,
string
>,
QueueOptionsType extends QueueOptions = QueueOptions,
WorkerType extends Worker<JobPayload, JobReturn> = Worker<JobPayload, JobReturn>,
WorkerOptionsType extends WorkerOptions = WorkerOptions,
Expand Down Expand Up @@ -238,19 +245,12 @@ export abstract class AbstractBackgroundJobProcessor<
await this.startIfNotStarted()

const job = await this._queue?.add(
// biome-ignore lint/suspicious/noExplicitAny: it's okay
this.config.queueId as any,
// biome-ignore lint/suspicious/noExplicitAny: it's okay
jobData as any,
this.config.queueId,
jobData,
prepareJobOptions(this.config.isTest, options),
)
if (!job?.id) {
throw new Error('Scheduled job ID is undefined')
}

if (this._spy) {
this._spy.addJob(job, 'scheduled')
}
if (!job?.id) throw new Error('Scheduled job ID is undefined')
if (this._spy) this._spy.addJob(job, 'scheduled')

return job.id
}
Expand All @@ -264,10 +264,8 @@ export abstract class AbstractBackgroundJobProcessor<
const jobs =
(await this._queue?.addBulk(
jobData.map((data) => ({
// biome-ignore lint/suspicious/noExplicitAny: bull expects ExtractNameType<JobPayload>, but ExtractNameType is not exposed
name: this.config.queueId as any,
// biome-ignore lint/suspicious/noExplicitAny: bull expects ExtractDataType<JobPayload, JobPayload>, but ExtractDataType is not exposed
data: data as any,
name: this.config.queueId,
data: data,
opts: prepareJobOptions(this.config.isTest, options),
})),
)) ?? []
Expand All @@ -279,10 +277,7 @@ export abstract class AbstractBackgroundJobProcessor<
// stating that it could return undefined.
throw new Error('Some scheduled job IDs are undefined')
}

if (this._spy) {
this._spy.addJobs(jobs, 'scheduled')
}
if (this._spy) this._spy.addJobs(jobs, 'scheduled')

return jobIds as string[]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ export type BackgroundJobProcessorDependencies<
JobPayload extends object,
JobReturn = void,
JobType extends SafeJob<JobPayload, JobReturn> = Job<JobPayload, JobReturn>,
QueueType extends Queue<JobPayload, JobReturn> = Queue<JobPayload, JobReturn>,
QueueType extends Queue<JobPayload, JobReturn, string, JobPayload, JobReturn, string> = Queue<
JobPayload,
JobReturn,
string,
JobPayload,
JobReturn
>,
QueueOptionsType extends QueueOptions = QueueOptions,
WorkerType extends Worker<JobPayload, JobReturn> = Worker<JobPayload, JobReturn>,
WorkerOptionsType extends WorkerOptions = WorkerOptions,
Expand Down

0 comments on commit 1e852ef

Please sign in to comment.