-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcancelTaskRun.server.ts
More file actions
72 lines (63 loc) · 1.96 KB
/
cancelTaskRun.server.ts
File metadata and controls
72 lines (63 loc) · 1.96 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { RunEngineVersion, type TaskRun } from "@trigger.dev/database";
import { engine } from "../runEngine.server";
import { BaseService } from "./baseService.server";
import { CancelTaskRunServiceV1 } from "./cancelTaskRunV1.server";
export type CancelTaskRunServiceOptions = {
reason?: string;
cancelAttempts?: boolean;
cancelledAt?: Date;
bulkActionId?: string;
/** Skip PENDING_CANCEL and finalize immediately (use when the worker is known to be dead). */
finalizeRun?: boolean;
};
type CancelTaskRunServiceResult = {
id: string;
alreadyFinished: boolean;
};
export type CancelableTaskRun = Pick<
TaskRun,
"id" | "engine" | "status" | "friendlyId" | "taskEventStore" | "createdAt" | "completedAt"
>;
export class CancelTaskRunService extends BaseService {
public async call(
taskRun: CancelableTaskRun,
options?: CancelTaskRunServiceOptions
): Promise<CancelTaskRunServiceResult | undefined> {
if (taskRun.engine === RunEngineVersion.V1) {
return await this.callV1(taskRun, options);
} else {
return await this.callV2(taskRun, options);
}
}
private async callV1(
taskRun: CancelableTaskRun,
options?: CancelTaskRunServiceOptions
): Promise<CancelTaskRunServiceResult | undefined> {
const service = new CancelTaskRunServiceV1(this._prisma);
const result = await service.call(taskRun, options);
if (!result) {
return;
}
return {
id: result.id,
alreadyFinished: false,
};
}
private async callV2(
taskRun: CancelableTaskRun,
options?: CancelTaskRunServiceOptions
): Promise<CancelTaskRunServiceResult | undefined> {
const result = await engine.cancelRun({
runId: taskRun.id,
completedAt: options?.cancelledAt,
reason: options?.reason,
finalizeRun: options?.finalizeRun,
bulkActionId: options?.bulkActionId,
tx: this._prisma,
});
return {
id: result.run.id,
alreadyFinished: result.alreadyFinished,
};
}
}