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

feat: improve queue getters to use generic job type #2905

Merged
merged 2 commits into from
Nov 14, 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
58 changes: 12 additions & 46 deletions src/classes/queue-getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@ import { JobJsonRaw, Metrics } from '../interfaces';
*
* @description Provides different getters for different aspects of a queue.
*/
export class QueueGetters<
DataType,
ResultType,
NameType extends string,
> extends QueueBase {
getJob(
jobId: string,
): Promise<Job<DataType, ResultType, NameType> | undefined> {
return this.Job.fromId(this, jobId) as Promise<
Job<DataType, ResultType, NameType>
>;
export class QueueGetters<JobBase extends Job = Job> extends QueueBase {
getJob(jobId: string): Promise<JobBase | undefined> {
return this.Job.fromId(this, jobId) as Promise<JobBase>;
}

private commandByType(
Expand Down Expand Up @@ -251,10 +243,7 @@ export class QueueGetters<
* @param start - zero based index from where to start returning jobs.
* @param end - zero based index where to stop returning jobs.
*/
getWaiting(
start = 0,
end = -1,
): Promise<Job<DataType, ResultType, NameType>[]> {
getWaiting(start = 0, end = -1): Promise<JobBase[]> {
return this.getJobs(['waiting'], start, end, true);
}

Expand All @@ -264,10 +253,7 @@ export class QueueGetters<
* @param start - zero based index from where to start returning jobs.
* @param end - zero based index where to stop returning jobs.
*/
getWaitingChildren(
start = 0,
end = -1,
): Promise<Job<DataType, ResultType, NameType>[]> {
getWaitingChildren(start = 0, end = -1): Promise<JobBase[]> {
return this.getJobs(['waiting-children'], start, end, true);
}

Expand All @@ -276,10 +262,7 @@ export class QueueGetters<
* @param start - zero based index from where to start returning jobs.
* @param end - zero based index where to stop returning jobs.
*/
getActive(
start = 0,
end = -1,
): Promise<Job<DataType, ResultType, NameType>[]> {
getActive(start = 0, end = -1): Promise<JobBase[]> {
return this.getJobs(['active'], start, end, true);
}

Expand All @@ -288,10 +271,7 @@ export class QueueGetters<
* @param start - zero based index from where to start returning jobs.
* @param end - zero based index where to stop returning jobs.
*/
getDelayed(
start = 0,
end = -1,
): Promise<Job<DataType, ResultType, NameType>[]> {
getDelayed(start = 0, end = -1): Promise<JobBase[]> {
return this.getJobs(['delayed'], start, end, true);
}

Expand All @@ -300,10 +280,7 @@ export class QueueGetters<
* @param start - zero based index from where to start returning jobs.
* @param end - zero based index where to stop returning jobs.
*/
getPrioritized(
start = 0,
end = -1,
): Promise<Job<DataType, ResultType, NameType>[]> {
getPrioritized(start = 0, end = -1): Promise<JobBase[]> {
return this.getJobs(['prioritized'], start, end, true);
}

Expand All @@ -312,10 +289,7 @@ export class QueueGetters<
* @param start - zero based index from where to start returning jobs.
* @param end - zero based index where to stop returning jobs.
*/
getCompleted(
start = 0,
end = -1,
): Promise<Job<DataType, ResultType, NameType>[]> {
getCompleted(start = 0, end = -1): Promise<JobBase[]> {
return this.getJobs(['completed'], start, end, false);
}

Expand All @@ -324,10 +298,7 @@ export class QueueGetters<
* @param start - zero based index from where to start returning jobs.
* @param end - zero based index where to stop returning jobs.
*/
getFailed(
start = 0,
end = -1,
): Promise<Job<DataType, ResultType, NameType>[]> {
getFailed(start = 0, end = -1): Promise<JobBase[]> {
return this.getJobs(['failed'], start, end, false);
}

Expand Down Expand Up @@ -422,18 +393,13 @@ export class QueueGetters<
start = 0,
end = -1,
asc = false,
): Promise<Job<DataType, ResultType, NameType>[]> {
): Promise<JobBase[]> {
const currentTypes = this.sanitizeJobTypes(types);

const jobIds = await this.getRanges(currentTypes, start, end, asc);

return Promise.all(
jobIds.map(
jobId =>
this.Job.fromId(this, jobId) as Promise<
Job<DataType, ResultType, NameType>
>,
),
jobIds.map(jobId => this.Job.fromId(this, jobId) as Promise<JobBase>),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/classes/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class Queue<
DataType = any,
ResultType = any,
NameType extends string = string,
> extends QueueGetters<DataType, ResultType, NameType> {
> extends QueueGetters<Job<DataType, ResultType, NameType>> {
token = v4();
jobsOpts: BaseJobOptions;
opts: QueueOptions;
Expand Down
Loading