Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving queue type on processor to avoid casting #394

Merged
merged 3 commits into from
Dec 2, 2024
Merged
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
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
>,
Comment on lines -44 to +51
Copy link
Collaborator Author

@CarlosGamero CarlosGamero Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As JobPayload is not defined at this stage, TS can't infer NameType

export declare class Queue<..., DefaultNameType extends string = string, ..., NameType extends string = ExtractNameType<DataTypeOrJob, DefaultNameType>> {

type ExtractNameType<DataTypeOrJob, Default extends string> = DataTypeOrJob extends Job<any, any, infer N> ? N : Default;

So trying to define it explicitly to help TS

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
Loading