sepia-search-motore-di-rice.../server/lib/schedulers/videos-indexer.ts

100 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-02-13 16:06:52 +01:00
import { AbstractScheduler } from './abstract-scheduler'
2020-02-14 14:09:31 +01:00
import { CONFIG, INDEXER_COUNT, SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
2020-02-13 16:06:52 +01:00
import { doRequest } from '../../helpers/requests'
import { logger } from '../../helpers/logger'
import { ResultList } from '../../../PeerTube/shared/models/result-list.model'
import { Video } from '../../../PeerTube/shared/models/videos/video.model'
2020-02-14 14:09:31 +01:00
import { indexVideos, listIndexInstances, refreshVideosIndex } from '../elastic-search-videos'
2020-02-13 16:06:52 +01:00
import { IndexableVideo } from '../../types/video.model'
import { inspect } from 'util'
2020-02-14 14:09:31 +01:00
import { getRemovedHosts, listIndexInstancesHost } from '../instances-index'
import { elasticSearch } from '../../helpers/elastic-search'
2020-02-13 16:06:52 +01:00
export class VideosIndexer extends AbstractScheduler {
private static instance: AbstractScheduler
protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.videosIndexer
private constructor () {
super()
}
protected async internalExecute () {
return this.indexVideos()
}
private async indexVideos () {
2020-02-14 14:09:31 +01:00
const dbHosts = await listIndexInstances()
const indexHosts = (await listIndexInstancesHost()).filter(h => h === 'peertube.cpy.re')
2020-02-13 16:06:52 +01:00
2020-02-14 14:09:31 +01:00
const hostsToRemove = getRemovedHosts(dbHosts, indexHosts)
await this.removeVideosFromHosts(hostsToRemove)
for (const instance of indexHosts) {
2020-02-13 16:06:52 +01:00
try {
2020-02-14 14:09:31 +01:00
let videos: IndexableVideo[] = []
let start = 0
do {
videos = await this.getVideos(instance, start)
start += videos.length
logger.debug('Getting %d results from %s (from = %d).', videos.length, instance, start)
2020-02-13 16:06:52 +01:00
2020-02-14 14:09:31 +01:00
if (videos.length !== 0) {
await indexVideos(videos)
}
} while (videos.length === INDEXER_COUNT.VIDEOS && start < 500)
2020-02-13 16:06:52 +01:00
logger.info('Added video data from %s.', instance)
} catch (err) {
console.error(inspect(err, { depth: 10 }))
logger.warn('Cannot index videos from %s.', instance, { err })
}
}
await refreshVideosIndex()
}
2020-02-14 14:09:31 +01:00
private async getVideos (host: string, start: number): Promise<IndexableVideo[]> {
2020-02-13 16:06:52 +01:00
const url = 'https://' + host + '/api/v1/videos'
const res = await doRequest<ResultList<Video>>({
uri: url,
qs: {
2020-02-14 14:09:31 +01:00
start,
2020-02-13 16:06:52 +01:00
filter: 'local',
2020-02-14 14:09:31 +01:00
skipCount: true,
2020-02-13 16:06:52 +01:00
count: INDEXER_COUNT.VIDEOS
},
json: true
})
return res.body.data.map(v => Object.assign(v, { elasticSearchId: host + v.id, host }))
}
2020-02-14 14:09:31 +01:00
private removeVideosFromHosts (hosts: string[]) {
if (hosts.length === 0) return
logger.info('Will remove videos from hosts.', { hosts })
const should = hosts.map(host => ({ term: { host } }))
return elasticSearch.delete_by_query({
index: CONFIG.ELASTIC_SEARCH.INDEXES.VIDEOS,
body: {
query: {
bool: {
should
}
}
}
})
}
2020-02-13 16:06:52 +01:00
static get Instance () {
return this.instance || (this.instance = new this())
}
}