The merger keeps its place over a restart (worker_cursors), not from 1970 each deploy
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
46525f4595
commit
e627862a8d
3 files changed
+46
-1
No files matched your search
@@ -18,6 +18,8 @@ const SLACK_FILES = 10
|
||||
const ROUND_MS = 60_000
|
||||
/** A round looks back this far past the previous one's start (rows committed while it ran). */
|
||||
const OVERLAP_MS = 60_000
|
||||
/** The merger's row in worker_cursors: the start of its last whole round, kept over a restart. */
|
||||
const CURSOR = 'merger'
|
||||
|
||||
export interface MergeRound {
|
||||
sets: number
|
||||
@@ -73,7 +75,8 @@ interface Member {
|
||||
export class Par2Merger {
|
||||
private stopped = false
|
||||
private folder = new SidecarFolder((trx, found, key, via) => this.merge(trx, found, key, via))
|
||||
private since = new Date(0)
|
||||
/** Null until read from worker_cursors (1970 the first time: everything). */
|
||||
private since: Date | null = null
|
||||
|
||||
stop() {
|
||||
this.stopped = true
|
||||
@@ -97,6 +100,10 @@ export class Par2Merger {
|
||||
/** The sets with something new since the last round: a par2 found, or a hash of one of their files. */
|
||||
async round(): Promise<MergeRound> {
|
||||
const { rows: now } = await db.rawQuery(`SELECT now() AS at`)
|
||||
if (!this.since) {
|
||||
const saved = await db.from('worker_cursors').where('name', CURSOR).first()
|
||||
this.since = saved ? new Date(saved.at) : new Date(0)
|
||||
}
|
||||
const since = new Date(this.since.getTime() - OVERLAP_MS)
|
||||
const { rows } = await db.rawQuery(
|
||||
`SELECT set_id FROM par2_sets WHERE created_at >= ?
|
||||
@@ -136,7 +143,12 @@ export class Par2Merger {
|
||||
}
|
||||
// Third: sidecars (.nfo, .sfv, .nzb) apart from their upload join it.
|
||||
if (!this.stopped) round.folded = await this.folder.round()
|
||||
// Only after a whole round: a restart halfway goes on from the round before.
|
||||
this.since = now[0].at
|
||||
await db.rawQuery(
|
||||
`INSERT INTO worker_cursors (name, at) VALUES (?, ?) ON CONFLICT (name) DO UPDATE SET at = excluded.at`,
|
||||
[CURSOR, this.since]
|
||||
)
|
||||
return round
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user