Compare commits
1 Commits
53fbcbe22c
..
v1.7.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 194cd3c175 |
+2
-23
@@ -6,8 +6,6 @@ import { fileURLToPath } from 'url';
|
|||||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||||
const DEFAULT_PATH = join(__dirname, '../data/catalyst.db');
|
const DEFAULT_PATH = join(__dirname, '../data/catalyst.db');
|
||||||
|
|
||||||
const JOB_RUN_LIMIT = 10;
|
|
||||||
|
|
||||||
let db;
|
let db;
|
||||||
|
|
||||||
function init(path) {
|
function init(path) {
|
||||||
@@ -19,7 +17,7 @@ function init(path) {
|
|||||||
db.exec('PRAGMA foreign_keys = ON');
|
db.exec('PRAGMA foreign_keys = ON');
|
||||||
db.exec('PRAGMA synchronous = NORMAL');
|
db.exec('PRAGMA synchronous = NORMAL');
|
||||||
createSchema();
|
createSchema();
|
||||||
if (path !== ':memory:') { seed(); seedJobs(); pruneAllJobRuns(); }
|
if (path !== ':memory:') { seed(); seedJobs(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSchema() {
|
function createSchema() {
|
||||||
@@ -269,7 +267,6 @@ export function importJobs(jobRows, jobRunRows = []) {
|
|||||||
`);
|
`);
|
||||||
for (const r of jobRunRows) insertRun.run(r);
|
for (const r of jobRunRows) insertRun.run(r);
|
||||||
}
|
}
|
||||||
pruneAllJobRuns();
|
|
||||||
db.exec('COMMIT');
|
db.exec('COMMIT');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,28 +326,10 @@ export function completeJobRun(runId, status, result) {
|
|||||||
db.prepare(`
|
db.prepare(`
|
||||||
UPDATE job_runs SET ended_at=strftime('%Y-%m-%dT%H:%M:%f', 'now'), status=@status, result=@result WHERE id=@id
|
UPDATE job_runs SET ended_at=strftime('%Y-%m-%dT%H:%M:%f', 'now'), status=@status, result=@result WHERE id=@id
|
||||||
`).run({ id: runId, status, result });
|
`).run({ id: runId, status, result });
|
||||||
const row = db.prepare('SELECT job_id FROM job_runs WHERE id = ?').get(runId);
|
|
||||||
if (row) pruneJobRuns(row.job_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getJobRuns(jobId) {
|
export function getJobRuns(jobId) {
|
||||||
return db.prepare(`SELECT * FROM job_runs WHERE job_id = ? ORDER BY id DESC LIMIT ${JOB_RUN_LIMIT}`).all(jobId);
|
return db.prepare('SELECT * FROM job_runs WHERE job_id = ? ORDER BY id DESC').all(jobId);
|
||||||
}
|
|
||||||
|
|
||||||
function pruneJobRuns(jobId) {
|
|
||||||
db.prepare(`
|
|
||||||
DELETE FROM job_runs
|
|
||||||
WHERE job_id = ?
|
|
||||||
AND id NOT IN (
|
|
||||||
SELECT id FROM job_runs WHERE job_id = ? ORDER BY id DESC LIMIT ?
|
|
||||||
)
|
|
||||||
`).run(jobId, jobId, JOB_RUN_LIMIT);
|
|
||||||
}
|
|
||||||
|
|
||||||
function pruneAllJobRuns() {
|
|
||||||
for (const j of db.prepare('SELECT id FROM jobs').all()) {
|
|
||||||
pruneJobRuns(j.id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Test helpers ──────────────────────────────────────────────────────────────
|
// ── Test helpers ──────────────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -450,17 +450,4 @@ describe('job_runs', () => {
|
|||||||
expect(runs[0].id).toBe(r2);
|
expect(runs[0].id).toBe(r2);
|
||||||
expect(runs[1].id).toBe(r1);
|
expect(runs[1].id).toBe(r1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('caps history at the last 10 runs per job', () => {
|
|
||||||
createJob(baseJob);
|
|
||||||
const id = getJobs()[0].id;
|
|
||||||
for (let i = 0; i < 15; i++) {
|
|
||||||
const runId = createJobRun(id);
|
|
||||||
completeJobRun(runId, 'success', `run ${i}`);
|
|
||||||
}
|
|
||||||
const runs = getJobRuns(id);
|
|
||||||
expect(runs).toHaveLength(10);
|
|
||||||
expect(runs[0].result).toBe('run 14');
|
|
||||||
expect(runs[9].result).toBe('run 5');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user