-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtriggerTaskValidator.ts
More file actions
103 lines (84 loc) · 2.71 KB
/
triggerTaskValidator.ts
File metadata and controls
103 lines (84 loc) · 2.71 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { MAX_TAGS_PER_RUN } from "~/models/taskRunTag.server";
import { logger } from "~/services/logger.server";
import { getEntitlement } from "~/services/platform.v3.server";
import { MAX_ATTEMPTS, OutOfEntitlementError } from "~/v3/services/triggerTask.server";
import { isFinalRunStatus } from "~/v3/taskStatus";
import { EngineServiceValidationError } from "../concerns/errors";
import {
EntitlementValidationParams,
MaxAttemptsValidationParams,
ParentRunValidationParams,
TagValidationParams,
TriggerTaskValidator,
ValidationResult,
} from "../types";
export class DefaultTriggerTaskValidator implements TriggerTaskValidator {
validateTags(params: TagValidationParams): ValidationResult {
const { tags } = params;
if (!tags) {
return { ok: true };
}
if (typeof tags === "string") {
return { ok: true };
}
if (tags.length > MAX_TAGS_PER_RUN) {
return {
ok: false,
error: new EngineServiceValidationError(
`Runs can only have ${MAX_TAGS_PER_RUN} tags, you're trying to set ${tags.length}.`
),
};
}
return { ok: true };
}
async validateEntitlement(params: EntitlementValidationParams): Promise<ValidationResult> {
const { environment } = params;
if (environment.type === "DEVELOPMENT") {
return { ok: true };
}
const result = await getEntitlement(environment.organizationId);
if (result && result.hasAccess === false) {
return {
ok: false,
error: new OutOfEntitlementError(),
};
}
return { ok: true };
}
validateMaxAttempts(params: MaxAttemptsValidationParams): ValidationResult {
const { taskId, attempt } = params;
if (attempt > MAX_ATTEMPTS) {
return {
ok: false,
error: new EngineServiceValidationError(
`Failed to trigger ${taskId} after ${MAX_ATTEMPTS} attempts.`
),
};
}
return { ok: true };
}
validateParentRun(params: ParentRunValidationParams): ValidationResult {
const { taskId, parentRun, resumeParentOnCompletion } = params;
// If there's no parent run specified, that's fine
if (!parentRun) {
return { ok: true };
}
// If we're not resuming the parent, we don't need to validate its status
if (!resumeParentOnCompletion) {
return { ok: true };
}
// Check if the parent run is in a final state
if (isFinalRunStatus(parentRun.status)) {
logger.debug("Parent run is in a terminal state", {
parentRun,
});
return {
ok: false,
error: new EngineServiceValidationError(
`Cannot trigger ${taskId} as the parent run has a status of ${parentRun.status}`
),
};
}
return { ok: true };
}
}