Skip to content

Commit a95b84d

Browse files
committed
Add instance/job diagnostics to SERVER_BUSY responses
1 parent 3876a33 commit a95b84d

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

apps/media-server/src/routes/video.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ const processSchema = z.object({
6363
remuxOnly: z.boolean().optional(),
6464
});
6565

66+
function getInstanceId(): string {
67+
return process.env.HOSTNAME || `pid-${process.pid}`;
68+
}
69+
6670
function isBusyError(err: unknown): boolean {
6771
return err instanceof Error && err.message.includes("Server is busy");
6872
}
@@ -74,6 +78,8 @@ function isTimeoutError(err: unknown): boolean {
7478
video.get("/status", (c) => {
7579
const jobs = getAllJobs();
7680
return c.json({
81+
instanceId: getInstanceId(),
82+
pid: process.pid,
7783
activeVideoProcesses: getActiveVideoProcessCount(),
7884
maxConcurrentVideoProcesses: getMaxConcurrentVideoProcesses(),
7985
activeProbeProcesses: getActiveProbeProcessCount(),
@@ -234,13 +240,24 @@ video.post("/process", async (c) => {
234240
if (!canAcceptNewVideoProcess()) {
235241
const activeVideoProcesses = getActiveVideoProcessCount();
236242
const maxConcurrentVideoProcesses = getMaxConcurrentVideoProcesses();
243+
const jobs = getAllJobs();
237244
return c.json(
238245
{
239246
error: "Server is busy",
240247
code: "SERVER_BUSY",
241248
details: `Too many concurrent video processing jobs (${activeVideoProcesses}/${maxConcurrentVideoProcesses}), please retry later`,
249+
instanceId: getInstanceId(),
250+
pid: process.pid,
242251
activeVideoProcesses,
243252
maxConcurrentVideoProcesses,
253+
jobCount: jobs.length,
254+
jobs: jobs.map((job) => ({
255+
jobId: job.jobId,
256+
videoId: job.videoId,
257+
phase: job.phase,
258+
progress: job.progress,
259+
updatedAt: job.updatedAt,
260+
})),
244261
},
245262
503,
246263
);

apps/web/workflows/process-video.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,35 @@ async function startMediaServerProcessJob(
155155
error?: string;
156156
code?: string;
157157
details?: string;
158+
instanceId?: string;
159+
pid?: number;
160+
activeVideoProcesses?: number;
161+
maxConcurrentVideoProcesses?: number;
162+
jobCount?: number;
158163
};
159-
const errorMessage =
164+
const baseErrorMessage =
160165
errorData.error ||
161166
errorData.details ||
162167
"Video processing failed to start";
168+
const busyDiagnostics =
169+
errorData.code === "SERVER_BUSY"
170+
? [
171+
errorData.instanceId ? `instance=${errorData.instanceId}` : null,
172+
typeof errorData.pid === "number" ? `pid=${errorData.pid}` : null,
173+
typeof errorData.activeVideoProcesses === "number" &&
174+
typeof errorData.maxConcurrentVideoProcesses === "number"
175+
? `active=${errorData.activeVideoProcesses}/${errorData.maxConcurrentVideoProcesses}`
176+
: null,
177+
typeof errorData.jobCount === "number"
178+
? `jobCount=${errorData.jobCount}`
179+
: null,
180+
]
181+
.filter(Boolean)
182+
.join(", ")
183+
: "";
184+
const errorMessage = busyDiagnostics
185+
? `${baseErrorMessage} (${busyDiagnostics})`
186+
: baseErrorMessage;
163187
const shouldRetry =
164188
response.status === 503 &&
165189
(errorData.code === "SERVER_BUSY" ||

0 commit comments

Comments
 (0)