fedicrawl/application/src/Storage/Nodes/fetchNodeToProcess.ts

67 wiersze
1.8 KiB
TypeScript
Czysty Zwykły widok Historia

2021-12-23 14:14:06 +00:00
import { Node, PrismaClient } from '@prisma/client'
import { NoNodeFoundError } from './NoNodeFoundError'
2021-12-23 14:14:06 +00:00
export const fetchNodeToProcess = async (prisma: PrismaClient): Promise<Node> => {
2022-01-03 12:26:17 +00:00
const currentTimestamp = Date.now()
const attemptLimitMilliseconds = parseInt(process.env.REATTEMPT_MINUTES ?? '60') * 60 * 1000
const attemptLimitDate = new Date(currentTimestamp - attemptLimitMilliseconds)
console.log('Searching for not yet processed node not attempted before attemptLimit', { attemptLimitDate, attemptLimitMilliseconds })
2021-12-23 14:14:06 +00:00
const newNode = await prisma.node.findFirst({
orderBy: {
foundAt: 'asc'
},
where: {
2022-01-03 12:26:17 +00:00
refreshedAt: null,
OR: [
{
refreshAttemptedAt: {
lt: attemptLimitDate
}
},
{
refreshAttemptedAt: null
}
]
2021-12-23 14:14:06 +00:00
}
})
if (newNode) {
console.log('Found not yet processed node', { domain: newNode.domain })
return newNode
}
2022-01-03 12:26:17 +00:00
const refreshLimitMilliseconds = parseInt(process.env.REFRESH_HOURS ?? '168') * 60 * 60 * 1000
const refreshLimitDate = new Date(currentTimestamp - refreshLimitMilliseconds)
console.log('Searching instance not refreshed for longest time and before refreshLimit and attemptLimit', {
refreshLimitMilliseconds,
refreshLimitDate,
attemptLimitDate,
attemptLimitMilliseconds
})
2021-12-23 14:14:06 +00:00
const node = await prisma.node.findFirst({
orderBy: {
refreshedAt: 'asc'
},
where: {
refreshedAt: {
2022-01-03 12:26:17 +00:00
lt: refreshLimitDate
},
OR: [
{
refreshAttemptedAt: {
lt: attemptLimitDate
}
},
{
refreshAttemptedAt: null
}
]
2021-12-23 14:14:06 +00:00
}
})
if (!node) {
throw new NoNodeFoundError()
2021-12-23 14:14:06 +00:00
}
console.log('Found oldest node', { domain: node.domain })
2021-12-23 14:14:06 +00:00
return node
}