-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtriggerTaskValidator.ts
More file actions
106 lines (87 loc) · 2.77 KB
/
triggerTaskValidator.ts
File metadata and controls
106 lines (87 loc) · 2.77 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
104
105
106
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 type {
EntitlementValidationParams,
EntitlementValidationResult,
MaxAttemptsValidationParams,
ParentRunValidationParams,
TagValidationParams,
TriggerTaskValidator,
ValidationResult,
} from "../types";
import { ServiceValidationError } from "~/v3/services/common.server";
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 ServiceValidationError(
`Runs can only have ${MAX_TAGS_PER_RUN} tags, you're trying to set ${tags.length}.`
),
};
}
return { ok: true };
}
async validateEntitlement(
params: EntitlementValidationParams
): Promise<EntitlementValidationResult> {
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, plan: result?.plan };
}
validateMaxAttempts(params: MaxAttemptsValidationParams): ValidationResult {
const { taskId, attempt } = params;
if (attempt > MAX_ATTEMPTS) {
return {
ok: false,
error: new ServiceValidationError(
`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 ServiceValidationError(
`Cannot trigger ${taskId} as the parent run has a status of ${parentRun.status}`
),
};
}
return { ok: true };
}
}