@lokalise/background-jobs-common
Advanced tools
Comparing version 3.4.0 to 3.5.0
@@ -133,8 +133,8 @@ "use strict"; | ||
await this.startIfNotStarted(); | ||
const jobs = await this.queue?.addBulk(jobData.map((data) => ({ | ||
const jobs = (await this.queue?.addBulk(jobData.map((data) => ({ | ||
name: this.config.queueId, | ||
data, | ||
opts: this.prepareJobOptions(options ?? {}), | ||
}))); | ||
const jobIds = jobs?.map((job) => job.id) ?? []; | ||
})))) ?? []; | ||
const jobIds = jobs.map((job) => job.id); | ||
/* v8 ignore next 3 */ | ||
@@ -146,2 +146,7 @@ if (jobIds.length === 0 || !jobIds.every((id) => !!id)) { | ||
} | ||
if (this._spy) { | ||
for (const job of jobs) { | ||
this._spy.addJob(job, 'scheduled'); | ||
} | ||
} | ||
return jobIds; | ||
@@ -218,3 +223,3 @@ } | ||
} | ||
this._spy?.addJobProcessingResult(job, 'completed'); // this should be executed before the hook to not be affected by it | ||
this._spy?.addJob(job, 'completed'); // this should be executed before the hook to not be affected by it | ||
await this.internalOnHook(job, job.requestContext, async (job, requestContext) => await this.onSuccess(job, requestContext)); | ||
@@ -239,3 +244,3 @@ } | ||
await this.internalOnHook(job, job.requestContext, async (job, requestContext) => this.onFailed(job, error, requestContext)); | ||
this._spy?.addJobProcessingResult(job, 'failed'); | ||
this._spy?.addJob(job, 'failed'); | ||
} | ||
@@ -242,0 +247,0 @@ } |
@@ -1,11 +0,12 @@ | ||
import type { JobFinalState, SafeJob } from '../types'; | ||
import type { BackgroundJobProcessorSpyInterface, JobSpyResult, JobDataSelector } from './types'; | ||
import type { SafeJob } from '../types'; | ||
import type { BackgroundJobProcessorSpyInterface, JobSpyResult, JobDataSelector, JobSpyState } from './types'; | ||
export declare class BackgroundJobProcessorSpy<JobData extends object, JobReturn> implements BackgroundJobProcessorSpyInterface<JobData, JobReturn> { | ||
private readonly jobProcessingResults; | ||
private readonly jobResults; | ||
private promises; | ||
constructor(); | ||
clear(): void; | ||
waitForJobWithId(id: string | undefined, awaitedState: JobFinalState): Promise<JobSpyResult<JobData, JobReturn>>; | ||
waitForJob(jobSelector: JobDataSelector<JobData>, awaitedState: JobFinalState): Promise<JobSpyResult<JobData, JobReturn>>; | ||
waitForJobWithId(id: string | undefined, awaitedState: JobSpyState): Promise<JobSpyResult<JobData, JobReturn>>; | ||
waitForJob(jobSelector: JobDataSelector<JobData>, awaitedState: JobSpyState): Promise<JobSpyResult<JobData, JobReturn>>; | ||
private registerPromise; | ||
private getJobResultKey; | ||
/** | ||
@@ -22,3 +23,3 @@ * Adds a job processing result and resolves any promises waiting for a matching job in the given final state. | ||
*/ | ||
addJobProcessingResult(job: SafeJob<JobData>, state: JobFinalState): void; | ||
addJob(job: SafeJob<JobData>, state: JobSpyState): void; | ||
} |
@@ -6,27 +6,24 @@ "use strict"; | ||
class BackgroundJobProcessorSpy { | ||
jobProcessingResults; | ||
jobResults; | ||
promises; | ||
constructor() { | ||
this.jobProcessingResults = new Map(); | ||
this.jobResults = new Map(); | ||
this.promises = []; | ||
} | ||
clear() { | ||
this.jobProcessingResults.clear(); | ||
this.jobResults.clear(); | ||
this.promises = []; | ||
} | ||
waitForJobWithId(id, awaitedState) { | ||
if (!id) { | ||
if (!id) | ||
throw new Error('Job id is not defined or empty'); | ||
} | ||
const result = this.jobProcessingResults.get(id); | ||
if (result && result.state === awaitedState) { | ||
const result = this.jobResults.get(this.getJobResultKey(id, awaitedState)); | ||
if (result && result.state === awaitedState) | ||
return Promise.resolve(result.job); | ||
} | ||
return this.registerPromise((job) => job.id === id, awaitedState); | ||
} | ||
waitForJob(jobSelector, awaitedState) { | ||
const result = Array.from(this.jobProcessingResults.values()).find((spy) => jobSelector(spy.job.data) && spy.state === awaitedState); | ||
if (result) { | ||
const result = Array.from(this.jobResults.values()).find((spy) => jobSelector(spy.job.data) && spy.state === awaitedState); | ||
if (result) | ||
return Promise.resolve(result.job); | ||
} | ||
return this.registerPromise((job) => jobSelector(job.data), awaitedState); | ||
@@ -43,2 +40,5 @@ } | ||
} | ||
getJobResultKey(jobId, state) { | ||
return state === 'failed' || state === 'completed' ? `${jobId}#final` : `${jobId}#${state}`; | ||
} | ||
/** | ||
@@ -55,7 +55,7 @@ * Adds a job processing result and resolves any promises waiting for a matching job in the given final state. | ||
*/ | ||
addJobProcessingResult(job, state) { | ||
addJob(job, state) { | ||
if (!job.id) | ||
return; | ||
const clonedJob = { ...job, data: (0, node_core_1.deepClone)(job.data) }; | ||
this.jobProcessingResults.set(job.id, { job: clonedJob, state }); | ||
this.jobResults.set(this.getJobResultKey(job.id, state), { job: clonedJob, state }); | ||
if (this.promises.length === 0) | ||
@@ -62,0 +62,0 @@ return; |
import type { Job } from 'bullmq'; | ||
import type { JobFinalState } from '../types'; | ||
export type JobSpyState = JobFinalState | 'scheduled'; | ||
export type JobDataSelector<JobData extends object> = (jobData: JobData) => boolean; | ||
@@ -7,4 +8,4 @@ export type JobSpyResult<JobData extends object, jobReturn> = Pick<Job<JobData, jobReturn>, 'data' | 'attemptsMade' | 'id' | 'progress' | 'returnvalue' | 'failedReason' | 'finishedOn'>; | ||
clear(): void; | ||
waitForJob(jobSelector: JobDataSelector<JobData>, state: JobFinalState): Promise<JobSpyResult<JobData, jobReturn>>; | ||
waitForJobWithId(id: string | undefined, awaitedState: JobFinalState): Promise<JobSpyResult<JobData, jobReturn>>; | ||
waitForJob(jobSelector: JobDataSelector<JobData>, state: JobSpyState): Promise<JobSpyResult<JobData, jobReturn>>; | ||
waitForJobWithId(id: string | undefined, awaitedState: JobSpyState): Promise<JobSpyResult<JobData, jobReturn>>; | ||
} |
{ | ||
"name": "@lokalise/background-jobs-common", | ||
"version": "3.4.0", | ||
"version": "3.5.0", | ||
"files": [ | ||
@@ -37,5 +37,5 @@ "dist", | ||
"dependencies": { | ||
"@lokalise/id-utils": "1.0.0", | ||
"@lokalise/node-core": "^10.0.0", | ||
"pino": "^9.1.0", | ||
"@lokalise/id-utils": "^2.1.0", | ||
"@lokalise/node-core": "^10.0.1", | ||
"pino": "^9.2.0", | ||
"ts-deepmerge": "^7.0.0" | ||
@@ -47,5 +47,5 @@ }, | ||
"devDependencies": { | ||
"@types/node": "^20.14.2", | ||
"@types/node": "^20.14.8", | ||
"@lokalise/eslint-config": "latest", | ||
"@lokalise/fastify-extras": "^21.0.0", | ||
"@lokalise/fastify-extras": "^21.2.2", | ||
"@lokalise/prettier-config": "latest", | ||
@@ -56,5 +56,5 @@ "@lokalise/package-vite-config": "latest", | ||
"ioredis": "^5.4.1", | ||
"prettier": "3.3.1", | ||
"prettier": "3.3.2", | ||
"rimraf": "^5.0.7", | ||
"typescript": "5.4.5", | ||
"typescript": "5.5.2", | ||
"vitest": "^1.6.0" | ||
@@ -61,0 +61,0 @@ }, |
40244
814
+ Added@lokalise/id-utils@2.2.0(transitive)
+ Addeduuidv7@1.0.2(transitive)
- Removed@lokalise/id-utils@1.0.0(transitive)
Updated@lokalise/id-utils@^2.1.0
Updated@lokalise/node-core@^10.0.1
Updatedpino@^9.2.0