From e2ef40f3e6c46bef1d83070f231b309df8e2a513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uro=C5=A1=20Marolt?= Date: Tue, 4 Jul 2023 14:29:32 +0200 Subject: [PATCH] use previous oldest data for the new query so we don't loop over the same rows --- backend/src/bin/jobs/checkStuckIntegrationRuns.ts | 3 +++ .../repositories/integrationRunRepository.ts | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/src/bin/jobs/checkStuckIntegrationRuns.ts b/backend/src/bin/jobs/checkStuckIntegrationRuns.ts index ba87950492..55a6fd524e 100644 --- a/backend/src/bin/jobs/checkStuckIntegrationRuns.ts +++ b/backend/src/bin/jobs/checkStuckIntegrationRuns.ts @@ -209,10 +209,13 @@ export const checkRuns = async (): Promise => { } } + const lastCreatedAt = runs[runs.length - 1].createdAt + runs = await runsRepo.findIntegrationsByState( [IntegrationRunState.PENDING, IntegrationRunState.PROCESSING], 1, 10, + lastCreatedAt, ) } } diff --git a/backend/src/database/repositories/integrationRunRepository.ts b/backend/src/database/repositories/integrationRunRepository.ts index 51db5d843d..9c5ad3a8a0 100644 --- a/backend/src/database/repositories/integrationRunRepository.ts +++ b/backend/src/database/repositories/integrationRunRepository.ts @@ -59,6 +59,7 @@ export default class IntegrationRunRepository extends RepositoryBase< states: IntegrationRunState[], page: number, perPage: number, + lastCreatedAt?: string, ): Promise { const seq = this.seq @@ -69,6 +70,17 @@ export default class IntegrationRunRepository extends RepositoryBase< return `:state${index}` }) + const conditions = [] + if (lastCreatedAt) { + replacements.lastCreatedAt = lastCreatedAt + conditions.push('"createdAt" < :lastCreatedAt') + } + + let conditionString = '' + if (conditions.length > 0) { + conditionString = ` and ${conditions.join(' and ')}` + } + const query = ` select id, "tenantId", @@ -82,7 +94,7 @@ export default class IntegrationRunRepository extends RepositoryBase< "createdAt", "updatedAt" from "integrationRuns" - where state in (${stateParams.join(', ')}) + where state in (${stateParams.join(', ')}) ${conditionString} order by "createdAt" desc limit ${perPage} offset ${(page - 1) * perPage} `