Skip to content

Commit

Permalink
feat: task executor emits lifecycle events through an EventEmitter
Browse files Browse the repository at this point in the history
JST-526
  • Loading branch information
pgrzy-golem committed Nov 20, 2023
1 parent b4c7d66 commit e9e6f33
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/executor/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface TaskExecutorEventsDict {
* Fires when task executor is about to shut down, immediately after TaskExecutor.shutdown() is called.
*
*/
beforeend: () => void;
beforeEnd: () => void;

/**
* Fires when task executor is completely terminated.
Expand Down
4 changes: 2 additions & 2 deletions src/executor/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ describe("Task Executor", () => {
expect(spy).toHaveBeenCalledTimes(1);
});

it('it should emit "beforeend" and "end" events', async () => {
it('it should emit "beforeEnd" and "end" events', async () => {
const executor = await TaskExecutor.create({ package: "test", startupTimeout: 0, logger, yagnaOptions });
const beforeEnd = jest.fn();
const end = jest.fn();

executor.events.on("beforeend", beforeEnd);
executor.events.on("beforeEnd", beforeEnd);
executor.events.on("end", end);

await executor.shutdown();
Expand Down
6 changes: 5 additions & 1 deletion src/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ export class TaskExecutor {
* Stop all executor services and shut down executor instance.
*
* You can call this method multiple times, it will resolve only once the executor is shutdown.
*
* When shutdown() is initially called, a beforeEnd event is emitted.
*
* Once the executor is fully stopped, an end event is emitted.
*/
shutdown(): Promise<void> {
if (!this.isRunning) {
Expand All @@ -302,7 +306,7 @@ export class TaskExecutor {
* @private
*/
private async doShutdown() {
this.events.emit("beforeend");
this.events.emit("beforeEnd");
if (runtimeContextChecker.isNode) this.removeSignalHandlers();
clearTimeout(this.startupTimeoutId);
if (!this.configOptions.storageProvider) await this.storageProvider?.close();
Expand Down

0 comments on commit e9e6f33

Please sign in to comment.