-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathsingle-task-loop.ts
More file actions
54 lines (45 loc) · 1.22 KB
/
Copy pathsingle-task-loop.ts
File metadata and controls
54 lines (45 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { RuntimeOptions } from "../../config/types.ts";
import { logInfo } from "../../ui/logger.ts";
import { type TaskRunResult, runTask } from "./task.ts";
type TaskRunner = (task: string, options: RuntimeOptions) => Promise<TaskRunResult>;
type InfoLogger = (message: string) => void;
export interface SingleTaskLoopResult {
total: number;
completed: number;
failed: number;
}
/**
* Run the single-task flow with optional repeat behavior.
*/
export async function runSingleTaskLoop(
task: string,
options: RuntimeOptions,
deps?: {
runTaskFn?: TaskRunner;
logInfoFn?: InfoLogger;
},
): Promise<SingleTaskLoopResult> {
const runTaskFn = deps?.runTaskFn ?? runTask;
const logInfoFn = deps?.logInfoFn ?? logInfo;
const total = options.repeatCount;
let completed = 0;
let failed = 0;
for (let i = 1; i <= total; i++) {
if (total > 1) {
logInfoFn(`[${i}/${total}] Executing: ${task}`);
}
const result = await runTaskFn(task, options);
if (result.success) {
completed++;
continue;
}
failed++;
if (result.fatal || !options.continueOnFailure) {
break;
}
}
if (total > 1) {
logInfoFn(`Done: ${completed} succeeded, ${failed} failed of ${total}`);
}
return { total, completed, failed };
}