log table

This commit is contained in:
2025-01-13 01:58:38 -05:00
parent da8d0fcc7e
commit 68005e8492
8 changed files with 117 additions and 31 deletions

View File

@@ -91,8 +91,8 @@ class BuildController extends EventEmitter {
return (data: Buffer | string) => {
const str = data.toString();
const readyToLog = remainder[type] + str.substring(0, str.lastIndexOf('\n'));
remainder[type] = str.substring(str.lastIndexOf('\n'));
this.db.appendLog(build.id, readyToLog);
remainder[type] = str.substring(str.lastIndexOf('\n') + 1);
this.db.appendLog(build.id, type, readyToLog);
this.emitLog({
id: build.id,
type: type,
@@ -153,4 +153,4 @@ class BuildController extends EventEmitter {
export default BuildController;
export { BuildController };
export type { };
export type { BuildEvent, LogType };

View File

@@ -1,5 +1,6 @@
import { Sequelize, DataTypes, Op, } from 'sequelize';
import type { ModelStatic, Filterable } from 'sequelize';
import type { LogType } from './BuildController.ts';
type Status = 'queued' | 'running' | 'cancelled' | 'success' | 'error';
type Dependencies = 'stable' | 'testing' | 'staging';
@@ -23,7 +24,13 @@ interface Build {
endTime?: Date;
status: Status;
pid?: number;
log?: string;
}
interface LogChunk {
id: number
buildId: number
type: LogType,
chunk: string
}
const MONTH = 1000 * 60 * 60 * 24 * 24;
@@ -37,6 +44,7 @@ const SELECT = ['id', 'repo', 'commit', 'distro', 'dependencies', 'startTime', '
class DB {
private build: ModelStatic<any>;
private logChunk: ModelStatic<any>;
private sequelize: Sequelize;
constructor(config: DBConfig = {}) {
@@ -88,14 +96,42 @@ class DB {
pid: {
type: DataTypes.INTEGER,
allowNull: true
},
log: {
type: DataTypes.TEXT,
allowNull: true
}
});
this.build.sync();
this.logChunk = this.sequelize.define('logChunk', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
buildId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'builds',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
type: {
type: DataTypes.ENUM('std', 'err'),
allowNull: false,
defaultValue: 'std'
},
chunk: {
type: DataTypes.TEXT,
allowNull: false
}
});
this.sync();
}
private async sync(): Promise<void> {
await this.build.sync();
await this.logChunk.sync();
}
public async createBuild(repo: string, commit: string, patch: string, distro: string): Promise<number> {
@@ -132,13 +168,19 @@ class DB {
});
}
public async appendLog(id: number, log: string): Promise<void> {
const sanitizedLog = log.replace(/'/g, "''");
await this.build.update({
log: Sequelize.literal(`log || '${sanitizedLog}'`)
}, {
public async appendLog(buildId: number, type: LogType, chunk: string): Promise<void> {
await this.logChunk.create({
buildId,
type,
chunk
});
}
public async getLog(buildId: number): Promise<LogChunk[]> {
return await this.logChunk.findAll({
order: [['id', 'ASC']],
where: {
id
buildId
}
});
}
@@ -151,7 +193,7 @@ class DB {
return await this.build.findAll({
attributes: SELECT,
order: [['id', 'DESC']],
where: FRESH,
where: FRESH
});
}

View File

@@ -21,10 +21,12 @@ class Web {
private _webserver: http.Server | null = null;
private db: DB;
private buildController: BuildController;
private app: Express;
private port: number;
constructor(options: WebConfig = {}) {
const app: Express = express();
const port: number = notStupidParseInt(process.env.PORT) || options['port'] as number || 8080;
const app: Express = this.app = express();
this.port = notStupidParseInt(process.env.PORT) || options['port'] as number || 8080;
app.set('trust proxy', 1);
app.set('view engine', 'ejs');
@@ -84,6 +86,8 @@ class Web {
res.sendStatus(404);
return;
}
const log = (await this.db.getLog(build.id)).map(logChunk => logChunk.chunk.split('\n')).flat();
res.render('build', {
page: {
title: 'Archery',
@@ -91,7 +95,7 @@ class Web {
description: `Building ${build.repo} on ${build.distro}`
},
build,
log: build.log?.split('\n')
log
});
});
@@ -101,14 +105,13 @@ class Web {
res.sendStatus(404);
return;
}
res.set('Content-Type', 'text/plain').send(build.log);
const log = (await this.db.getLog(build.id)).map(logChunk => logChunk.chunk).join('\n');
res.set('Content-Type', 'text/plain').send(log);
});
app.get('/healthcheck', (_, res) => {
res.send('Healthy');
});
this._webserver = app.listen(port, () => console.log(`archery is running on port ${port}`));
}
close = () => {
@@ -119,6 +122,9 @@ class Web {
setDB = (db: DB) => {
this.db = db;
if (!this._webserver) {
this._webserver = this.app.listen(this.port, () => console.log(`archery is running on port ${this.port}`));
}
}
setBuildController = (buildController: BuildController) => {