Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make aggregation retries more granular #14

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions src/aggregation/aggregation-tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,14 @@ export class AggregationTasksService {
if (!this.aggregationJobInProgress) {
this.aggregationJobInProgress = true;

let success = false;
let executionNumber = 0;
//retry up to 3 times in case of error
while (!success && executionNumber < 3) {
try {
this.logger.debug('Starting Aggregations');
try {
this.logger.debug('Starting Aggregations');

await this.aggregationService.runAggregations();
success = true;
await this.aggregationService.runAggregations();

this.logger.debug('Finished Aggregations');
} catch (err) {
this.logger.error(
`Error during Aggregations job, execution ${executionNumber}: ${err}`,
);
executionNumber++;
}
this.logger.debug('Finished Aggregations');
} catch (err) {
this.logger.error(`Error during Aggregations job: ${err}`);
}

this.aggregationJobInProgress = false;
Expand Down
35 changes: 31 additions & 4 deletions src/aggregation/aggregation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ export class AggregationService {
private readonly aggregationRunners: AggregationRunner[],
) {}

async executeWithRetries(maxTries: number, fn: () => Promise<void>) {
let success = false;
let executionNumber = 0;
let lastErr;
while (!success && executionNumber < maxTries) {
try {
await fn();
success = true;
} catch (err) {
lastErr = err;
this.logger.warn(
`Error during Aggregations job, execution ${executionNumber}/${maxTries-1}: ${err}`,
);
if (executionNumber != maxTries) {
this.logger.warn(`Sleeping for 30s before retrying`);
await new Promise((resolve) => setTimeout(resolve, 30000));
}
executionNumber++;
}
}
if (!success) {
throw lastErr;
}
}

async runAggregations() {
const filledTables: AggregationTable[] = [];
const pendingAggregationRunners = Object.assign(
Expand All @@ -36,10 +61,12 @@ export class AggregationService {
// execute runner
this.logger.debug(`STARTING: ${aggregationRunner.getName()}`);

await aggregationRunner.run(
this.prismaService,
this.prismaDmobService,
this.filSparkService,
await this.executeWithRetries(3, () =>
aggregationRunner.run(
this.prismaService,
this.prismaDmobService,
this.filSparkService,
),
);
this.logger.debug(`FINISHED: ${aggregationRunner.getName()}`);
executedRunners++;
Expand Down