feat(core): Add health check endpoint to task runner server (no-changelog)

This commit is contained in:
Iván Ovejero 2024-11-22 13:40:11 +01:00
parent 0cbb46c8b9
commit db3b2ce038
No known key found for this signature in database
2 changed files with 35 additions and 0 deletions

View file

@ -9,6 +9,38 @@ import { TaskRunnerServer } from '@/runners/task-runner-server';
import type { TaskRunnerServerInitRequest } from '../runner-types'; import type { TaskRunnerServerInitRequest } from '../runner-types';
describe('TaskRunnerServer', () => { describe('TaskRunnerServer', () => {
describe('health endpoint', () => {
it('should send 200 status code', async () => {
const server = new TaskRunnerServer(
mock(),
mock<GlobalConfig>({ taskRunners: { path: '/runners' } }),
mock(),
mock(),
);
const mockResponse = { sendStatus: jest.fn() };
// @ts-expect-error Test
server.app = {
get: jest.fn().mockImplementation((path, handler) => {
if (path === '/runners/healthz') {
handler({}, mockResponse);
}
}),
post: jest.fn(),
stop: jest.fn().mockResolvedValue(undefined),
disable: jest.fn(),
use: jest.fn(),
};
await server.start();
expect(mockResponse.sendStatus).toHaveBeenCalledWith(200);
await server.stop(); // allow Jest to exit cleanly
});
});
describe('handleUpgradeRequest', () => { describe('handleUpgradeRequest', () => {
it('should close WebSocket when response status code is > 200', () => { it('should close WebSocket when response status code is > 200', () => {
const ws = mock<WebSocket>(); const ws = mock<WebSocket>();

View file

@ -163,6 +163,9 @@ export class TaskRunnerServer {
authEndpoint, authEndpoint,
send(async (req) => await this.taskRunnerAuthController.createGrantToken(req)), send(async (req) => await this.taskRunnerAuthController.createGrantToken(req)),
); );
const healthEndpoint = `${this.getEndpointBasePath()}/healthz`;
this.app.get(healthEndpoint, (_, res) => res.sendStatus(200));
} }
private handleUpgradeRequest = ( private handleUpgradeRequest = (