fix(worker): improve environment variable handling and URL normalization
Update worker server to prioritize platform-specific PORT environment variable when setting the compute worker port. Add robust normalization for COMPUTE_WORKER_URL, ensuring proper scheme and validation, and trim trailing slashes for consistency. This enhances reliability in diverse deployment environments.
This commit is contained in:
parent
91d9fe47a5
commit
086329505f
2 changed files with 26 additions and 2 deletions
|
|
@ -422,7 +422,8 @@ async function createWorkerLoop<TPayload, TResult>(input: {
|
|||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const port = readIntEnv('COMPUTE_WORKER_PORT', 8081);
|
||||
const platformPort = readIntEnv('PORT', 8081);
|
||||
const port = readIntEnv('COMPUTE_WORKER_PORT', platformPort);
|
||||
const host = process.env.COMPUTE_WORKER_HOST?.trim() || '0.0.0.0';
|
||||
const workerToken = requireEnv('COMPUTE_WORKER_TOKEN');
|
||||
const natsUrl = requireEnv('NATS_URL');
|
||||
|
|
|
|||
|
|
@ -30,6 +30,29 @@ function readRequiredEnv(name: string): string {
|
|||
return value;
|
||||
}
|
||||
|
||||
function normalizeWorkerBaseUrl(raw: string): string {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) throw new Error('COMPUTE_WORKER_URL is empty');
|
||||
|
||||
const withScheme = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(trimmed)
|
||||
? trimmed
|
||||
: (/^(localhost|127(?:\.\d{1,3}){3})(:\d+)?(\/|$)/.test(trimmed)
|
||||
? `http://${trimmed}`
|
||||
: `https://${trimmed}`);
|
||||
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(withScheme);
|
||||
} catch {
|
||||
throw new Error(
|
||||
`Invalid COMPUTE_WORKER_URL="${raw}". Expected full URL like https://example.com (or http://localhost:4000).`,
|
||||
);
|
||||
}
|
||||
|
||||
parsed.pathname = parsed.pathname.replace(/\/+$/, '');
|
||||
return parsed.toString().replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
function parseRetryAfterMs(value: string | null): number | null {
|
||||
if (!value) return null;
|
||||
const asNum = Number(value);
|
||||
|
|
@ -82,7 +105,7 @@ export class WorkerComputeBackend implements ComputeBackend {
|
|||
private readonly retries: number;
|
||||
|
||||
constructor() {
|
||||
this.baseUrl = readRequiredEnv('COMPUTE_WORKER_URL').replace(/\/+$/, '');
|
||||
this.baseUrl = normalizeWorkerBaseUrl(readRequiredEnv('COMPUTE_WORKER_URL'));
|
||||
this.token = readRequiredEnv('COMPUTE_WORKER_TOKEN');
|
||||
this.waitTimeoutMs = DEFAULT_WAIT_TIMEOUT_MS;
|
||||
this.retries = DEFAULT_RETRIES;
|
||||
|
|
|
|||
Loading…
Reference in a new issue