Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions agents/src/http_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import { type IncomingMessage, type Server, type ServerResponse, createServer } from 'node:http';
import { log } from './log.js';

const healthCheck = async (res: ServerResponse) => {
res.writeHead(200);
res.end('OK');
};
export interface HealthCheckResult {
healthy: boolean;
message: string;
}

interface WorkerResponse {
agent_name: string;
Expand All @@ -23,13 +23,25 @@ export class HTTPServer {
app: Server;
#logger = log();

constructor(host: string, port: number, workerListener: () => WorkerResponse) {
constructor(
host: string,
port: number,
healthCheckListener: () => HealthCheckResult,
workerListener: () => WorkerResponse,
) {
this.host = host;
this.port = port;

this.app = createServer((req: IncomingMessage, res: ServerResponse) => {
if (req.url === '/') {
healthCheck(res);
const result = healthCheckListener();
if (result.healthy) {
res.writeHead(200);
res.end(result.message);
} else {
res.writeHead(503);
res.end(result.message);
}
} else if (req.url === '/worker') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(workerListener()));
Expand Down
4 changes: 4 additions & 0 deletions agents/src/ipc/supervised_proc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export abstract class SupervisedProc {
return this.#started;
}

get isAlive(): boolean {
return this.#started && !this.#closing && !!this.proc?.connected;
}

get runningJob(): RunningJobInfo | undefined {
return this.#runningJob;
}
Expand Down
31 changes: 24 additions & 7 deletions agents/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,30 @@ export class AgentServer {
);

this.#opts = opts;
this.#httpServer = new HTTPServer(opts.host, opts.port, () => ({
agent_name: opts.agentName,
worker_type: JobType[opts.serverType],
active_jobs: this.activeJobs.length,
sdk_version: version,
project_type: PROJECT_TYPE,
}));
this.#httpServer = new HTTPServer(
opts.host,
opts.port,
() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: now that there are two inline methods being declared as constructor arguments I'd prefer both methods being declared with a name so that it's easier to reason about what's going on on first glance.

// Check if inference executor exists and is not alive
if (this.#inferenceExecutor && !this.#inferenceExecutor.isAlive) {
return { healthy: false, message: 'inference process not running' };
}

// Check if we've lost connection to LiveKit (session closed while not intentionally closing)
if (!this.#closed && !this.#connecting && !this.#session) {
return { healthy: false, message: 'not connected to livekit' };
}

return { healthy: true, message: 'OK' };
},
() => ({
agent_name: opts.agentName,
worker_type: JobType[opts.serverType],
active_jobs: this.activeJobs.length,
sdk_version: version,
project_type: PROJECT_TYPE,
}),
);
}

/** @throws {@link WorkerError} if worker failed to connect or already running */
Expand Down