-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathperformTaskRunAlerts.server.ts
More file actions
72 lines (65 loc) · 1.93 KB
/
performTaskRunAlerts.server.ts
File metadata and controls
72 lines (65 loc) · 1.93 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 { type Prisma, type ProjectAlertChannel } from "@trigger.dev/database";
import { type prisma } from "~/db.server";
import { commonWorker } from "~/v3/commonWorker.server";
import { BaseService } from "../baseService.server";
import { DeliverAlertService } from "./deliverAlert.server";
type FoundRun = Prisma.Result<
typeof prisma.taskRun,
{ include: { lockedBy: true; runtimeEnvironment: true } },
"findUniqueOrThrow"
>;
export class PerformTaskRunAlertsService extends BaseService {
public async call(runId: string) {
const run = await this._prisma.taskRun.findFirst({
where: { id: runId },
include: {
lockedBy: true,
runtimeEnvironment: {
include: {
parentEnvironment: true,
},
},
},
});
if (!run) {
return;
}
// Find all the alert channels
const alertChannels = await this._prisma.projectAlertChannel.findMany({
where: {
projectId: run.projectId,
alertTypes: {
has: "TASK_RUN",
},
environmentTypes: {
has: run.runtimeEnvironment.parentEnvironment?.type ?? run.runtimeEnvironment.type,
},
enabled: true,
},
});
for (const alertChannel of alertChannels) {
await this.#createAndSendAlert(alertChannel, run);
}
}
async #createAndSendAlert(alertChannel: ProjectAlertChannel, run: FoundRun) {
await DeliverAlertService.createAndSendAlert(
{
channelId: alertChannel.id,
channelType: alertChannel.type,
projectId: run.projectId,
environmentId: run.runtimeEnvironmentId,
alertType: "TASK_RUN",
taskRunId: run.id,
},
this._prisma
);
}
static async enqueue(runId: string, runAt?: Date) {
return await commonWorker.enqueue({
id: `performTaskRunAlerts:${runId}`,
job: "v3.performTaskRunAlerts",
payload: { runId },
availableAt: runAt,
});
}
}