Update db/cleanup.js

This commit is contained in:
Aleksandr Statciuk 2022-02-13 10:22:50 +03:00
parent 395f40013a
commit 9a95ff55a5
3 changed files with 17 additions and 17 deletions

View file

@ -1,4 +1,5 @@
const { db, logger } = require('../../core')
const _ = require('lodash')
async function main() {
logger.info(`loading streams...`)
@ -7,16 +8,15 @@ async function main() {
logger.info(`removing broken links...`)
let removed = 0
const buffer = {}
for (const stream of streams) {
const duplicate = buffer[stream.channel]
if (duplicate && !stream.is_online) {
const failed = _.filter(streams, { status: 'error' })
for (const stream of failed) {
const hasDuplicate = _.find(streams, s => s.channel === stream.channel && s.status !== 'error')
if (hasDuplicate) {
await db.streams.remove({ _id: stream._id })
removed++
} else {
buffer[stream.channel] = stream
}
}
db.streams.compact()
logger.info(`removed ${removed} links`)