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: add job id for unlock provider #219

Merged
merged 6 commits into from
Dec 13, 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
19 changes: 6 additions & 13 deletions src/modules/crawler/job-unlock.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import { IGenerateSignature, IJobUnlockPayload, IUnlockToken } from './interface

@Injectable()
export class JobUnlockProvider {
private readonly signatureJobBackOff = 5 * 1000;
private readonly sendTxJobBackOff = 60 * 1000;
private readonly jobRemoveDueDate = 30 * 24 * 60 * 60; // 30 days in seconds
constructor(
private readonly queueService: QueueService,
private readonly configService: ConfigService,
Expand Down Expand Up @@ -144,11 +141,9 @@ export class JobUnlockProvider {
eventLogId: data.eventLogId,
},
{
attempts: 5,
removeOnComplete: {
age: this.jobRemoveDueDate,
},
backoff: this.signatureJobBackOff,
jobId: `signature-validate-${data.eventLogId}`,
removeOnComplete: true,
removeOnFail: true,
},
);
}
Expand All @@ -168,11 +163,9 @@ export class JobUnlockProvider {
eventLogId: data.eventLogId,
},
{
attempts: 5,
removeOnComplete: {
age: this.jobRemoveDueDate,
},
backoff: this.sendTxJobBackOff,
jobId: `send-unlock-${data.eventLogId}`,
removeOnComplete: true,
removeOnFail: true,
},
);
}
Expand Down
28 changes: 3 additions & 25 deletions src/shared/modules/queue/queue.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import Bull, { DoneCallback, Job, JobOptions, Queue } from 'bull';
import { DoneCallback, Job, JobOptions, Queue } from 'bull';

import { EEnvKey } from '../../../constants/env.constant.js';
import { BullLib } from '../../../shared/utils/queue.js';
Expand Down Expand Up @@ -39,29 +39,7 @@ export class QueueService {
}
});
}
public async addJobToQueue<T>(queueName: string, job: T, options: JobOptions = { attempts: 3, backoff: 5000 }) {
const queue = this.initQueueOnDemand(queueName);
if (!!options.jobId) {
const canContinue = await this.removeExistedJobIfFailed(options.jobId, queue);
if (!canContinue) {
this.logger.warn('this job is existed in queue and not in failed status');
return false;
}
}
await queue.add(job, options);
return true;
}
public async removeExistedJobIfFailed(jobId: Bull.JobId, queue: Queue): Promise<boolean> {
try {
const existedJob = await queue.getJob(jobId);
if (existedJob) {
await existedJob.remove();
return true;
}
return false;
} catch (error) {
this.logger.error(error);
return false;
}
public async addJobToQueue<T>(queueName: string, job: T, options: JobOptions) {
return this.initQueueOnDemand(queueName).add(job, options);
}
}
9 changes: 6 additions & 3 deletions src/shared/utils/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import Queue from 'bull';
import * as Redis from 'ioredis';

export class BullLib {
static createNewQueue<T>(queueName: string, redisConfig: Redis.RedisOptions): Queue.Queue<T> {
const defaultLockTime = 1 * 60 * 60 * 1000;
static createNewQueue<T>(
queueName: string,
redisConfig: Redis.RedisOptions,
lockDuration: number = 1 * 60 * 60 * 1000,
): Queue.Queue<T> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return new Queue(queueName, {
redis: redisConfig,
settings: {
lockDuration: defaultLockTime, // lock the job for one hours.
lockDuration, // lock the job for one hours.
maxStalledCount: 0,
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/shared/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export const unixZeroMinuteSecond = (value: number) => dayjs.unix(value).minute(

export const unixToDate = (value: number) => dayjs.unix(value).toDate();

export const startOfDayUnix = (date: Date) => dayjs(date).startOf('day').valueOf() / 1000;
export const startOfDayUnix = (date: Date) => Math.floor(dayjs(date).startOf('day').valueOf() / 1000);

export const endOfDayUnix = (date: Date) => dayjs(date).endOf('day').valueOf() / 1000;
export const endOfDayUnix = (date: Date) => Math.floor(dayjs(date).endOf('day').valueOf() / 1000);
export const getTimeInFutureInMinutes = (minutes: number) => dayjs(new Date()).add(minutes, 'minutes').unix();
export const getNextDayInUnix = () => dayjs().add(1, 'days').subtract(dayjs().hour()).unix();