Skip to content

Commit

Permalink
fully asyncify handleStartStepRun
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Jun 18, 2024
1 parent c9e26a0 commit 452d212
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/clients/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class Worker {
return step(context);
};

const success = (result: any) => {
const success = async (result: any) => {
this.logger.info(`Step run ${action.stepRunId} succeeded`);

try {
Expand All @@ -211,25 +211,27 @@ export class Worker {
StepActionEventType.STEP_EVENT_TYPE_COMPLETED,
result || null
);
this.client.dispatcher.sendStepActionEvent(event).catch((e) => {
this.logger.error(`Could not send completed action event: ${e.message}`);

// send a failure event
const failureEvent = this.getStepActionEvent(
action,
StepActionEventType.STEP_EVENT_TYPE_FAILED,
e.message
);

this.client.dispatcher.sendStepActionEvent(failureEvent).catch((err2) => {
this.logger.error(`Could not send failed action event: ${err2.message}`);
});
});
await this.client.dispatcher.sendStepActionEvent(event);

// delete the run from the futures
delete this.futures[action.stepRunId];
} catch (e: any) {
this.logger.error(`Could not send action event: ${e.message}`);
} catch (actionEventError: any) {
this.logger.error(`Could not send completed action event: ${actionEventError.message}`);

// send a failure event
const failureEvent = this.getStepActionEvent(
action,
StepActionEventType.STEP_EVENT_TYPE_FAILED,
actionEventError.message
);

try {
await this.client.dispatcher.sendStepActionEvent(failureEvent);
} catch (failureEventError: any) {
this.logger.error(`Could not send failed action event: ${failureEventError.message}`);
}

this.logger.error(`Could not send action event: ${actionEventError.message}`);
}
};

Expand Down Expand Up @@ -260,7 +262,17 @@ export class Worker {
}
};

const future = new HatchetPromise(run().then(success).catch(failure));
const future = new HatchetPromise(
(async () => {
try {
await run();
} catch (e: any) {
failure(e);
return;
}
await success(null);
})()
);
this.futures[action.stepRunId] = future;

// Send the action event to the dispatcher
Expand Down

0 comments on commit 452d212

Please sign in to comment.