Skip to content

Commit 3444917

Browse files
authored
fix: set maximum job timeout (#244)
1 parent 6944eb1 commit 3444917

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ const schema = Type.Object({
8282
* database.
8383
*/
8484
JOB_QUEUE_SIZE_LIMIT: Type.Number({ default: 200 }),
85+
/** Maximum time a job will run before marking it as failed. */
86+
JOB_QUEUE_TIMEOUT_MS: Type.Number({ default: 60_000 }),
8587

8688
/**
8789
* The max number of immediate attempts that will be made to retrieve metadata from external URIs

src/token-processor/queue/job/job.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { logger, stopwatch } from '@hirosystems/api-toolkit';
1+
import { logger, resolveOrTimeout, stopwatch } from '@hirosystems/api-toolkit';
22
import { ENV } from '../../../env';
33
import { PgStore } from '../../../pg/pg-store';
44
import { DbJob, DbJobInvalidReason, DbJobStatus } from '../../../pg/types';
@@ -43,8 +43,13 @@ export abstract class Job {
4343
// what to do in each case. If we choose to retry, this queue entry will simply not be marked as
4444
// `processed = true` so it can be picked up by the queue at a later time.
4545
try {
46-
await this.handler();
47-
status = DbJobStatus.done;
46+
const success = await resolveOrTimeout(this.handler(), ENV.JOB_QUEUE_TIMEOUT_MS);
47+
if (success) {
48+
status = DbJobStatus.done;
49+
} else {
50+
logger.error(`Job ${this.description()} allowed timeout exceeded`);
51+
status = DbJobStatus.failed;
52+
}
4853
} catch (error) {
4954
if (error instanceof RetryableJobError) {
5055
const retries = await this.db.increaseJobRetryCount({ id: this.job.id });
@@ -62,7 +67,7 @@ export abstract class Job {
6267
status = DbJobStatus.failed;
6368
}
6469
} else if (error instanceof UserError) {
65-
logger.error(error, `User error on Job ${this.description()}`);
70+
logger.warn(error, `User error on Job ${this.description()}`);
6671
status = DbJobStatus.invalid;
6772
invalidReason = getUserErrorInvalidReason(error);
6873
} else {

0 commit comments

Comments
 (0)