Skip to content

Commit f1e5bab

Browse files
committed
Sync logs and db records
1 parent 008e7a7 commit f1e5bab

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/cron/janitor.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,32 @@ import * as fs from 'fs';
33
import { DB_FILE_STATUS, JANITOR_COPIES } from '../constants';
44
import { DatabaseService } from '../services/database.service';
55

6+
export async function syncLogsDb(db: DatabaseService) {
7+
console.log('janitor: sync logs with DB records');
8+
const result = await db.query<{
9+
id: number;
10+
basename: string;
11+
path: string;
12+
}>(
13+
`
14+
SELECT id, basename, path
15+
FROM logs
16+
WHERE status = ?
17+
ORDER BY id DESC
18+
`,
19+
[DB_FILE_STATUS.CopiedToObjectStore],
20+
);
21+
for (const row of result.rows) {
22+
try {
23+
const filePath = row.path;
24+
fs.openSync(`${filePath}`, 'r');
25+
} catch (err) {
26+
console.log(`Delete db row id with missing log: ${row.id}`);
27+
db.deleteLog(row.id);
28+
}
29+
}
30+
}
31+
632
export async function removeOldLogs(db: DatabaseService) {
733
console.log('janitor: start');
834
const nameHash: { [key: string]: number } = {};

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { backup } from './cron/backup';
1212
import { DatabaseService } from './services/database.service';
1313
import { rotateLogs } from './cron/rotate';
14-
import { removeOldLogs } from './cron/janitor';
14+
import { removeOldLogs, syncLogsDb } from './cron/janitor';
1515
import { compress } from './cron/compress';
1616

1717
console.log('Starting...');
@@ -41,6 +41,7 @@ async function main() {
4141
await backup(db);
4242
});
4343
const janitorJob = Cron(CRON_JANITOR, async () => {
44+
await syncLogsDb(db);
4445
await removeOldLogs(db);
4546
});
4647
console.log(`Rotate job next run: ${rotateJob.nextRun()}`);

0 commit comments

Comments
 (0)