feat(server): temp segments cleanup task
Hourly cron that deletes expired temp_segments from S3 and DB. Implements issue #34. Registered alongside scheduler in index.js.
This commit is contained in:
parent
a016175fc8
commit
77130ac769
2 changed files with 45 additions and 0 deletions
|
|
@ -32,6 +32,7 @@ import metricsRouter from './routes/metrics.js';
|
||||||
import commentsRouter from './routes/comments.js';
|
import commentsRouter from './routes/comments.js';
|
||||||
import importsRouter from './routes/imports.js';
|
import importsRouter from './routes/imports.js';
|
||||||
import { startSchedulerLoop } from './scheduler.js';
|
import { startSchedulerLoop } from './scheduler.js';
|
||||||
|
import { startCleanupLoop } from './tasks/cleanupTempSegments.js';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
|
|
@ -196,4 +197,7 @@ app.listen(PORT, () => {
|
||||||
// Boot the recorder scheduler tick loop after the HTTP server is live so
|
// Boot the recorder scheduler tick loop after the HTTP server is live so
|
||||||
// the loop's self-calls to /recorders/:id/start|stop reach a ready socket.
|
// the loop's self-calls to /recorders/:id/start|stop reach a ready socket.
|
||||||
startSchedulerLoop();
|
startSchedulerLoop();
|
||||||
|
|
||||||
|
// Boot the temp-segment cleanup loop (runs hourly).
|
||||||
|
startCleanupLoop();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
41
services/mam-api/src/tasks/cleanupTempSegments.js
Normal file
41
services/mam-api/src/tasks/cleanupTempSegments.js
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
import pool from '../db/pool.js';
|
||||||
|
import { deleteObject } from '../s3/client.js';
|
||||||
|
|
||||||
|
const CLEANUP_INTERVAL_MS = 3600_000;
|
||||||
|
|
||||||
|
let _interval = null;
|
||||||
|
|
||||||
|
async function cleanup() {
|
||||||
|
const expired = await pool.query(
|
||||||
|
'SELECT * FROM temp_segments WHERE expires_at < NOW()'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (expired.rows.length === 0) return;
|
||||||
|
|
||||||
|
let deleted = 0;
|
||||||
|
let errors = 0;
|
||||||
|
|
||||||
|
for (const segment of expired.rows) {
|
||||||
|
try {
|
||||||
|
await deleteObject(segment.s3_key);
|
||||||
|
await pool.query('DELETE FROM temp_segments WHERE id = $1', [segment.id]);
|
||||||
|
deleted++;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[cleanup] failed to delete segment ' + segment.id + ': ' + err.message);
|
||||||
|
errors++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[cleanup] temp segments: ${deleted} deleted, ${errors} errors`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function startCleanupLoop() {
|
||||||
|
if (_interval) return;
|
||||||
|
console.log(`[cleanup] temp segments loop started (interval=${CLEANUP_INTERVAL_MS}ms)`);
|
||||||
|
cleanup().catch(() => {});
|
||||||
|
_interval = setInterval(() => cleanup().catch(() => {}), CLEANUP_INTERVAL_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stopCleanupLoop() {
|
||||||
|
if (_interval) { clearInterval(_interval); _interval = null; }
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue