Fix removing non existing hosts

This commit is contained in:
Chocobozzz 2024-01-09 11:06:15 +01:00
parent bf63503e23
commit aaad2ea75d
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 8 additions and 43 deletions

View File

@ -74,9 +74,9 @@ export abstract class AbstractIndexer <T extends IndexableDoc, DB> {
})
}
removeFromHosts (hosts: string[]) {
removeFromHosts (existingHosts: string[]) {
return client.index(this.indexName).deleteDocuments({
filter: 'host IN ' + buildInValuesArray(Array.from(hosts))
filter: 'host NOT IN ' + buildInValuesArray(Array.from(existingHosts))
})
}

View File

@ -1,10 +1,9 @@
import { CONFIG } from '../../initializers/constants'
import { listIndexInstancesHost, getMajorInstanceVersion } from '../requests/instances-index'
import { client } from '../../helpers/meilisearch'
import { logger } from '../../helpers/logger'
import Bluebird from 'bluebird'
async function buildInstanceHosts () {
export async function buildInstanceHosts () {
let indexHosts = await listIndexInstancesHost()
if (CONFIG.INSTANCES_INDEX.WHITELIST.ENABLED) {
@ -27,36 +26,5 @@ async function buildInstanceHosts () {
return true
}, { concurrency: 10 })
const dbHosts = await listDBInstances()
const removedHosts = getRemovedHosts(dbHosts, indexHosts)
return { indexHosts, removedHosts }
}
export {
buildInstanceHosts
}
// ##################################################
async function listDBInstances () {
const setResult = new Set<string>()
const indexes = [
CONFIG.MEILISEARCH.INDEXES.VIDEOS,
CONFIG.MEILISEARCH.INDEXES.CHANNELS
]
for (const index of indexes) {
const result = await client.index(index).searchForFacetValues({ facetName: 'host' })
for (const b of result.facetHits) {
setResult.add(b.value)
}
}
return Array.from(setResult)
}
function getRemovedHosts (dbHosts: string[], indexHosts: string[]) {
return dbHosts.filter(dbHost => indexHosts.includes(dbHost) === false)
return indexHosts
}

View File

@ -56,18 +56,15 @@ export class IndexationScheduler extends AbstractScheduler {
private async runIndexer () {
logger.info('Running indexer.')
const { indexHosts, removedHosts } = await buildInstanceHosts()
this.indexedHosts = indexHosts
this.indexedHosts = await buildInstanceHosts()
logger.info({ removedHosts }, `Will remove ${removedHosts.length} hosts that do not exist anymore`)
logger.info({ indexHosts: this.indexedHosts }, `Will index ${this.indexedHosts.length} hosts and remove non existing hosts`)
for (const o of this.indexers) {
await o.removeFromHosts(removedHosts)
await o.removeFromHosts(this.indexedHosts)
}
logger.info({ indexHosts }, `Will index ${indexHosts.length} hosts`)
await Bluebird.map(indexHosts, async host => {
await Bluebird.map(this.indexedHosts, async host => {
try {
await this.indexHost(host)
} catch (err) {