multithread exec function

This commit is contained in:
Lumeille 2024-03-09 22:28:16 +11:00
parent 1cbc553e0b
commit c814c6e7d6
1 changed files with 8 additions and 6 deletions

View File

@ -13,10 +13,12 @@ export type State = typeof state
const executor = async () => { const executor = async () => {
const curTime = Date.now() const curTime = Date.now()
for (const endpoint of config.pollEndpoints) { const promises = config.pollEndpoints.map(async endpoint => {
const endpointState = state.get(endpoint.name) const endpointState = state.get(endpoint.name)
if (endpointState === undefined) {console.log(`Could not find endpoint for ${endpoint.name}`)} if (endpointState === undefined) console.log(`Could not find endpoint for ${endpoint.name}`)
else if (curTime - endpointState.lastExec > ((endpoint.interval ?? defaultInterval) * 1000)) { else if (curTime - endpointState.lastExec > ((endpoint.interval ?? defaultInterval) * 1000)) {
console.log(`Time to poll ${endpoint.name}`)
endpointState.lastExec = curTime
if (endpoint.type === "fetch" ) { if (endpoint.type === "fetch" ) {
const r = await fetch(endpoint.endpoint, {method: "GET"}) const r = await fetch(endpoint.endpoint, {method: "GET"})
if (r.ok) handleUp(endpointState, curTime, endpoint) if (r.ok) handleUp(endpointState, curTime, endpoint)
@ -24,13 +26,13 @@ const executor = async () => {
} }
else if (endpoint.type === "ping" ) { else if (endpoint.type === "ping" ) {
const r = await ping({address: endpoint.endpoint}) const r = await ping({address: endpoint.endpoint})
console.log(r.errors)
if (r.errors.length === 0) handleUp(endpointState, curTime, endpoint) if (r.errors.length === 0) handleUp(endpointState, curTime, endpoint)
else handleDown(endpointState, curTime, endpoint) else handleDown(endpointState, curTime, endpoint)
} }
console.log(`Time to poll ${endpoint.name}`) }
endpointState.lastExec = curTime })
} await Promise.all(promises)
}
} }
const main = async () => { const main = async () => {