-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathresources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.runs.bulkaction.$bulkActionParam.stream.tsx
More file actions
103 lines (90 loc) · 2.79 KB
/
resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.runs.bulkaction.$bulkActionParam.stream.tsx
File metadata and controls
103 lines (90 loc) · 2.79 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 { z } from "zod";
import { $replica } from "~/db.server";
import { env } from "~/env.server";
import { logger } from "~/services/logger.server";
import { requireUserId } from "~/services/session.server";
import { EnvironmentParamSchema } from "~/utils/pathBuilder";
import { createSSELoader, type SendFunction } from "~/utils/sse";
const Params = EnvironmentParamSchema.extend({
bulkActionParam: z.string(),
});
export const loader = createSSELoader({
timeout: env.DEV_PRESENCE_SSE_TIMEOUT,
interval: env.DEV_PRESENCE_POLL_MS,
debug: false,
handler: async ({ id, controller, debug, request, params }) => {
const userId = await requireUserId(request);
const { organizationSlug, projectParam, envParam, bulkActionParam } = Params.parse(params);
const environment = await $replica.runtimeEnvironment.findFirst({
where: {
id: envParam,
project: {
slug: projectParam,
organization: {
members: {
some: {
userId,
},
},
},
},
},
});
if (!environment) {
throw new Response("Not Found", { status: 404 });
}
const getBulkActionProgress = async (send: SendFunction) => {
try {
const bulkAction = await $replica.bulkActionGroup.findFirst({
select: {
status: true,
successCount: true,
failureCount: true,
},
where: {
friendlyId: bulkActionParam,
environmentId: environment.id,
},
});
send({
event: "progress",
data: JSON.stringify({
status: bulkAction?.status,
successCount: bulkAction?.successCount,
failureCount: bulkAction?.failureCount,
}),
});
return bulkAction;
} catch (error) {
// Handle the case where the controller is closed
logger.debug("Failed to send bulk action progress data, stream might be closed", { error });
return null;
}
};
return {
beforeStream: async () => {
logger.debug("Start dev presence listening SSE session", {
environmentId: environment.id,
});
},
initStream: async ({ send }) => {
const bulkAction = await getBulkActionProgress(send);
send({ event: "time", data: new Date().toISOString() });
if (bulkAction?.status !== "PENDING") {
return false;
}
return true;
},
iterator: async ({ send, date }) => {
const bulkAction = await getBulkActionProgress(send);
if (bulkAction?.status !== "PENDING") {
return false;
}
return true;
},
cleanup: async ({ send }) => {
await getBulkActionProgress(send);
},
};
},
});