web sockets and live logging
This commit is contained in:
@@ -19,10 +19,11 @@ class BuildController extends EventEmitter {
|
||||
private db: DB;
|
||||
private process: spawn.ChildProcess | null = null;
|
||||
private running: boolean = false;
|
||||
private interval: NodeJS.Timeout;
|
||||
|
||||
constructor(config = {}) {
|
||||
super();
|
||||
setInterval(this.triggerBuild, 60000);
|
||||
// this.interval = setInterval(this.triggerBuild, 60000);
|
||||
}
|
||||
|
||||
triggerBuild = () => {
|
||||
@@ -106,18 +107,18 @@ class BuildController extends EventEmitter {
|
||||
});
|
||||
docker.on('close', (code) => {
|
||||
this.process = null;
|
||||
const status = code === 0 ? 'success' : 'error';
|
||||
this.emitLog({
|
||||
id: build.id,
|
||||
type: 'finish',
|
||||
message: code
|
||||
message: status
|
||||
});
|
||||
this.db.finishBuild(build.id, status);
|
||||
|
||||
if (code === 0) {
|
||||
this.db.finishBuild(build.id, 'success');
|
||||
resolve();
|
||||
}
|
||||
else {
|
||||
this.db.finishBuild(build.id, 'error');
|
||||
reject(code);
|
||||
}
|
||||
});
|
||||
@@ -149,6 +150,12 @@ class BuildController extends EventEmitter {
|
||||
setDB = (db: DB) => {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
close = () => {
|
||||
if (this.interval) {
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default BuildController;
|
||||
|
25
src/Web.ts
25
src/Web.ts
@@ -1,10 +1,11 @@
|
||||
import * as http from "http";
|
||||
import crypto from 'crypto';
|
||||
import type { Express } from "express";
|
||||
import express, { application } from 'express';
|
||||
import express from 'express';
|
||||
import expressWs from "express-ws";
|
||||
import bodyParser from "body-parser";
|
||||
import type { DB } from "./DB.ts";
|
||||
import type { BuildController } from "./BuildController.ts";
|
||||
import type { BuildController, BuildEvent } from "./BuildController.ts";
|
||||
|
||||
interface WebConfig {
|
||||
port?: number;
|
||||
@@ -26,6 +27,7 @@ class Web {
|
||||
|
||||
constructor(options: WebConfig = {}) {
|
||||
const app: Express = this.app = express();
|
||||
const wsApp = expressWs(app).app;
|
||||
this.port = notStupidParseInt(process.env.PORT) || options['port'] as number || 8080;
|
||||
|
||||
app.set('trust proxy', 1);
|
||||
@@ -95,7 +97,8 @@ class Web {
|
||||
description: `Building ${build.repo} on ${build.distro}`
|
||||
},
|
||||
build,
|
||||
log
|
||||
log,
|
||||
ended: build.status !== 'queued' && build.status !== 'running'
|
||||
});
|
||||
});
|
||||
|
||||
@@ -112,6 +115,22 @@ class Web {
|
||||
app.get('/healthcheck', (_, res) => {
|
||||
res.send('Healthy');
|
||||
});
|
||||
|
||||
wsApp.ws('/build/:num/ws', (ws, req) => {
|
||||
console.log('WS Opened');
|
||||
const eventListener = (be: BuildEvent) => {
|
||||
if (be.id === notStupidParseInt(req.params.num)) {
|
||||
ws.send(JSON.stringify(be));
|
||||
}
|
||||
};
|
||||
this.buildController.on('log', eventListener);
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('WS Closed');
|
||||
this.buildController.removeListener('log', eventListener);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
close = () => {
|
||||
|
@@ -24,4 +24,5 @@ buildController.setDB(db);
|
||||
process.on('SIGTERM', () => {
|
||||
web.close();
|
||||
db.close();
|
||||
buildController.close();
|
||||
});
|
||||
|
Reference in New Issue
Block a user