From 1b85da3f9bcc26c9be29383207bc0d554951def1 Mon Sep 17 00:00:00 2001 From: blam Date: Fri, 22 Sep 2023 16:42:02 +0200 Subject: [PATCH] chore: move metrics collection to a different place to make it simpler Signed-off-by: blam --- packages/backend-tasks/src/tasks/TaskWorker.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/backend-tasks/src/tasks/TaskWorker.ts b/packages/backend-tasks/src/tasks/TaskWorker.ts index a2ed75fef5..ecf71c1db4 100644 --- a/packages/backend-tasks/src/tasks/TaskWorker.ts +++ b/packages/backend-tasks/src/tasks/TaskWorker.ts @@ -77,13 +77,7 @@ export class TaskWorker { } while (!options?.signal?.aborted) { - const startTime = process.hrtime(); const runResult = await this.runOnce(options?.signal); - const delta = process.hrtime(startTime); - const endTime = delta[0] + delta[1] / 1e9; - - this.duration.record(endTime, { task_id: this.taskId }); - this.counter.add(1, { task_id: this.taskId, result: 'completed' }); if (runResult.result === 'abort') { break; @@ -100,7 +94,6 @@ export class TaskWorker { this.logger.warn( `Task worker failed unexpectedly, attempt number ${attemptNum}, ${e}`, ); - this.counter.add(1, { task_id: this.taskId, result: 'failed' }); await sleep(Duration.fromObject({ seconds: 1 })); } } @@ -164,10 +157,17 @@ export class TaskWorker { }, Duration.fromISO(taskSettings.timeoutAfterDuration).as('milliseconds')); try { + const startTime = process.hrtime(); await this.fn(taskAbortController.signal); + const delta = process.hrtime(startTime); + const endTime = delta[0] + delta[1] / 1e9; + + this.duration.record(endTime, { task_id: this.taskId }); + this.counter.add(1, { task_id: this.taskId, result: 'completed' }); taskAbortController.abort(); // releases resources } catch (e) { this.logger.error(e); + this.counter.add(1, { task_id: this.taskId, result: 'failed' }); await this.tryReleaseTask(ticket, taskSettings); return { result: 'failed' }; } finally {