Update cleanup-database.js

This commit is contained in:
Aleksandr Statciuk 2021-12-12 08:43:43 +03:00
parent 712650e65c
commit b016b84160
1 changed files with 14 additions and 4 deletions

View File

@ -1,11 +1,21 @@
const { db, logger } = require('../core')
async function main() {
const removed = await db.remove(
{ 'status.code': { $in: ['timeout', 'offline'] } },
{ multi: true }
)
logger.info(`Loading database...`)
let streams = await db.find({})
logger.info(`Removing broken links...`)
let removed = 0
const buffer = []
for (const stream of streams) {
const duplicate = buffer.find(i => i.id === stream.id)
if (duplicate && ['offline', 'timeout'].includes(stream.status.code)) {
await db.remove({ _id: stream._id })
removed++
} else {
buffer.push(stream)
}
}
db.compact()
logger.info(`Removed ${removed} links`)