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(job): use generic types for static methods #975

Merged
merged 1 commit into from
Dec 31, 2021
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
22 changes: 16 additions & 6 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class Job<
data: T;
opts?: BulkJobOptions;
}[],
) {
): Promise<Job<T, R, N>[]> {
const client = await queue.client;

const jobInstances = jobs.map(
Expand Down Expand Up @@ -251,11 +251,21 @@ export class Job<
* @param jobId - an optional job id (overrides the id coming from the JSON object)
* @returns
*/
static fromJSON(queue: MinimalQueue, json: JobJsonRaw, jobId?: string): Job {
static fromJSON<T = any, R = any, N extends string = string>(
queue: MinimalQueue,
json: JobJsonRaw,
jobId?: string,
): Job<T, R, N> {
const data = JSON.parse(json.data || '{}');
const opts = JSON.parse(json.opts || '{}');

const job = new this(queue, json.name, data, opts, json.id || jobId);
const job = new this<T, R, N>(
queue,
json.name as N,
data,
opts,
json.id || jobId,
);

job.progress = JSON.parse(json.progress || '0');

Expand Down Expand Up @@ -297,17 +307,17 @@ export class Job<
* @param jobId - the job id.
* @returns
*/
static async fromId(
static async fromId<T = any, R = any, N extends string = string>(
queue: MinimalQueue,
jobId: string,
): Promise<Job | undefined> {
): Promise<Job<T, R, N> | undefined> {
// jobId can be undefined if moveJob returns undefined
if (jobId) {
const client = await queue.client;
const jobData = await client.hgetall(queue.toKey(jobId));
return isEmpty(jobData)
? undefined
: Job.fromJSON(queue, (<unknown>jobData) as JobJsonRaw, jobId);
: Job.fromJSON<T, R, N>(queue, (<unknown>jobData) as JobJsonRaw, jobId);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/classes/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ export class Queue<
*/
async addBulk(
jobs: { name: NameType; data: DataType; opts?: BulkJobOptions }[],
) {
return Job.createBulk(
): Promise<Job<DataType, DataType, NameType>[]> {
return Job.createBulk<DataType, DataType, NameType>(
this,
jobs.map(job => ({
name: job.name,
Expand Down
51 changes: 22 additions & 29 deletions src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,20 @@ export class Scripts {
) {
const queueKeys = queue.keys;

const keys = [
queueKeys.repeat,
queueKeys.delayed,
];
const keys = [queueKeys.repeat, queueKeys.delayed];

const args = [
repeatJobId,
repeatJobKey,
queueKeys[''],
];
const args = [repeatJobId, repeatJobKey, queueKeys['']];

return keys.concat(args);
}

static async removeRepeatable(queue: MinimalQueue, repeatJobId: string, repeatJobKey: string): Promise<void> {
static async removeRepeatable(
queue: MinimalQueue,
repeatJobId: string,
repeatJobKey: string,
): Promise<void> {
const client = await queue.client;
const args = this.removeRepeatableArgs(
queue,
repeatJobId,
repeatJobKey
);
const args = this.removeRepeatableArgs(queue, repeatJobId, repeatJobKey);

return (<any>client).removeRepeatable(args);
}
Expand Down Expand Up @@ -691,10 +684,10 @@ export class Scripts {
return raw2jobData(result);
}

//
// It checks if the job in the top of the delay set should be moved back to the
// top of the wait queue (so that it will be processed as soon as possible)
//
/**
* It checks if the job in the top of the delay set should be moved back to the
* top of the wait queue (so that it will be processed as soon as possible)
*/
static async updateDelaySet(queue: MinimalQueue, delayedTimestamp: number) {
const client = await queue.client;

Expand All @@ -713,7 +706,7 @@ export class Scripts {
return (<any>client).updateDelaySet(keys.concat(args));
}

static async promote(queue: MinimalQueue, jobId: string) {
static async promote(queue: MinimalQueue, jobId: string): Promise<number> {
const client = await queue.client;

const keys = [
Expand All @@ -729,15 +722,15 @@ export class Scripts {
return (<any>client).promote(keys.concat(args));
}

//
// Looks for unlocked jobs in the active queue.
//
// The job was being worked on, but the worker process died and it failed to renew the lock.
// We call these jobs 'stalled'. This is the most common case. We resolve these by moving them
// back to wait to be re-processed. To prevent jobs from cycling endlessly between active and wait,
// (e.g. if the job handler keeps crashing),
// we limit the number stalled job recoveries to settings.maxStalledCount.
//
/**
* Looks for unlocked jobs in the active queue.
*
* The job was being worked on, but the worker process died and it failed to renew the lock.
* We call these jobs 'stalled'. This is the most common case. We resolve these by moving them
* back to wait to be re-processed. To prevent jobs from cycling endlessly between active and wait,
* (e.g. if the job handler keeps crashing),
* we limit the number stalled job recoveries to settings.maxStalledCount.
*/
static async moveStalledJobsToWait(queue: QueueScheduler) {
const client = await queue.client;

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"paths": {
"@src/*": ["src/*"]
},
"lib": ["esnext", "dom"]
"lib": ["esnext"]
},
"include": ["src"],
"exclude": ["node_modules", "dist", "tests/*"]
Expand Down