fedicrawl/application/src/Storage/Nodes/findNodeWithOldestRefreshWi...

59 wiersze
1.7 KiB
TypeScript
Czysty Zwykły widok Historia

2022-09-14 19:16:04 +00:00
import { ElasticClient } from '../ElasticClient'
import nodeIndex from '../Definitions/nodeIndex'
import Node from '../Definitions/Node'
2022-09-18 11:32:25 +00:00
const findNodeWithOldestRefreshWithLimits = async (
elastic: ElasticClient
): Promise<Node | null> => {
2022-09-14 19:16:04 +00:00
const currentTimestamp = Date.now()
2022-09-18 11:32:25 +00:00
const attemptLimitMilliseconds =
parseInt(process.env.REATTEMPT_MINUTES ?? '60') * 60 * 1000
2022-09-14 19:16:04 +00:00
const attemptLimitDate = new Date(currentTimestamp - attemptLimitMilliseconds)
2022-09-18 11:32:25 +00:00
const refreshLimitMilliseconds =
parseInt(process.env.REFRESH_HOURS ?? '168') * 60 * 60 * 1000
2022-09-14 19:16:04 +00:00
const refreshLimitDate = new Date(currentTimestamp - refreshLimitMilliseconds)
2022-09-18 11:32:25 +00:00
console.log(
'Searching instance not refreshed for longest time and before refreshLimit and attemptLimit',
{
refreshLimitMilliseconds,
refreshLimitDate,
attemptLimitDate,
attemptLimitMilliseconds
}
)
2022-09-14 19:16:04 +00:00
const result = await elastic.search<Node>({
index: nodeIndex,
body: {
size: 1,
2022-09-18 11:32:25 +00:00
sort: [
{
refreshedAt: { order: 'asc' }
}
],
2022-09-14 19:16:04 +00:00
query: {
bool: {
must: [
{ range: { refreshedAt: { lt: refreshLimitDate.getTime() } } }
],
should: [
2022-09-18 11:32:25 +00:00
{
range: { refreshAttemptedAt: { lt: attemptLimitDate.getTime() } }
},
{
bool: { must_not: [{ exists: { field: 'refreshAttemptedAt' } }] }
}
2022-09-14 19:16:04 +00:00
],
minimum_should_match: 1
}
}
}
})
2022-09-18 11:32:25 +00:00
const node = result.hits.hits.pop()?._source
if (node !== undefined) {
console.info('Found oldest node', { node })
2022-09-14 19:16:04 +00:00
return node
}
return null
}
export default findNodeWithOldestRefreshWithLimits