-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathadmin.api.v1.snapshot.ts
More file actions
172 lines (145 loc) · 4.96 KB
/
admin.api.v1.snapshot.ts
File metadata and controls
172 lines (145 loc) · 4.96 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { S3Client } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import { type DataFunctionArgs } from "@remix-run/node";
import v8 from "v8";
import { prisma } from "~/db.server";
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server";
import { logger } from "~/services/logger.server";
// Use 100MB parts for faster parallel uploads of large snapshots
const PART_SIZE = 100 * 1024 * 1024;
// Use high parallelism to maximize upload speed
const QUEUE_SIZE = 8;
// Format date as yyyy-MM-dd HH_mm_ss_SSS
function formatDate(date: Date) {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const milliseconds = date.getMilliseconds();
return `${year}-${month.toString().padStart(2, "0")}-${day.toString().padStart(2, "0")} ${hours
.toString()
.padStart(2, "0")}_${minutes.toString().padStart(2, "0")}_${seconds
.toString()
.padStart(2, "0")}_${milliseconds.toString().padStart(3, "0")}`;
}
function getS3Config() {
const bucket = process.env.SNAPSHOT_S3_BUCKET;
const region = process.env.SNAPSHOT_S3_REGION ?? "us-east-1";
if (!bucket) {
return undefined;
}
// Optional - only needed for non-AWS S3 (MinIO, R2, etc.) or local dev
const endpoint = process.env.SNAPSHOT_S3_ENDPOINT;
const accessKeyId = process.env.SNAPSHOT_S3_ACCESS_KEY_ID;
const secretAccessKey = process.env.SNAPSHOT_S3_SECRET_ACCESS_KEY;
// If explicit credentials provided, use them (local dev / non-AWS)
// Otherwise, SDK uses default credential chain (IAM role, env vars, etc.)
const credentials =
accessKeyId && secretAccessKey ? { accessKeyId, secretAccessKey } : undefined;
return {
bucket,
region,
endpoint,
credentials,
};
}
export async function loader({ request }: DataFunctionArgs) {
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
if (!authenticationResult) {
throw new Response("You must be an admin to perform this action", { status: 403 });
}
const user = await prisma.user.findFirst({
where: {
id: authenticationResult.userId,
},
});
if (!user?.admin) {
throw new Response("You must be an admin to perform this action", { status: 403 });
}
const s3Config = getS3Config();
if (!s3Config) {
throw new Response(
"S3 is not configured. Set SNAPSHOT_S3_ENDPOINT, SNAPSHOT_S3_BUCKET, SNAPSHOT_S3_ACCESS_KEY_ID, and SNAPSHOT_S3_SECRET_ACCESS_KEY.",
{ status: 500 }
);
}
const s3Client = new S3Client({
region: s3Config.region,
...(s3Config.credentials && { credentials: s3Config.credentials }),
...(s3Config.endpoint && { endpoint: s3Config.endpoint, forcePathStyle: true }),
});
const filename = `${getTaskIdentifier()}-${formatDate(new Date())}.heapsnapshot`;
const s3Key = `snapshots/${filename}`;
logger.info("Taking heap snapshot and streaming to S3", {
bucket: s3Config.bucket,
key: s3Key,
});
try {
const startTime = Date.now();
const snapshotStream = v8.getHeapSnapshot();
const upload = new Upload({
client: s3Client,
params: {
Bucket: s3Config.bucket,
Key: s3Key,
Body: snapshotStream,
ContentType: "application/octet-stream",
},
queueSize: QUEUE_SIZE,
partSize: PART_SIZE,
leavePartsOnError: false,
});
let totalBytes = 0;
upload.on("httpUploadProgress", (progress) => {
totalBytes = progress.loaded ?? totalBytes;
logger.info("Upload progress", {
loaded: progress.loaded,
part: progress.part,
});
});
await upload.done();
const duration = Date.now() - startTime;
logger.info("Heap snapshot uploaded to S3", {
bucket: s3Config.bucket,
key: s3Key,
durationMs: duration,
durationSec: Math.round(duration / 1000),
totalBytes,
uploadSpeedMBps: totalBytes > 0 ? Math.round((totalBytes / 1024 / 1024 / (duration / 1000)) * 10) / 10 : 0,
});
return new Response(
JSON.stringify({
success: true,
bucket: s3Config.bucket,
key: s3Key,
sizeBytes: totalBytes,
durationMs: duration,
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
} catch (error) {
logger.error("Failed to upload heap snapshot to S3", {
error: error instanceof Error ? error.message : String(error),
bucket: s3Config.bucket,
key: s3Key,
});
throw new Response(
`Failed to upload snapshot to S3: ${error instanceof Error ? error.message : String(error)}`,
{ status: 500 }
);
}
}
function getTaskIdentifier() {
if (!process.env.ECS_CONTAINER_METADATA_URI) {
return "local";
}
const url = new URL(process.env.ECS_CONTAINER_METADATA_URI);
return url.pathname.split("/")[2].split("-")[0];
}