@certalis/agent-smith 0.1.13 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin.js +123 -20
- package/dist/edge.js +1 -0
- package/dist/https.js +6 -7
- package/dist/pool.js +123 -20
- package/dist/post-processor.worker.js +3 -1
- package/dist/server.js +123 -20
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -239,9 +239,11 @@ var init_errors = __esm(() => {
|
|
|
239
239
|
};
|
|
240
240
|
ServiceUnavailableError = class ServiceUnavailableError extends AppError {
|
|
241
241
|
service;
|
|
242
|
-
|
|
242
|
+
retryAfterSeconds;
|
|
243
|
+
constructor(message, service, retryAfterSeconds) {
|
|
243
244
|
super(message, "SERVICE_UNAVAILABLE", 503, { service });
|
|
244
245
|
this.service = service;
|
|
246
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
245
247
|
}
|
|
246
248
|
};
|
|
247
249
|
});
|
|
@@ -4505,7 +4507,8 @@ function errorResponse(error) {
|
|
|
4505
4507
|
}
|
|
4506
4508
|
if (error instanceof Error && "statusCode" in error && typeof error.statusCode === "number") {
|
|
4507
4509
|
const details = "context" in error && error.context !== undefined ? { details: error.context } : {};
|
|
4508
|
-
|
|
4510
|
+
const retryAfter = error.statusCode === 503 && "retryAfterSeconds" in error && typeof error.retryAfterSeconds === "number" && Number.isSafeInteger(error.retryAfterSeconds) && error.retryAfterSeconds > 0 ? { "Retry-After": String(error.retryAfterSeconds) } : undefined;
|
|
4511
|
+
return jsonResponse({ error: error.message, ...details }, error.statusCode, retryAfter);
|
|
4509
4512
|
}
|
|
4510
4513
|
const message = error instanceof Error ? error.message : "Internal server error";
|
|
4511
4514
|
const status = 500;
|
|
@@ -5111,7 +5114,7 @@ function utilization(value) {
|
|
|
5111
5114
|
const raw = number(value.used_percentage ?? value.usedPercentage ?? value.percent ?? value.utilization);
|
|
5112
5115
|
if (raw === null)
|
|
5113
5116
|
return null;
|
|
5114
|
-
return raw
|
|
5117
|
+
return raw / 100;
|
|
5115
5118
|
}
|
|
5116
5119
|
function resetAt(value) {
|
|
5117
5120
|
const raw = value.resets_at ?? value.reset_at ?? value.resetAt;
|
|
@@ -8111,6 +8114,18 @@ class APIRouter {
|
|
|
8111
8114
|
this.registerHandlers();
|
|
8112
8115
|
}
|
|
8113
8116
|
registerHandlers() {
|
|
8117
|
+
if (this.context.deploymentDrain) {
|
|
8118
|
+
const control = this.context.deploymentDrain;
|
|
8119
|
+
for (const [method, action] of [
|
|
8120
|
+
["GET", "status"],
|
|
8121
|
+
["POST", "start"],
|
|
8122
|
+
["DELETE", "resume"]
|
|
8123
|
+
]) {
|
|
8124
|
+
this.staticHandlers.set(`${method}:/api/drain`, () => Response.json(control(action), {
|
|
8125
|
+
headers: { "cache-control": "no-store" }
|
|
8126
|
+
}));
|
|
8127
|
+
}
|
|
8128
|
+
}
|
|
8114
8129
|
if (this.enrollment) {
|
|
8115
8130
|
this.staticHandlers.set("POST:/api/enrollment-invitations", this.enrollment.issue);
|
|
8116
8131
|
this.staticHandlers.set("POST:/api/enrollment-emails", this.enrollment.emails);
|
|
@@ -8145,6 +8160,9 @@ class APIRouter {
|
|
|
8145
8160
|
this.staticHandlers.set("GET:/api/stats", () => statsHandler());
|
|
8146
8161
|
this.staticHandlers.set("GET:/api/status", () => fleetStatusHandler());
|
|
8147
8162
|
this.staticHandlers.set("GET:/api/pool-status", (_req, url) => {
|
|
8163
|
+
if (this.context.deploymentDrain?.("status").draining) {
|
|
8164
|
+
return errorResponse(new ServiceUnavailableError("Pool is draining for deployment", undefined, 30));
|
|
8165
|
+
}
|
|
8148
8166
|
const provider = url.searchParams.get("provider") ?? "";
|
|
8149
8167
|
const model = url.searchParams.get("model") ?? undefined;
|
|
8150
8168
|
if (!isAccountProvider(provider) || !getProviders().includes(provider) || model !== undefined && (model.trim().length === 0 || model.length > 200)) {
|
|
@@ -9834,6 +9852,15 @@ var init_proxy_operations = __esm(() => {
|
|
|
9834
9852
|
});
|
|
9835
9853
|
|
|
9836
9854
|
// ../../packages/proxy/src/handlers/proxy-attempts.ts
|
|
9855
|
+
function admissionRetrySeconds(req) {
|
|
9856
|
+
if (req.signal.aborted)
|
|
9857
|
+
return;
|
|
9858
|
+
const count = req.headers.get("x-stainless-retry-count") ?? "0";
|
|
9859
|
+
if (!/^\d+$/.test(count))
|
|
9860
|
+
return 1;
|
|
9861
|
+
const ordinal = Number(count);
|
|
9862
|
+
return ordinal === 0 ? 1 : ordinal === 1 ? 10 : 30;
|
|
9863
|
+
}
|
|
9837
9864
|
function createBodyStream(body) {
|
|
9838
9865
|
if (!body)
|
|
9839
9866
|
return;
|
|
@@ -9893,7 +9920,7 @@ async function attemptAccount(request2, account2, failoverAttempts) {
|
|
|
9893
9920
|
permit = request2.ctx.runtime.teamRouting.enabled ? await admission?.acquire(account2.id, request2.ctx.runtime.teamRouting, request2.req.signal) : undefined;
|
|
9894
9921
|
} catch (error) {
|
|
9895
9922
|
if (error instanceof TeamAdmissionError) {
|
|
9896
|
-
throw new ServiceUnavailableError(error.message, request2.ctx.providerName);
|
|
9923
|
+
throw new ServiceUnavailableError(error.message, request2.ctx.providerName, admissionRetrySeconds(request2.req));
|
|
9897
9924
|
}
|
|
9898
9925
|
throw error;
|
|
9899
9926
|
}
|
|
@@ -11864,7 +11891,7 @@ function exposureFailure(req, url, server, config) {
|
|
|
11864
11891
|
if (!isSameOriginManagementRequest(req, url)) {
|
|
11865
11892
|
return jsonError(403, "Cross-origin management requests are not allowed");
|
|
11866
11893
|
}
|
|
11867
|
-
if (MUTATING_METHODS.has(req.method)) {
|
|
11894
|
+
if (url.pathname === "/api/drain" || MUTATING_METHODS.has(req.method)) {
|
|
11868
11895
|
if (role !== "admin") {
|
|
11869
11896
|
return jsonError(401, "An admin key is required for management writes");
|
|
11870
11897
|
}
|
|
@@ -11921,7 +11948,7 @@ function wireStrategyHotReload(config, log26, dbOps, proxyContext, runtimeConfig
|
|
|
11921
11948
|
}
|
|
11922
11949
|
});
|
|
11923
11950
|
}
|
|
11924
|
-
function bootstrapRuntime(port, serverLog) {
|
|
11951
|
+
function bootstrapRuntime(port, serverLog, deploymentDrain) {
|
|
11925
11952
|
container.registerInstance(SERVICE_KEYS.Config, new Config);
|
|
11926
11953
|
container.registerInstance(SERVICE_KEYS.Logger, serverLog);
|
|
11927
11954
|
const config = container.resolve(SERVICE_KEYS.Config);
|
|
@@ -11940,6 +11967,7 @@ function bootstrapRuntime(port, serverLog) {
|
|
|
11940
11967
|
container.registerInstance(SERVICE_KEYS.PricingLogger, pricingLogger);
|
|
11941
11968
|
setPricingLogger(pricingLogger);
|
|
11942
11969
|
const apiRouter = new APIRouter({
|
|
11970
|
+
deploymentDrain,
|
|
11943
11971
|
enrollmentOrigin: process.env.AGENT_SMITH_ENROLLMENT_ORIGIN,
|
|
11944
11972
|
config,
|
|
11945
11973
|
dbOps,
|
|
@@ -12100,6 +12128,48 @@ var init_dashboard_assets = __esm(() => {
|
|
|
12100
12128
|
init_src2();
|
|
12101
12129
|
});
|
|
12102
12130
|
|
|
12131
|
+
// ../../packages/runtime-server/src/deployment-drain.ts
|
|
12132
|
+
class DeploymentDrain {
|
|
12133
|
+
activity;
|
|
12134
|
+
now;
|
|
12135
|
+
expiresAt = null;
|
|
12136
|
+
stopping = false;
|
|
12137
|
+
log = new Logger("DeploymentDrain");
|
|
12138
|
+
constructor(activity, now = () => Date.now()) {
|
|
12139
|
+
this.activity = activity;
|
|
12140
|
+
this.now = now;
|
|
12141
|
+
}
|
|
12142
|
+
isDraining = () => {
|
|
12143
|
+
if (this.expiresAt !== null && this.now() >= this.expiresAt) {
|
|
12144
|
+
this.expiresAt = null;
|
|
12145
|
+
this.log.info("Deployment pause expired; accepting new inference");
|
|
12146
|
+
}
|
|
12147
|
+
return this.stopping || this.expiresAt !== null;
|
|
12148
|
+
};
|
|
12149
|
+
control = (action) => {
|
|
12150
|
+
if (action === "start" && !this.stopping) {
|
|
12151
|
+
if (!this.isDraining())
|
|
12152
|
+
this.log.info("Deployment pause started");
|
|
12153
|
+
this.expiresAt = this.now() + 120000;
|
|
12154
|
+
} else if (action === "resume" && !this.stopping) {
|
|
12155
|
+
this.expiresAt = null;
|
|
12156
|
+
this.log.info("Deployment pause cancelled; accepting new inference");
|
|
12157
|
+
}
|
|
12158
|
+
return {
|
|
12159
|
+
draining: this.isDraining(),
|
|
12160
|
+
expiresAt: this.expiresAt,
|
|
12161
|
+
...this.activity()
|
|
12162
|
+
};
|
|
12163
|
+
};
|
|
12164
|
+
shutdown() {
|
|
12165
|
+
this.stopping = true;
|
|
12166
|
+
this.expiresAt = null;
|
|
12167
|
+
}
|
|
12168
|
+
}
|
|
12169
|
+
var init_deployment_drain = __esm(() => {
|
|
12170
|
+
init_src3();
|
|
12171
|
+
});
|
|
12172
|
+
|
|
12103
12173
|
// ../../packages/runtime-server/src/oauth-token.ts
|
|
12104
12174
|
async function forwardOAuthTokenRequest(req, body) {
|
|
12105
12175
|
const headers3 = new Headers(req.headers);
|
|
@@ -12148,7 +12218,8 @@ function createServerFetchHandler({
|
|
|
12148
12218
|
handleProxyRequest = handleProxy,
|
|
12149
12219
|
handleWebSocketUpgrade = handleWebSocketUpgradeRequest,
|
|
12150
12220
|
handleOAuthTokenRequest = forwardOAuthTokenRequest,
|
|
12151
|
-
serveDashboardAsset = serveDashboardRoute
|
|
12221
|
+
serveDashboardAsset = serveDashboardRoute,
|
|
12222
|
+
isDeploymentDraining = () => false
|
|
12152
12223
|
}) {
|
|
12153
12224
|
return async (req, server) => {
|
|
12154
12225
|
const url = new URL(req.url);
|
|
@@ -12176,6 +12247,9 @@ function createServerFetchHandler({
|
|
|
12176
12247
|
}
|
|
12177
12248
|
const isProviderRoute = url.pathname === "/v1" || url.pathname.startsWith("/v1/") || url.pathname.startsWith("/tc-acct/");
|
|
12178
12249
|
if (isProviderRoute) {
|
|
12250
|
+
if (isDeploymentDraining()) {
|
|
12251
|
+
return errorResponse(new ServiceUnavailableError("Pool is draining for deployment", undefined, 30));
|
|
12252
|
+
}
|
|
12179
12253
|
if (server) {
|
|
12180
12254
|
const websocketResponse = await handleWebSocketUpgrade(req, url, proxyContext, server);
|
|
12181
12255
|
if (websocketResponse) {
|
|
@@ -12290,17 +12364,38 @@ async function stopServerRuntime() {
|
|
|
12290
12364
|
const errors2 = [];
|
|
12291
12365
|
const activeServer = serverInstance;
|
|
12292
12366
|
const activeUnixServer = unixServerInstance;
|
|
12293
|
-
|
|
12294
|
-
|
|
12367
|
+
deploymentDrain?.shutdown();
|
|
12368
|
+
const listeners = [activeUnixServer, activeServer].filter((listener) => listener !== null);
|
|
12369
|
+
const shutdownDeadline = Date.now() + 25000;
|
|
12295
12370
|
try {
|
|
12296
|
-
await
|
|
12297
|
-
|
|
12298
|
-
|
|
12299
|
-
|
|
12300
|
-
|
|
12301
|
-
|
|
12302
|
-
|
|
12303
|
-
|
|
12371
|
+
await new Promise((resolve3) => {
|
|
12372
|
+
let timer;
|
|
12373
|
+
const finish = () => {
|
|
12374
|
+
clearInterval(timer);
|
|
12375
|
+
resolve3();
|
|
12376
|
+
};
|
|
12377
|
+
const check = () => {
|
|
12378
|
+
const pending = listeners.some((listener) => listener.pendingRequests > 0 || listener.pendingWebSockets > 0);
|
|
12379
|
+
if (!pending)
|
|
12380
|
+
finish();
|
|
12381
|
+
else if (Date.now() >= shutdownDeadline) {
|
|
12382
|
+
lifecycleLog.warn("Shutdown grace expired; closing remaining connections");
|
|
12383
|
+
finish();
|
|
12384
|
+
}
|
|
12385
|
+
};
|
|
12386
|
+
checkShutdownDrain = check;
|
|
12387
|
+
timer = setInterval(check, 50);
|
|
12388
|
+
check();
|
|
12389
|
+
});
|
|
12390
|
+
const stopped = await Promise.allSettled(listeners.map((listener) => listener.stop(true)));
|
|
12391
|
+
for (const result of stopped) {
|
|
12392
|
+
if (result.status === "rejected")
|
|
12393
|
+
errors2.push(toError("Failed to stop listener", result.reason));
|
|
12394
|
+
}
|
|
12395
|
+
} finally {
|
|
12396
|
+
checkShutdownDrain = undefined;
|
|
12397
|
+
serverInstance = null;
|
|
12398
|
+
unixServerInstance = null;
|
|
12304
12399
|
}
|
|
12305
12400
|
stopRetentionMaintenance();
|
|
12306
12401
|
try {
|
|
@@ -12348,6 +12443,11 @@ function startServer(options) {
|
|
|
12348
12443
|
if (withDashboard) {
|
|
12349
12444
|
loadDashboardAssets();
|
|
12350
12445
|
}
|
|
12446
|
+
const drain = new DeploymentDrain(() => ({
|
|
12447
|
+
activeRequests: Math.max(0, (serverInstance?.pendingRequests ?? 0) + (unixServerInstance?.pendingRequests ?? 0) - 1),
|
|
12448
|
+
activeWebSockets: (serverInstance?.pendingWebSockets ?? 0) + (unixServerInstance?.pendingWebSockets ?? 0)
|
|
12449
|
+
}));
|
|
12450
|
+
deploymentDrain = drain;
|
|
12351
12451
|
const {
|
|
12352
12452
|
config,
|
|
12353
12453
|
dbOps,
|
|
@@ -12357,13 +12457,14 @@ function startServer(options) {
|
|
|
12357
12457
|
proxyContext,
|
|
12358
12458
|
providerRegistry,
|
|
12359
12459
|
runtimeConfig: runtimeConfig2
|
|
12360
|
-
} = bootstrapRuntime(port, serverLog);
|
|
12460
|
+
} = bootstrapRuntime(port, serverLog, drain.control);
|
|
12361
12461
|
stopRetentionJob = runStartupMaintenance(config, dbOps);
|
|
12362
12462
|
const fetchHandler = createServerFetchHandler({
|
|
12363
12463
|
apiRouter,
|
|
12364
12464
|
exposureConfig,
|
|
12365
12465
|
proxyContext,
|
|
12366
|
-
withDashboard
|
|
12466
|
+
withDashboard,
|
|
12467
|
+
isDeploymentDraining: drain.isDraining
|
|
12367
12468
|
});
|
|
12368
12469
|
try {
|
|
12369
12470
|
if (options?.additionalUnixSocket) {
|
|
@@ -12402,6 +12503,7 @@ function startServer(options) {
|
|
|
12402
12503
|
advanceRoutingClock: () => {
|
|
12403
12504
|
proxyContext.teamAdmission?.advance();
|
|
12404
12505
|
proxyContext.quotaHold?.advance();
|
|
12506
|
+
checkShutdownDrain?.();
|
|
12405
12507
|
}
|
|
12406
12508
|
};
|
|
12407
12509
|
}
|
|
@@ -12416,7 +12518,7 @@ async function handleGracefulShutdown(signal) {
|
|
|
12416
12518
|
process.exit(1);
|
|
12417
12519
|
}
|
|
12418
12520
|
}
|
|
12419
|
-
var serverLog, lifecycleLog, serverInstance = null, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null;
|
|
12521
|
+
var serverLog, lifecycleLog, serverInstance = null, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null, deploymentDrain, checkShutdownDrain;
|
|
12420
12522
|
var init_src11 = __esm(() => {
|
|
12421
12523
|
init_src9();
|
|
12422
12524
|
init_src2();
|
|
@@ -12425,6 +12527,7 @@ var init_src11 = __esm(() => {
|
|
|
12425
12527
|
init_src10();
|
|
12426
12528
|
init_bootstrap_runtime();
|
|
12427
12529
|
init_dashboard_assets();
|
|
12530
|
+
init_deployment_drain();
|
|
12428
12531
|
init_fetch_handler();
|
|
12429
12532
|
init_startup_banner();
|
|
12430
12533
|
init_startup_maintenance();
|
package/dist/edge.js
CHANGED
package/dist/https.js
CHANGED
|
@@ -129,6 +129,7 @@ var REQUEST_HEADERS = [
|
|
|
129
129
|
"session-id",
|
|
130
130
|
"openai-beta",
|
|
131
131
|
"x-client-request-id",
|
|
132
|
+
"x-stainless-retry-count",
|
|
132
133
|
"user-agent"
|
|
133
134
|
];
|
|
134
135
|
var RESPONSE_HEADERS = [
|
|
@@ -367,17 +368,15 @@ async function startStandalone(options, poolEntry, env = process.env) {
|
|
|
367
368
|
let stopPromise;
|
|
368
369
|
const stop = () => stopPromise ??= (async () => {
|
|
369
370
|
stopping = true;
|
|
370
|
-
|
|
371
|
-
await bounded(new Promise((resolve) => server.close(() => resolve())), 1e4);
|
|
372
|
-
} catch {
|
|
373
|
-
server.closeAllConnections();
|
|
374
|
-
}
|
|
371
|
+
const publicClosed = new Promise((resolve) => server.close(() => resolve()));
|
|
375
372
|
if (!exited)
|
|
376
373
|
child.kill("SIGTERM");
|
|
377
374
|
try {
|
|
378
|
-
await bounded(closed,
|
|
375
|
+
await bounded(Promise.all([publicClosed, closed]), 28000);
|
|
379
376
|
} catch {
|
|
380
|
-
|
|
377
|
+
server.closeAllConnections();
|
|
378
|
+
if (!exited)
|
|
379
|
+
child.kill("SIGKILL");
|
|
381
380
|
await closed;
|
|
382
381
|
}
|
|
383
382
|
})();
|
package/dist/pool.js
CHANGED
|
@@ -238,9 +238,11 @@ var init_errors = __esm(() => {
|
|
|
238
238
|
};
|
|
239
239
|
ServiceUnavailableError = class ServiceUnavailableError extends AppError {
|
|
240
240
|
service;
|
|
241
|
-
|
|
241
|
+
retryAfterSeconds;
|
|
242
|
+
constructor(message, service, retryAfterSeconds) {
|
|
242
243
|
super(message, "SERVICE_UNAVAILABLE", 503, { service });
|
|
243
244
|
this.service = service;
|
|
245
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
244
246
|
}
|
|
245
247
|
};
|
|
246
248
|
});
|
|
@@ -1105,7 +1107,8 @@ function errorResponse(error) {
|
|
|
1105
1107
|
}
|
|
1106
1108
|
if (error instanceof Error && "statusCode" in error && typeof error.statusCode === "number") {
|
|
1107
1109
|
const details = "context" in error && error.context !== undefined ? { details: error.context } : {};
|
|
1108
|
-
|
|
1110
|
+
const retryAfter = error.statusCode === 503 && "retryAfterSeconds" in error && typeof error.retryAfterSeconds === "number" && Number.isSafeInteger(error.retryAfterSeconds) && error.retryAfterSeconds > 0 ? { "Retry-After": String(error.retryAfterSeconds) } : undefined;
|
|
1111
|
+
return jsonResponse({ error: error.message, ...details }, error.statusCode, retryAfter);
|
|
1109
1112
|
}
|
|
1110
1113
|
const message = error instanceof Error ? error.message : "Internal server error";
|
|
1111
1114
|
const status = 500;
|
|
@@ -1931,7 +1934,7 @@ function utilization(value) {
|
|
|
1931
1934
|
const raw = number(value.used_percentage ?? value.usedPercentage ?? value.percent ?? value.utilization);
|
|
1932
1935
|
if (raw === null)
|
|
1933
1936
|
return null;
|
|
1934
|
-
return raw
|
|
1937
|
+
return raw / 100;
|
|
1935
1938
|
}
|
|
1936
1939
|
function resetAt(value) {
|
|
1937
1940
|
const raw = value.resets_at ?? value.reset_at ?? value.resetAt;
|
|
@@ -8110,6 +8113,18 @@ class APIRouter {
|
|
|
8110
8113
|
this.registerHandlers();
|
|
8111
8114
|
}
|
|
8112
8115
|
registerHandlers() {
|
|
8116
|
+
if (this.context.deploymentDrain) {
|
|
8117
|
+
const control = this.context.deploymentDrain;
|
|
8118
|
+
for (const [method, action] of [
|
|
8119
|
+
["GET", "status"],
|
|
8120
|
+
["POST", "start"],
|
|
8121
|
+
["DELETE", "resume"]
|
|
8122
|
+
]) {
|
|
8123
|
+
this.staticHandlers.set(`${method}:/api/drain`, () => Response.json(control(action), {
|
|
8124
|
+
headers: { "cache-control": "no-store" }
|
|
8125
|
+
}));
|
|
8126
|
+
}
|
|
8127
|
+
}
|
|
8113
8128
|
if (this.enrollment) {
|
|
8114
8129
|
this.staticHandlers.set("POST:/api/enrollment-invitations", this.enrollment.issue);
|
|
8115
8130
|
this.staticHandlers.set("POST:/api/enrollment-emails", this.enrollment.emails);
|
|
@@ -8144,6 +8159,9 @@ class APIRouter {
|
|
|
8144
8159
|
this.staticHandlers.set("GET:/api/stats", () => statsHandler());
|
|
8145
8160
|
this.staticHandlers.set("GET:/api/status", () => fleetStatusHandler());
|
|
8146
8161
|
this.staticHandlers.set("GET:/api/pool-status", (_req, url) => {
|
|
8162
|
+
if (this.context.deploymentDrain?.("status").draining) {
|
|
8163
|
+
return errorResponse(new ServiceUnavailableError("Pool is draining for deployment", undefined, 30));
|
|
8164
|
+
}
|
|
8147
8165
|
const provider = url.searchParams.get("provider") ?? "";
|
|
8148
8166
|
const model = url.searchParams.get("model") ?? undefined;
|
|
8149
8167
|
if (!isAccountProvider(provider) || !getProviders().includes(provider) || model !== undefined && (model.trim().length === 0 || model.length > 200)) {
|
|
@@ -9833,6 +9851,15 @@ var init_proxy_operations = __esm(() => {
|
|
|
9833
9851
|
});
|
|
9834
9852
|
|
|
9835
9853
|
// ../../packages/proxy/src/handlers/proxy-attempts.ts
|
|
9854
|
+
function admissionRetrySeconds(req) {
|
|
9855
|
+
if (req.signal.aborted)
|
|
9856
|
+
return;
|
|
9857
|
+
const count = req.headers.get("x-stainless-retry-count") ?? "0";
|
|
9858
|
+
if (!/^\d+$/.test(count))
|
|
9859
|
+
return 1;
|
|
9860
|
+
const ordinal = Number(count);
|
|
9861
|
+
return ordinal === 0 ? 1 : ordinal === 1 ? 10 : 30;
|
|
9862
|
+
}
|
|
9836
9863
|
function createBodyStream(body) {
|
|
9837
9864
|
if (!body)
|
|
9838
9865
|
return;
|
|
@@ -9892,7 +9919,7 @@ async function attemptAccount(request2, account2, failoverAttempts) {
|
|
|
9892
9919
|
permit = request2.ctx.runtime.teamRouting.enabled ? await admission?.acquire(account2.id, request2.ctx.runtime.teamRouting, request2.req.signal) : undefined;
|
|
9893
9920
|
} catch (error) {
|
|
9894
9921
|
if (error instanceof TeamAdmissionError) {
|
|
9895
|
-
throw new ServiceUnavailableError(error.message, request2.ctx.providerName);
|
|
9922
|
+
throw new ServiceUnavailableError(error.message, request2.ctx.providerName, admissionRetrySeconds(request2.req));
|
|
9896
9923
|
}
|
|
9897
9924
|
throw error;
|
|
9898
9925
|
}
|
|
@@ -11863,7 +11890,7 @@ function exposureFailure(req, url, server, config) {
|
|
|
11863
11890
|
if (!isSameOriginManagementRequest(req, url)) {
|
|
11864
11891
|
return jsonError(403, "Cross-origin management requests are not allowed");
|
|
11865
11892
|
}
|
|
11866
|
-
if (MUTATING_METHODS.has(req.method)) {
|
|
11893
|
+
if (url.pathname === "/api/drain" || MUTATING_METHODS.has(req.method)) {
|
|
11867
11894
|
if (role !== "admin") {
|
|
11868
11895
|
return jsonError(401, "An admin key is required for management writes");
|
|
11869
11896
|
}
|
|
@@ -11920,7 +11947,7 @@ function wireStrategyHotReload(config, log26, dbOps, proxyContext, runtimeConfig
|
|
|
11920
11947
|
}
|
|
11921
11948
|
});
|
|
11922
11949
|
}
|
|
11923
|
-
function bootstrapRuntime(port, serverLog) {
|
|
11950
|
+
function bootstrapRuntime(port, serverLog, deploymentDrain) {
|
|
11924
11951
|
container.registerInstance(SERVICE_KEYS.Config, new Config);
|
|
11925
11952
|
container.registerInstance(SERVICE_KEYS.Logger, serverLog);
|
|
11926
11953
|
const config = container.resolve(SERVICE_KEYS.Config);
|
|
@@ -11939,6 +11966,7 @@ function bootstrapRuntime(port, serverLog) {
|
|
|
11939
11966
|
container.registerInstance(SERVICE_KEYS.PricingLogger, pricingLogger);
|
|
11940
11967
|
setPricingLogger(pricingLogger);
|
|
11941
11968
|
const apiRouter = new APIRouter({
|
|
11969
|
+
deploymentDrain,
|
|
11942
11970
|
enrollmentOrigin: process.env.AGENT_SMITH_ENROLLMENT_ORIGIN,
|
|
11943
11971
|
config,
|
|
11944
11972
|
dbOps,
|
|
@@ -12099,6 +12127,48 @@ var init_dashboard_assets = __esm(() => {
|
|
|
12099
12127
|
init_src2();
|
|
12100
12128
|
});
|
|
12101
12129
|
|
|
12130
|
+
// ../../packages/runtime-server/src/deployment-drain.ts
|
|
12131
|
+
class DeploymentDrain {
|
|
12132
|
+
activity;
|
|
12133
|
+
now;
|
|
12134
|
+
expiresAt = null;
|
|
12135
|
+
stopping = false;
|
|
12136
|
+
log = new Logger("DeploymentDrain");
|
|
12137
|
+
constructor(activity, now = () => Date.now()) {
|
|
12138
|
+
this.activity = activity;
|
|
12139
|
+
this.now = now;
|
|
12140
|
+
}
|
|
12141
|
+
isDraining = () => {
|
|
12142
|
+
if (this.expiresAt !== null && this.now() >= this.expiresAt) {
|
|
12143
|
+
this.expiresAt = null;
|
|
12144
|
+
this.log.info("Deployment pause expired; accepting new inference");
|
|
12145
|
+
}
|
|
12146
|
+
return this.stopping || this.expiresAt !== null;
|
|
12147
|
+
};
|
|
12148
|
+
control = (action) => {
|
|
12149
|
+
if (action === "start" && !this.stopping) {
|
|
12150
|
+
if (!this.isDraining())
|
|
12151
|
+
this.log.info("Deployment pause started");
|
|
12152
|
+
this.expiresAt = this.now() + 120000;
|
|
12153
|
+
} else if (action === "resume" && !this.stopping) {
|
|
12154
|
+
this.expiresAt = null;
|
|
12155
|
+
this.log.info("Deployment pause cancelled; accepting new inference");
|
|
12156
|
+
}
|
|
12157
|
+
return {
|
|
12158
|
+
draining: this.isDraining(),
|
|
12159
|
+
expiresAt: this.expiresAt,
|
|
12160
|
+
...this.activity()
|
|
12161
|
+
};
|
|
12162
|
+
};
|
|
12163
|
+
shutdown() {
|
|
12164
|
+
this.stopping = true;
|
|
12165
|
+
this.expiresAt = null;
|
|
12166
|
+
}
|
|
12167
|
+
}
|
|
12168
|
+
var init_deployment_drain = __esm(() => {
|
|
12169
|
+
init_src4();
|
|
12170
|
+
});
|
|
12171
|
+
|
|
12102
12172
|
// ../../packages/runtime-server/src/oauth-token.ts
|
|
12103
12173
|
async function forwardOAuthTokenRequest(req, body) {
|
|
12104
12174
|
const headers3 = new Headers(req.headers);
|
|
@@ -12147,7 +12217,8 @@ function createServerFetchHandler({
|
|
|
12147
12217
|
handleProxyRequest = handleProxy,
|
|
12148
12218
|
handleWebSocketUpgrade = handleWebSocketUpgradeRequest,
|
|
12149
12219
|
handleOAuthTokenRequest = forwardOAuthTokenRequest,
|
|
12150
|
-
serveDashboardAsset = serveDashboardRoute
|
|
12220
|
+
serveDashboardAsset = serveDashboardRoute,
|
|
12221
|
+
isDeploymentDraining = () => false
|
|
12151
12222
|
}) {
|
|
12152
12223
|
return async (req, server) => {
|
|
12153
12224
|
const url = new URL(req.url);
|
|
@@ -12175,6 +12246,9 @@ function createServerFetchHandler({
|
|
|
12175
12246
|
}
|
|
12176
12247
|
const isProviderRoute = url.pathname === "/v1" || url.pathname.startsWith("/v1/") || url.pathname.startsWith("/tc-acct/");
|
|
12177
12248
|
if (isProviderRoute) {
|
|
12249
|
+
if (isDeploymentDraining()) {
|
|
12250
|
+
return errorResponse(new ServiceUnavailableError("Pool is draining for deployment", undefined, 30));
|
|
12251
|
+
}
|
|
12178
12252
|
if (server) {
|
|
12179
12253
|
const websocketResponse = await handleWebSocketUpgrade(req, url, proxyContext, server);
|
|
12180
12254
|
if (websocketResponse) {
|
|
@@ -12289,17 +12363,38 @@ async function stopServerRuntime() {
|
|
|
12289
12363
|
const errors2 = [];
|
|
12290
12364
|
const activeServer = serverInstance;
|
|
12291
12365
|
const activeUnixServer = unixServerInstance;
|
|
12292
|
-
|
|
12293
|
-
|
|
12366
|
+
deploymentDrain?.shutdown();
|
|
12367
|
+
const listeners = [activeUnixServer, activeServer].filter((listener) => listener !== null);
|
|
12368
|
+
const shutdownDeadline = Date.now() + 25000;
|
|
12294
12369
|
try {
|
|
12295
|
-
await
|
|
12296
|
-
|
|
12297
|
-
|
|
12298
|
-
|
|
12299
|
-
|
|
12300
|
-
|
|
12301
|
-
|
|
12302
|
-
|
|
12370
|
+
await new Promise((resolve3) => {
|
|
12371
|
+
let timer;
|
|
12372
|
+
const finish = () => {
|
|
12373
|
+
clearInterval(timer);
|
|
12374
|
+
resolve3();
|
|
12375
|
+
};
|
|
12376
|
+
const check = () => {
|
|
12377
|
+
const pending = listeners.some((listener) => listener.pendingRequests > 0 || listener.pendingWebSockets > 0);
|
|
12378
|
+
if (!pending)
|
|
12379
|
+
finish();
|
|
12380
|
+
else if (Date.now() >= shutdownDeadline) {
|
|
12381
|
+
lifecycleLog.warn("Shutdown grace expired; closing remaining connections");
|
|
12382
|
+
finish();
|
|
12383
|
+
}
|
|
12384
|
+
};
|
|
12385
|
+
checkShutdownDrain = check;
|
|
12386
|
+
timer = setInterval(check, 50);
|
|
12387
|
+
check();
|
|
12388
|
+
});
|
|
12389
|
+
const stopped = await Promise.allSettled(listeners.map((listener) => listener.stop(true)));
|
|
12390
|
+
for (const result of stopped) {
|
|
12391
|
+
if (result.status === "rejected")
|
|
12392
|
+
errors2.push(toError("Failed to stop listener", result.reason));
|
|
12393
|
+
}
|
|
12394
|
+
} finally {
|
|
12395
|
+
checkShutdownDrain = undefined;
|
|
12396
|
+
serverInstance = null;
|
|
12397
|
+
unixServerInstance = null;
|
|
12303
12398
|
}
|
|
12304
12399
|
stopRetentionMaintenance();
|
|
12305
12400
|
try {
|
|
@@ -12347,6 +12442,11 @@ function startServer(options) {
|
|
|
12347
12442
|
if (withDashboard) {
|
|
12348
12443
|
loadDashboardAssets();
|
|
12349
12444
|
}
|
|
12445
|
+
const drain = new DeploymentDrain(() => ({
|
|
12446
|
+
activeRequests: Math.max(0, (serverInstance?.pendingRequests ?? 0) + (unixServerInstance?.pendingRequests ?? 0) - 1),
|
|
12447
|
+
activeWebSockets: (serverInstance?.pendingWebSockets ?? 0) + (unixServerInstance?.pendingWebSockets ?? 0)
|
|
12448
|
+
}));
|
|
12449
|
+
deploymentDrain = drain;
|
|
12350
12450
|
const {
|
|
12351
12451
|
config,
|
|
12352
12452
|
dbOps,
|
|
@@ -12356,13 +12456,14 @@ function startServer(options) {
|
|
|
12356
12456
|
proxyContext,
|
|
12357
12457
|
providerRegistry,
|
|
12358
12458
|
runtimeConfig: runtimeConfig2
|
|
12359
|
-
} = bootstrapRuntime(port, serverLog);
|
|
12459
|
+
} = bootstrapRuntime(port, serverLog, drain.control);
|
|
12360
12460
|
stopRetentionJob = runStartupMaintenance(config, dbOps);
|
|
12361
12461
|
const fetchHandler = createServerFetchHandler({
|
|
12362
12462
|
apiRouter,
|
|
12363
12463
|
exposureConfig,
|
|
12364
12464
|
proxyContext,
|
|
12365
|
-
withDashboard
|
|
12465
|
+
withDashboard,
|
|
12466
|
+
isDeploymentDraining: drain.isDraining
|
|
12366
12467
|
});
|
|
12367
12468
|
try {
|
|
12368
12469
|
if (options?.additionalUnixSocket) {
|
|
@@ -12401,6 +12502,7 @@ function startServer(options) {
|
|
|
12401
12502
|
advanceRoutingClock: () => {
|
|
12402
12503
|
proxyContext.teamAdmission?.advance();
|
|
12403
12504
|
proxyContext.quotaHold?.advance();
|
|
12505
|
+
checkShutdownDrain?.();
|
|
12404
12506
|
}
|
|
12405
12507
|
};
|
|
12406
12508
|
}
|
|
@@ -12415,7 +12517,7 @@ async function handleGracefulShutdown(signal) {
|
|
|
12415
12517
|
process.exit(1);
|
|
12416
12518
|
}
|
|
12417
12519
|
}
|
|
12418
|
-
var serverLog, lifecycleLog, serverInstance = null, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null;
|
|
12520
|
+
var serverLog, lifecycleLog, serverInstance = null, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null, deploymentDrain, checkShutdownDrain;
|
|
12419
12521
|
var init_src11 = __esm(() => {
|
|
12420
12522
|
init_src9();
|
|
12421
12523
|
init_src2();
|
|
@@ -12424,6 +12526,7 @@ var init_src11 = __esm(() => {
|
|
|
12424
12526
|
init_src10();
|
|
12425
12527
|
init_bootstrap_runtime();
|
|
12426
12528
|
init_dashboard_assets();
|
|
12529
|
+
init_deployment_drain();
|
|
12427
12530
|
init_fetch_handler();
|
|
12428
12531
|
init_startup_banner();
|
|
12429
12532
|
init_startup_maintenance();
|
|
@@ -238,9 +238,11 @@ var init_errors = __esm(() => {
|
|
|
238
238
|
};
|
|
239
239
|
ServiceUnavailableError = class ServiceUnavailableError extends AppError {
|
|
240
240
|
service;
|
|
241
|
-
|
|
241
|
+
retryAfterSeconds;
|
|
242
|
+
constructor(message, service, retryAfterSeconds) {
|
|
242
243
|
super(message, "SERVICE_UNAVAILABLE", 503, { service });
|
|
243
244
|
this.service = service;
|
|
245
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
244
246
|
}
|
|
245
247
|
};
|
|
246
248
|
});
|
package/dist/server.js
CHANGED
|
@@ -238,9 +238,11 @@ var init_errors = __esm(() => {
|
|
|
238
238
|
};
|
|
239
239
|
ServiceUnavailableError = class ServiceUnavailableError extends AppError {
|
|
240
240
|
service;
|
|
241
|
-
|
|
241
|
+
retryAfterSeconds;
|
|
242
|
+
constructor(message, service, retryAfterSeconds) {
|
|
242
243
|
super(message, "SERVICE_UNAVAILABLE", 503, { service });
|
|
243
244
|
this.service = service;
|
|
245
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
244
246
|
}
|
|
245
247
|
};
|
|
246
248
|
});
|
|
@@ -1105,7 +1107,8 @@ function errorResponse(error) {
|
|
|
1105
1107
|
}
|
|
1106
1108
|
if (error instanceof Error && "statusCode" in error && typeof error.statusCode === "number") {
|
|
1107
1109
|
const details = "context" in error && error.context !== undefined ? { details: error.context } : {};
|
|
1108
|
-
|
|
1110
|
+
const retryAfter = error.statusCode === 503 && "retryAfterSeconds" in error && typeof error.retryAfterSeconds === "number" && Number.isSafeInteger(error.retryAfterSeconds) && error.retryAfterSeconds > 0 ? { "Retry-After": String(error.retryAfterSeconds) } : undefined;
|
|
1111
|
+
return jsonResponse({ error: error.message, ...details }, error.statusCode, retryAfter);
|
|
1109
1112
|
}
|
|
1110
1113
|
const message = error instanceof Error ? error.message : "Internal server error";
|
|
1111
1114
|
const status = 500;
|
|
@@ -1931,7 +1934,7 @@ function utilization(value) {
|
|
|
1931
1934
|
const raw = number(value.used_percentage ?? value.usedPercentage ?? value.percent ?? value.utilization);
|
|
1932
1935
|
if (raw === null)
|
|
1933
1936
|
return null;
|
|
1934
|
-
return raw
|
|
1937
|
+
return raw / 100;
|
|
1935
1938
|
}
|
|
1936
1939
|
function resetAt(value) {
|
|
1937
1940
|
const raw = value.resets_at ?? value.reset_at ?? value.resetAt;
|
|
@@ -8110,6 +8113,18 @@ class APIRouter {
|
|
|
8110
8113
|
this.registerHandlers();
|
|
8111
8114
|
}
|
|
8112
8115
|
registerHandlers() {
|
|
8116
|
+
if (this.context.deploymentDrain) {
|
|
8117
|
+
const control = this.context.deploymentDrain;
|
|
8118
|
+
for (const [method, action] of [
|
|
8119
|
+
["GET", "status"],
|
|
8120
|
+
["POST", "start"],
|
|
8121
|
+
["DELETE", "resume"]
|
|
8122
|
+
]) {
|
|
8123
|
+
this.staticHandlers.set(`${method}:/api/drain`, () => Response.json(control(action), {
|
|
8124
|
+
headers: { "cache-control": "no-store" }
|
|
8125
|
+
}));
|
|
8126
|
+
}
|
|
8127
|
+
}
|
|
8113
8128
|
if (this.enrollment) {
|
|
8114
8129
|
this.staticHandlers.set("POST:/api/enrollment-invitations", this.enrollment.issue);
|
|
8115
8130
|
this.staticHandlers.set("POST:/api/enrollment-emails", this.enrollment.emails);
|
|
@@ -8144,6 +8159,9 @@ class APIRouter {
|
|
|
8144
8159
|
this.staticHandlers.set("GET:/api/stats", () => statsHandler());
|
|
8145
8160
|
this.staticHandlers.set("GET:/api/status", () => fleetStatusHandler());
|
|
8146
8161
|
this.staticHandlers.set("GET:/api/pool-status", (_req, url) => {
|
|
8162
|
+
if (this.context.deploymentDrain?.("status").draining) {
|
|
8163
|
+
return errorResponse(new ServiceUnavailableError("Pool is draining for deployment", undefined, 30));
|
|
8164
|
+
}
|
|
8147
8165
|
const provider = url.searchParams.get("provider") ?? "";
|
|
8148
8166
|
const model = url.searchParams.get("model") ?? undefined;
|
|
8149
8167
|
if (!isAccountProvider(provider) || !getProviders().includes(provider) || model !== undefined && (model.trim().length === 0 || model.length > 200)) {
|
|
@@ -9833,6 +9851,15 @@ var init_proxy_operations = __esm(() => {
|
|
|
9833
9851
|
});
|
|
9834
9852
|
|
|
9835
9853
|
// ../../packages/proxy/src/handlers/proxy-attempts.ts
|
|
9854
|
+
function admissionRetrySeconds(req) {
|
|
9855
|
+
if (req.signal.aborted)
|
|
9856
|
+
return;
|
|
9857
|
+
const count = req.headers.get("x-stainless-retry-count") ?? "0";
|
|
9858
|
+
if (!/^\d+$/.test(count))
|
|
9859
|
+
return 1;
|
|
9860
|
+
const ordinal = Number(count);
|
|
9861
|
+
return ordinal === 0 ? 1 : ordinal === 1 ? 10 : 30;
|
|
9862
|
+
}
|
|
9836
9863
|
function createBodyStream(body) {
|
|
9837
9864
|
if (!body)
|
|
9838
9865
|
return;
|
|
@@ -9892,7 +9919,7 @@ async function attemptAccount(request2, account2, failoverAttempts) {
|
|
|
9892
9919
|
permit = request2.ctx.runtime.teamRouting.enabled ? await admission?.acquire(account2.id, request2.ctx.runtime.teamRouting, request2.req.signal) : undefined;
|
|
9893
9920
|
} catch (error) {
|
|
9894
9921
|
if (error instanceof TeamAdmissionError) {
|
|
9895
|
-
throw new ServiceUnavailableError(error.message, request2.ctx.providerName);
|
|
9922
|
+
throw new ServiceUnavailableError(error.message, request2.ctx.providerName, admissionRetrySeconds(request2.req));
|
|
9896
9923
|
}
|
|
9897
9924
|
throw error;
|
|
9898
9925
|
}
|
|
@@ -11863,7 +11890,7 @@ function exposureFailure(req, url, server, config) {
|
|
|
11863
11890
|
if (!isSameOriginManagementRequest(req, url)) {
|
|
11864
11891
|
return jsonError(403, "Cross-origin management requests are not allowed");
|
|
11865
11892
|
}
|
|
11866
|
-
if (MUTATING_METHODS.has(req.method)) {
|
|
11893
|
+
if (url.pathname === "/api/drain" || MUTATING_METHODS.has(req.method)) {
|
|
11867
11894
|
if (role !== "admin") {
|
|
11868
11895
|
return jsonError(401, "An admin key is required for management writes");
|
|
11869
11896
|
}
|
|
@@ -11920,7 +11947,7 @@ function wireStrategyHotReload(config, log26, dbOps, proxyContext, runtimeConfig
|
|
|
11920
11947
|
}
|
|
11921
11948
|
});
|
|
11922
11949
|
}
|
|
11923
|
-
function bootstrapRuntime(port, serverLog) {
|
|
11950
|
+
function bootstrapRuntime(port, serverLog, deploymentDrain) {
|
|
11924
11951
|
container.registerInstance(SERVICE_KEYS.Config, new Config);
|
|
11925
11952
|
container.registerInstance(SERVICE_KEYS.Logger, serverLog);
|
|
11926
11953
|
const config = container.resolve(SERVICE_KEYS.Config);
|
|
@@ -11939,6 +11966,7 @@ function bootstrapRuntime(port, serverLog) {
|
|
|
11939
11966
|
container.registerInstance(SERVICE_KEYS.PricingLogger, pricingLogger);
|
|
11940
11967
|
setPricingLogger(pricingLogger);
|
|
11941
11968
|
const apiRouter = new APIRouter({
|
|
11969
|
+
deploymentDrain,
|
|
11942
11970
|
enrollmentOrigin: process.env.AGENT_SMITH_ENROLLMENT_ORIGIN,
|
|
11943
11971
|
config,
|
|
11944
11972
|
dbOps,
|
|
@@ -12099,6 +12127,48 @@ var init_dashboard_assets = __esm(() => {
|
|
|
12099
12127
|
init_src2();
|
|
12100
12128
|
});
|
|
12101
12129
|
|
|
12130
|
+
// ../../packages/runtime-server/src/deployment-drain.ts
|
|
12131
|
+
class DeploymentDrain {
|
|
12132
|
+
activity;
|
|
12133
|
+
now;
|
|
12134
|
+
expiresAt = null;
|
|
12135
|
+
stopping = false;
|
|
12136
|
+
log = new Logger("DeploymentDrain");
|
|
12137
|
+
constructor(activity, now = () => Date.now()) {
|
|
12138
|
+
this.activity = activity;
|
|
12139
|
+
this.now = now;
|
|
12140
|
+
}
|
|
12141
|
+
isDraining = () => {
|
|
12142
|
+
if (this.expiresAt !== null && this.now() >= this.expiresAt) {
|
|
12143
|
+
this.expiresAt = null;
|
|
12144
|
+
this.log.info("Deployment pause expired; accepting new inference");
|
|
12145
|
+
}
|
|
12146
|
+
return this.stopping || this.expiresAt !== null;
|
|
12147
|
+
};
|
|
12148
|
+
control = (action) => {
|
|
12149
|
+
if (action === "start" && !this.stopping) {
|
|
12150
|
+
if (!this.isDraining())
|
|
12151
|
+
this.log.info("Deployment pause started");
|
|
12152
|
+
this.expiresAt = this.now() + 120000;
|
|
12153
|
+
} else if (action === "resume" && !this.stopping) {
|
|
12154
|
+
this.expiresAt = null;
|
|
12155
|
+
this.log.info("Deployment pause cancelled; accepting new inference");
|
|
12156
|
+
}
|
|
12157
|
+
return {
|
|
12158
|
+
draining: this.isDraining(),
|
|
12159
|
+
expiresAt: this.expiresAt,
|
|
12160
|
+
...this.activity()
|
|
12161
|
+
};
|
|
12162
|
+
};
|
|
12163
|
+
shutdown() {
|
|
12164
|
+
this.stopping = true;
|
|
12165
|
+
this.expiresAt = null;
|
|
12166
|
+
}
|
|
12167
|
+
}
|
|
12168
|
+
var init_deployment_drain = __esm(() => {
|
|
12169
|
+
init_src4();
|
|
12170
|
+
});
|
|
12171
|
+
|
|
12102
12172
|
// ../../packages/runtime-server/src/oauth-token.ts
|
|
12103
12173
|
async function forwardOAuthTokenRequest(req, body) {
|
|
12104
12174
|
const headers3 = new Headers(req.headers);
|
|
@@ -12147,7 +12217,8 @@ function createServerFetchHandler({
|
|
|
12147
12217
|
handleProxyRequest = handleProxy,
|
|
12148
12218
|
handleWebSocketUpgrade = handleWebSocketUpgradeRequest,
|
|
12149
12219
|
handleOAuthTokenRequest = forwardOAuthTokenRequest,
|
|
12150
|
-
serveDashboardAsset = serveDashboardRoute
|
|
12220
|
+
serveDashboardAsset = serveDashboardRoute,
|
|
12221
|
+
isDeploymentDraining = () => false
|
|
12151
12222
|
}) {
|
|
12152
12223
|
return async (req, server) => {
|
|
12153
12224
|
const url = new URL(req.url);
|
|
@@ -12175,6 +12246,9 @@ function createServerFetchHandler({
|
|
|
12175
12246
|
}
|
|
12176
12247
|
const isProviderRoute = url.pathname === "/v1" || url.pathname.startsWith("/v1/") || url.pathname.startsWith("/tc-acct/");
|
|
12177
12248
|
if (isProviderRoute) {
|
|
12249
|
+
if (isDeploymentDraining()) {
|
|
12250
|
+
return errorResponse(new ServiceUnavailableError("Pool is draining for deployment", undefined, 30));
|
|
12251
|
+
}
|
|
12178
12252
|
if (server) {
|
|
12179
12253
|
const websocketResponse = await handleWebSocketUpgrade(req, url, proxyContext, server);
|
|
12180
12254
|
if (websocketResponse) {
|
|
@@ -12289,17 +12363,38 @@ async function stopServerRuntime() {
|
|
|
12289
12363
|
const errors2 = [];
|
|
12290
12364
|
const activeServer = serverInstance;
|
|
12291
12365
|
const activeUnixServer = unixServerInstance;
|
|
12292
|
-
|
|
12293
|
-
|
|
12366
|
+
deploymentDrain?.shutdown();
|
|
12367
|
+
const listeners = [activeUnixServer, activeServer].filter((listener) => listener !== null);
|
|
12368
|
+
const shutdownDeadline = Date.now() + 25000;
|
|
12294
12369
|
try {
|
|
12295
|
-
await
|
|
12296
|
-
|
|
12297
|
-
|
|
12298
|
-
|
|
12299
|
-
|
|
12300
|
-
|
|
12301
|
-
|
|
12302
|
-
|
|
12370
|
+
await new Promise((resolve3) => {
|
|
12371
|
+
let timer;
|
|
12372
|
+
const finish = () => {
|
|
12373
|
+
clearInterval(timer);
|
|
12374
|
+
resolve3();
|
|
12375
|
+
};
|
|
12376
|
+
const check = () => {
|
|
12377
|
+
const pending = listeners.some((listener) => listener.pendingRequests > 0 || listener.pendingWebSockets > 0);
|
|
12378
|
+
if (!pending)
|
|
12379
|
+
finish();
|
|
12380
|
+
else if (Date.now() >= shutdownDeadline) {
|
|
12381
|
+
lifecycleLog.warn("Shutdown grace expired; closing remaining connections");
|
|
12382
|
+
finish();
|
|
12383
|
+
}
|
|
12384
|
+
};
|
|
12385
|
+
checkShutdownDrain = check;
|
|
12386
|
+
timer = setInterval(check, 50);
|
|
12387
|
+
check();
|
|
12388
|
+
});
|
|
12389
|
+
const stopped = await Promise.allSettled(listeners.map((listener) => listener.stop(true)));
|
|
12390
|
+
for (const result of stopped) {
|
|
12391
|
+
if (result.status === "rejected")
|
|
12392
|
+
errors2.push(toError("Failed to stop listener", result.reason));
|
|
12393
|
+
}
|
|
12394
|
+
} finally {
|
|
12395
|
+
checkShutdownDrain = undefined;
|
|
12396
|
+
serverInstance = null;
|
|
12397
|
+
unixServerInstance = null;
|
|
12303
12398
|
}
|
|
12304
12399
|
stopRetentionMaintenance();
|
|
12305
12400
|
try {
|
|
@@ -12347,6 +12442,11 @@ function startServer(options) {
|
|
|
12347
12442
|
if (withDashboard) {
|
|
12348
12443
|
loadDashboardAssets();
|
|
12349
12444
|
}
|
|
12445
|
+
const drain = new DeploymentDrain(() => ({
|
|
12446
|
+
activeRequests: Math.max(0, (serverInstance?.pendingRequests ?? 0) + (unixServerInstance?.pendingRequests ?? 0) - 1),
|
|
12447
|
+
activeWebSockets: (serverInstance?.pendingWebSockets ?? 0) + (unixServerInstance?.pendingWebSockets ?? 0)
|
|
12448
|
+
}));
|
|
12449
|
+
deploymentDrain = drain;
|
|
12350
12450
|
const {
|
|
12351
12451
|
config,
|
|
12352
12452
|
dbOps,
|
|
@@ -12356,13 +12456,14 @@ function startServer(options) {
|
|
|
12356
12456
|
proxyContext,
|
|
12357
12457
|
providerRegistry,
|
|
12358
12458
|
runtimeConfig: runtimeConfig2
|
|
12359
|
-
} = bootstrapRuntime(port, serverLog);
|
|
12459
|
+
} = bootstrapRuntime(port, serverLog, drain.control);
|
|
12360
12460
|
stopRetentionJob = runStartupMaintenance(config, dbOps);
|
|
12361
12461
|
const fetchHandler = createServerFetchHandler({
|
|
12362
12462
|
apiRouter,
|
|
12363
12463
|
exposureConfig,
|
|
12364
12464
|
proxyContext,
|
|
12365
|
-
withDashboard
|
|
12465
|
+
withDashboard,
|
|
12466
|
+
isDeploymentDraining: drain.isDraining
|
|
12366
12467
|
});
|
|
12367
12468
|
try {
|
|
12368
12469
|
if (options?.additionalUnixSocket) {
|
|
@@ -12401,6 +12502,7 @@ function startServer(options) {
|
|
|
12401
12502
|
advanceRoutingClock: () => {
|
|
12402
12503
|
proxyContext.teamAdmission?.advance();
|
|
12403
12504
|
proxyContext.quotaHold?.advance();
|
|
12505
|
+
checkShutdownDrain?.();
|
|
12404
12506
|
}
|
|
12405
12507
|
};
|
|
12406
12508
|
}
|
|
@@ -12415,7 +12517,7 @@ async function handleGracefulShutdown(signal) {
|
|
|
12415
12517
|
process.exit(1);
|
|
12416
12518
|
}
|
|
12417
12519
|
}
|
|
12418
|
-
var serverLog, lifecycleLog, serverInstance = null, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null;
|
|
12520
|
+
var serverLog, lifecycleLog, serverInstance = null, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null, deploymentDrain, checkShutdownDrain;
|
|
12419
12521
|
var init_src11 = __esm(() => {
|
|
12420
12522
|
init_src9();
|
|
12421
12523
|
init_src2();
|
|
@@ -12424,6 +12526,7 @@ var init_src11 = __esm(() => {
|
|
|
12424
12526
|
init_src10();
|
|
12425
12527
|
init_bootstrap_runtime();
|
|
12426
12528
|
init_dashboard_assets();
|
|
12529
|
+
init_deployment_drain();
|
|
12427
12530
|
init_fetch_handler();
|
|
12428
12531
|
init_startup_banner();
|
|
12429
12532
|
init_startup_maintenance();
|
package/package.json
CHANGED