Skip to content

Commit

Permalink
fix: fix a bug of interrupt called after finish when finish fails
Browse files Browse the repository at this point in the history
  • Loading branch information
yujiosaka committed Nov 4, 2023
1 parent 31a37b9 commit 693aa3d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/job-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ export default class JobRunner<I> {

try {
await task(job);
await job.finish();
} catch (error) {
await job.interrupt();
throw error;
}

await job.finish();
}

async requestJobStart(): Promise<Job<I> | null> {
Expand Down
32 changes: 16 additions & 16 deletions test/job-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("JobRunner", () => {
expect(job?.createdAt).toBeDate();
expect(job?.updatedAt).toBeDate();
expect(jobStore.fetchLastJobLock).toHaveBeenCalledTimes(1);
expect(jobStore.activateJobLock).toHaveBeenCalled();
expect(jobStore.activateJobLock).toHaveBeenCalledTimes(1);
});

test("runs job when last job lock is not found", async () => {
Expand All @@ -95,7 +95,7 @@ describe("JobRunner", () => {
expect(job?.createdAt).toBeDate();
expect(job?.updatedAt).toBeDate();
expect(jobStore.fetchLastJobLock).toHaveBeenCalledTimes(1);
expect(jobStore.activateJobLock).toHaveBeenCalled();
expect(jobStore.activateJobLock).toHaveBeenCalledTimes(1);
});

test("does not run job when last job lock is active", async () => {
Expand All @@ -121,7 +121,7 @@ describe("JobRunner", () => {
expect(job?.createdAt).toBeDate();
expect(job?.updatedAt).toBeDate();
expect(jobStore.fetchLastJobLock).toHaveBeenCalledTimes(2);
expect(jobStore.activateJobLock).toHaveBeenCalled();
expect(jobStore.activateJobLock).toHaveBeenCalledTimes(1);
});

test("does not run job with unfulfilled job requirement", async () => {
Expand Down Expand Up @@ -169,7 +169,7 @@ describe("JobRunner", () => {
expect(job?.createdAt).toBeDate();
expect(job?.updatedAt).toBeDate();
expect(jobStore.fetchLastJobLock).toHaveBeenCalledTimes(1);
expect(jobStore.activateJobLock).toHaveBeenCalled();
expect(jobStore.activateJobLock).toHaveBeenCalledTimes(1);
});

test("runs job with no lock", async () => {
Expand Down Expand Up @@ -212,9 +212,9 @@ describe("JobRunner", () => {

expect(failureTask).toHaveBeenCalledTimes(1);
expect(jobStore.fetchLastJobLock).toHaveBeenCalledTimes(1);
expect(jobStore.activateJobLock).toHaveBeenCalled();
expect(jobStore.activateJobLock).toHaveBeenCalledTimes(1);
expect(finish).not.toHaveBeenCalled();
expect(interrupt).toHaveBeenCalled();
expect(interrupt).toHaveBeenCalledTimes(1);
});

test("finishes task", async () => {
Expand All @@ -223,8 +223,8 @@ describe("JobRunner", () => {

expect(successTask).toHaveBeenCalledTimes(1);
expect(jobStore.fetchLastJobLock).toHaveBeenCalledTimes(1);
expect(jobStore.activateJobLock).toHaveBeenCalled();
expect(finish).toHaveBeenCalled();
expect(jobStore.activateJobLock).toHaveBeenCalledTimes(1);
expect(finish).toHaveBeenCalledTimes(1);
expect(interrupt).not.toHaveBeenCalled();
});

Expand All @@ -236,8 +236,8 @@ describe("JobRunner", () => {

expect(successTask).toHaveBeenCalledTimes(1);
expect(jobStore.fetchLastJobLock).toHaveBeenCalledTimes(1);
expect(jobStore.activateJobLock).toHaveBeenCalled();
expect(finish).toHaveBeenCalled();
expect(jobStore.activateJobLock).toHaveBeenCalledTimes(1);
expect(finish).toHaveBeenCalledTimes(1);
expect(interrupt).not.toHaveBeenCalled();
});

Expand All @@ -260,8 +260,8 @@ describe("JobRunner", () => {

expect(successTask).toHaveBeenCalledTimes(1);
expect(jobStore.fetchLastJobLock).toHaveBeenCalledTimes(2);
expect(jobStore.activateJobLock).toHaveBeenCalled();
expect(finish).toHaveBeenCalled();
expect(jobStore.activateJobLock).toHaveBeenCalledTimes(1);
expect(finish).toHaveBeenCalledTimes(1);
expect(interrupt).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -310,8 +310,8 @@ describe("JobRunner", () => {

expect(successTask).toHaveBeenCalledTimes(1);
expect(jobStore.fetchLastJobLock).toHaveBeenCalledTimes(1);
expect(jobStore.activateJobLock).toHaveBeenCalled();
expect(finish).toHaveBeenCalled();
expect(jobStore.activateJobLock).toHaveBeenCalledTimes(1);
expect(finish).toHaveBeenCalledTimes(1);
expect(interrupt).not.toHaveBeenCalled();
});

Expand All @@ -322,7 +322,7 @@ describe("JobRunner", () => {
expect(successTask).toHaveBeenCalledTimes(1);
expect(jobStore.fetchLastJobLock).toHaveBeenCalledTimes(1);
expect(jobStore.activateJobLock).not.toHaveBeenCalled();
expect(finish).toHaveBeenCalled();
expect(finish).toHaveBeenCalledTimes(1);
expect(interrupt).not.toHaveBeenCalled();
});

Expand All @@ -333,7 +333,7 @@ describe("JobRunner", () => {
expect(successTask).toHaveBeenCalledTimes(1);
expect(jobStore.fetchLastJobLock).not.toHaveBeenCalledTimes(1);
expect(jobStore.activateJobLock).not.toHaveBeenCalled();
expect(finish).toHaveBeenCalled();
expect(finish).toHaveBeenCalledTimes(1);
expect(interrupt).not.toHaveBeenCalled();
});
});
Expand Down

0 comments on commit 693aa3d

Please sign in to comment.