Skip to content

Commit

Permalink
fix: prevent error silencing if refreshing activity/allocation fails …
Browse files Browse the repository at this point in the history
…during another action
  • Loading branch information
SewerynKras committed Jun 19, 2024
1 parent 47742cd commit 15aa074
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
50 changes: 42 additions & 8 deletions src/activity/activity.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,23 @@ export class ActivityModuleImpl implements ActivityModule {
try {
this.events.emit("scriptSent", activity, script);
const result = await this.activityApi.executeScript(activity, script);
this.events.emit("scriptExecuted", await this.refreshActivity(activity).catch(() => activity), script, result);
this.events.emit(
"scriptExecuted",
await this.refreshActivity(activity).catch(() => {
this.logger.warn("Failed to refresh activity after script execution", { activityId: activity.id });
return activity;
}),
script,
result,
);
return result;
} catch (error) {
this.events.emit(
"errorExecutingScript",
await this.refreshActivity(activity).catch(() => activity),
await this.refreshActivity(activity).catch(() => {
this.logger.warn("Failed to refresh activity after script execution error", { activityId: activity.id });
return activity;
}),
script,
error,
);
Expand All @@ -150,15 +161,21 @@ export class ActivityModuleImpl implements ActivityModule {
const results = await this.activityApi.getExecBatchResults(activity, batchId, commandIndex, timeout);
this.events.emit(
"batchResultsReceived",
await this.refreshActivity(activity).catch(() => activity),
await this.refreshActivity(activity).catch(() => {
this.logger.warn("Failed to refresh activity after batch results received", { activityId: activity.id });
return activity;
}),
batchId,
results,
);
return results;
} catch (error) {
this.events.emit(
"errorGettingBatchResults",
await this.refreshActivity(activity).catch(() => activity),
await this.refreshActivity(activity).catch(() => {
this.logger.warn("Failed to refresh activity after batch results error", { activityId: activity.id });
return activity;
}),
batchId,
error,
);
Expand All @@ -175,15 +192,21 @@ export class ActivityModuleImpl implements ActivityModule {
tap(async (event) => {
this.events.emit(
"batchEventsReceived",
await this.refreshActivity(activity).catch(() => activity),
await this.refreshActivity(activity).catch(() => {
this.logger.warn("Failed to refresh activity after batch events received", { activityId: activity.id });
return activity;
}),
batchId,
event,
);
}),
catchError(async (error) => {
this.events.emit(
"errorGettingBatchEvents",
await this.refreshActivity(activity).catch(() => activity),
await this.refreshActivity(activity).catch(() => {
this.logger.warn("Failed to refresh activity after batch events error", { activityId: activity.id });
return activity;
}),
batchId,
error,
);
Expand Down Expand Up @@ -262,12 +285,23 @@ export class ActivityModuleImpl implements ActivityModule {
this.logger.debug("Initializing the exe-unit for activity", { activityId: activity.id });
try {
await ctx.before();
this.events.emit("workContextInitialized", await this.refreshActivity(activity).catch(() => activity));
this.events.emit(
"workContextInitialized",
await this.refreshActivity(activity).catch(() => {
this.logger.warn("Failed to refresh activity after work context initialization", { activityId: activity.id });
return activity;
}),
);
return ctx;
} catch (error) {
this.events.emit(
"errorInitializingWorkContext",
await this.refreshActivity(activity).catch(() => activity),
await this.refreshActivity(activity).catch(() => {
this.logger.warn("Failed to refresh activity after work context initialization error", {
activityId: activity.id,
});
return activity;
}),
error,
);
throw error;
Expand Down
10 changes: 8 additions & 2 deletions src/payment/payment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,19 @@ export class PaymentModuleImpl implements PaymentModule {
async releaseAllocation(allocation: Allocation): Promise<void> {
this.logger.info("Releasing allocation", { id: allocation.id });
try {
const lastKnownAllocationState = await this.getAllocation(allocation.id);
const lastKnownAllocationState = await this.getAllocation(allocation.id).catch(() => {
this.logger.warn("Failed to fetch allocation before releasing", { id: allocation.id });
return allocation;
});
await this.paymentApi.releaseAllocation(allocation);
this.events.emit("allocationReleased", lastKnownAllocationState);
} catch (error) {
this.events.emit(
"errorReleasingAllocation",
await this.paymentApi.getAllocation(allocation.id).catch(() => allocation),
await this.paymentApi.getAllocation(allocation.id).catch(() => {
this.logger.warn("Failed to fetch allocation after failed release attempt", { id: allocation.id });
return allocation;
}),
error,
);
throw error;
Expand Down

0 comments on commit 15aa074

Please sign in to comment.