cleanup is only done after all tasks have settled

This commit is contained in:
Andre Staltz 2024-01-18 16:52:00 +02:00
parent 2809d7bb50
commit 198b4be8e9
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
1 changed files with 22 additions and 8 deletions

View File

@ -42,29 +42,45 @@ function initGC(peer, config) {
const startTime = Date.now() const startTime = Date.now()
const done = multicb({ pluck: 1 }) const done = multicb({ pluck: 1 })
function makeRecCB(/**@type {string}*/ errorMessage) {
const cb = done()
return (/**@type {Error=}*/ err) => {
if (err) debug('%s: %s', errorMessage, err.message ?? err)
cb()
}
}
let waiting = false let waiting = false
for (const rec of peer.db.records()) { for (const rec of peer.db.records()) {
if (!rec.msg) continue if (!rec.msg) continue
const { id: msgID, msg } = rec const { id: msgID, msg } = rec
const [purpose, details] = peer.goals.getMsgPurpose(msgID, msg) const [purpose, details] = peer.goals.getMsgPurpose(msgID, msg)
if (purpose === 'goal') continue // don't cleanup
if (purpose === 'none') { if (purpose === 'none') {
peer.db.del(msgID, done()) const recCB = makeRecCB('Failed to delete msg when cleaning up')
peer.db.del(msgID, recCB)
waiting = true waiting = true
} else if (purpose === 'ghost') { } else if (purpose === 'ghost') {
const { tangleID, span } = details const { tangleID, span } = details
const cb = done() const recCB = makeRecCB('Failed to delete ghost msg when cleaning up')
// TODO: Could one msg be a ghostable in MANY tangles? Or just one? // TODO: Could one msg be a ghostable in MANY tangles? Or just one?
peer.db.ghosts.add({ tangleID, msgID, span }, (err) => { peer.db.ghosts.add({ tangleID, msgID, span }, (err) => {
// prettier-ignore if (err) return recCB(err)
if (err) return cb(new Error('gc failed to add ghost', { cause: err })) peer.db.del(msgID, recCB)
peer.db.del(msgID, cb)
}) })
waiting = true waiting = true
} else if (purpose === 'trail') { } else if (purpose === 'trail') {
peer.db.erase(msgID, done()) const recCB = makeRecCB('Failed to erase trail msg when cleaning up')
peer.db.erase(msgID, recCB)
waiting = true waiting = true
} else {
cb(new Error('Unreachable'))
} }
} }
if (waiting) done(whenEnded)
else whenEnded()
/** @param {Error=} err */ /** @param {Error=} err */
function whenEnded(err) { function whenEnded(err) {
const duration = Date.now() - startTime const duration = Date.now() - startTime
@ -72,8 +88,6 @@ function initGC(peer, config) {
else debug('Cleanup completed in %sms', duration) else debug('Cleanup completed in %sms', duration)
cb() cb()
} }
if (waiting) done(whenEnded)
else whenEnded()
} }
/** /**