-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from flying-dice/fix/catch-errors-on-failed-tasks
Fix/catch errors on failed tasks
- Loading branch information
Showing
8 changed files
with
103 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { findFirstPendingTask } from './find-first-pending-task' | ||
import { AssetTaskStatus, AssetTaskType } from '../schemas/release-asset-task.schema' | ||
|
||
describe('findFirstPending', () => { | ||
it('should return the first pending task with sequence 1', () => { | ||
const tasks = [ | ||
{ id: '1', sequence: 1, status: AssetTaskStatus.PENDING, type: AssetTaskType.DOWNLOAD }, | ||
{ id: '2', sequence: 2, status: AssetTaskStatus.PENDING, type: AssetTaskType.EXTRACT } | ||
] | ||
const result = findFirstPendingTask(tasks) | ||
expect(result).toEqual(tasks[0]) | ||
}) | ||
|
||
it('should return the first pending task with sequence greater than 1 if previous task is completed', () => { | ||
const tasks = [ | ||
{ id: '1', sequence: 1, status: AssetTaskStatus.COMPLETED, type: AssetTaskType.DOWNLOAD }, | ||
{ id: '2', sequence: 2, status: AssetTaskStatus.PENDING, type: AssetTaskType.EXTRACT } | ||
] | ||
const result = findFirstPendingTask(tasks) | ||
expect(result).toEqual(tasks[1]) | ||
}) | ||
|
||
it('should return undefined if no tasks are pending', () => { | ||
const tasks = [ | ||
{ id: '1', sequence: 1, status: AssetTaskStatus.COMPLETED, type: AssetTaskType.DOWNLOAD }, | ||
{ id: '2', sequence: 2, status: AssetTaskStatus.COMPLETED, type: AssetTaskType.EXTRACT } | ||
] | ||
const result = findFirstPendingTask(tasks) | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should return undefined if no tasks exist', () => { | ||
const tasks = [] | ||
const result = findFirstPendingTask(tasks) | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should return undefined if the first task is not pending and no previous task is completed', () => { | ||
const tasks = [ | ||
{ id: '1', sequence: 1, status: AssetTaskStatus.FAILED, type: AssetTaskType.DOWNLOAD }, | ||
{ id: '2', sequence: 2, status: AssetTaskStatus.PENDING, type: AssetTaskType.EXTRACT } | ||
] | ||
const result = findFirstPendingTask(tasks) | ||
expect(result).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { AssetTask } from '../schemas/release-asset-task.schema' | ||
|
||
export function findFirstPendingTask<T extends Pick<AssetTask, 'status' | 'sequence'>>( | ||
tasks: T[] | ||
): T | undefined { | ||
let previousTask: T | undefined | ||
|
||
for (const task of tasks) { | ||
if (task.status === 'PENDING') { | ||
if (previousTask?.status === 'COMPLETED') { | ||
return task | ||
} | ||
if (!previousTask) { | ||
return task | ||
} | ||
} | ||
previousTask = task | ||
} | ||
|
||
return undefined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters