-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcheckSchedule.server.ts
More file actions
130 lines (116 loc) · 3.82 KB
/
checkSchedule.server.ts
File metadata and controls
130 lines (116 loc) · 3.82 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { ZodError } from "zod";
import { CronPattern } from "../schedules";
import { BaseService, ServiceValidationError } from "./baseService.server";
import { getLimit } from "~/services/platform.v3.server";
import { getTimezones } from "~/utils/timezones.server";
import { env } from "~/env.server";
import { type PrismaClientOrTransaction, type RuntimeEnvironmentType } from "@trigger.dev/database";
type Schedule = {
cron: string;
timezone?: string;
taskIdentifier: string;
friendlyId?: string;
};
export class CheckScheduleService extends BaseService {
public async call(projectId: string, schedule: Schedule, environmentIds: string[]) {
//validate the cron expression
try {
CronPattern.parse(schedule.cron);
} catch (e) {
if (e instanceof ZodError) {
throw new ServiceValidationError(`Invalid cron expression: ${e.issues[0].message}`);
}
throw new ServiceValidationError(
`Invalid cron expression: ${e instanceof Error ? e.message : JSON.stringify(e)}`
);
}
//chek it's a valid timezone
if (schedule.timezone) {
const possibleTimezones = getTimezones();
if (!possibleTimezones.includes(schedule.timezone)) {
throw new ServiceValidationError(
`Invalid IANA timezone: '${schedule.timezone}'. View the list of valid timezones at ${env.APP_ORIGIN}/timezones`
);
}
}
//check the task exists
const task = await this._prisma.backgroundWorkerTask.findFirst({
where: {
slug: schedule.taskIdentifier,
projectId: projectId,
},
orderBy: {
createdAt: "desc",
},
});
if (!task) {
throw new ServiceValidationError(
`Task with identifier ${schedule.taskIdentifier} not found in project.`
);
}
if (task.triggerSource !== "SCHEDULED") {
throw new ServiceValidationError(
`Task with identifier ${schedule.taskIdentifier} is not a scheduled task.`
);
}
//check they're within their limit
const project = await this._prisma.project.findFirst({
where: {
id: projectId,
},
select: {
organizationId: true,
environments: {
select: {
id: true,
type: true,
archivedAt: true,
},
},
},
});
if (!project) {
throw new ServiceValidationError("Project not found");
}
const environments = project.environments.filter((env) => environmentIds.includes(env.id));
if (environments.some((env) => env.archivedAt)) {
throw new ServiceValidationError("Can't add or edit a schedule for an archived branch");
}
//if creating a schedule, check they're under the limits
if (!schedule.friendlyId) {
const limit = await getLimit(project.organizationId, "schedules", 100_000_000);
const schedulesCount = await CheckScheduleService.getUsedSchedulesCount({
prisma: this._prisma,
environments: project.environments,
});
if (schedulesCount >= limit) {
throw new ServiceValidationError(
`You have created ${schedulesCount}/${limit} schedules so you'll need to increase your limits or delete some schedules.`
);
}
}
}
static async getUsedSchedulesCount({
prisma,
environments,
}: {
prisma: PrismaClientOrTransaction;
environments: { id: string; type: RuntimeEnvironmentType; archivedAt: Date | null }[];
}) {
const deployedEnvironments = environments.filter(
(env) => env.type !== "DEVELOPMENT" && !env.archivedAt
);
const schedulesCount = await prisma.taskScheduleInstance.count({
where: {
environmentId: {
in: deployedEnvironments.map((env) => env.id),
},
active: true,
taskSchedule: {
active: true,
},
},
});
return schedulesCount;
}
}