diff --git a/packages/server/modules/backgroundjobs/repositories/backgroundjobs.ts b/packages/server/modules/backgroundjobs/repositories/backgroundjobs.ts index 2243f2ea2..1d934f2f9 100644 --- a/packages/server/modules/backgroundjobs/repositories/backgroundjobs.ts +++ b/packages/server/modules/backgroundjobs/repositories/backgroundjobs.ts @@ -65,20 +65,37 @@ export const failBackgroundJobsWhichExceedMaximumAttemptsOrNoRemainingComputeBud .backgroundJobs(db) .where(BackgroundJobs.withoutTablePrefix.col.originServerUrl, originServerUrl) .andWhere(BackgroundJobs.withoutTablePrefix.col.jobType, jobType) - .whereIn(BackgroundJobs.withoutTablePrefix.col.status, [ - BackgroundJobStatus.Queued, - BackgroundJobStatus.Processing - ]) .andWhere(function () { - this.where( - BackgroundJobs.withoutTablePrefix.col.attempt, - '>', // greater than because processing jobs may currently equal maxAttempt and still be running - db.raw('"maxAttempt"') // camelCase requires the column name to be wrapped in double quotes - ).orWhere( - BackgroundJobs.withoutTablePrefix.col.remainingComputeBudgetSeconds, - '<=', - 0 - ) + this.where(function () { + this.where( + BackgroundJobs.withoutTablePrefix.col.status, + BackgroundJobStatus.Processing + ).andWhere( + BackgroundJobs.withoutTablePrefix.col.attempt, + '>', // greater than because processing jobs may currently equal maxAttempt and still be running + db.raw('"maxAttempt"') // camelCase requires the column name to be wrapped in double quotes + ) + }) + .orWhere(function () { + this.where( + BackgroundJobs.withoutTablePrefix.col.status, + BackgroundJobStatus.Queued + ).andWhere( + BackgroundJobs.withoutTablePrefix.col.attempt, + '>=', // greater or equal than because queued jobs cannot be picked up by a worker when they reach maxAttempt + db.raw('"maxAttempt"') // camelCase requires the column name to be wrapped in double quotes + ) + }) + .orWhere(function () { + this.whereIn(BackgroundJobs.withoutTablePrefix.col.status, [ + BackgroundJobStatus.Queued, + BackgroundJobStatus.Processing + ]).where( + BackgroundJobs.withoutTablePrefix.col.remainingComputeBudgetSeconds, + '<=', + 0 + ) + }) }) .update({ [BackgroundJobs.withoutTablePrefix.col.status]: BackgroundJobStatus.Failed diff --git a/packages/server/modules/backgroundjobs/tests/integration/repositories.spec.ts b/packages/server/modules/backgroundjobs/tests/integration/repositories.spec.ts index d6f37c40c..82cf429ed 100644 --- a/packages/server/modules/backgroundjobs/tests/integration/repositories.spec.ts +++ b/packages/server/modules/backgroundjobs/tests/integration/repositories.spec.ts @@ -174,10 +174,10 @@ describe('Background Jobs repositories @backgroundjobs', () => { }) describe('failQueuedBackgroundJobsWhichExceedMaximumAttemptsOrNoRemainingComputeBudgetFactory', () => { - it('should fail queued background jobs that exceed maximum attempts', async () => { + it('should fail queued background jobs that meet or exceed maximum attempts', async () => { const job = createTestJob({ status: BackgroundJobStatus.Queued, - attempt: 3, + attempt: 2, maxAttempt: 2 }) await storeBackgroundJob({ job }) diff --git a/packages/server/modules/fileuploads/tests/integration/tasks.spec.ts b/packages/server/modules/fileuploads/tests/integration/tasks.spec.ts index a45864e49..189330a82 100644 --- a/packages/server/modules/fileuploads/tests/integration/tasks.spec.ts +++ b/packages/server/modules/fileuploads/tests/integration/tasks.spec.ts @@ -179,7 +179,7 @@ describe('File import garbage collection @fileuploads integration', () => { testData: identifiableData, blobId: fileTwo.fileId }), - attempt: 4, + attempt: 3, maxAttempt: 3 }) await saveUploadFile(fileOne)