mirror of
https://github.com/pockethost/pockethost.git
synced 2025-11-28 08:11:01 +00:00
chore: improve tryFetch documentation and handling
This commit is contained in:
parent
3bb058762a
commit
a76566002d
@ -1,50 +1,64 @@
|
||||
import { LoggerService } from '@pockethost/common'
|
||||
import fetch from 'node-fetch'
|
||||
import { AsyncContext } from './AsyncContext'
|
||||
import fetch, { Response } from 'node-fetch'
|
||||
|
||||
export const TRYFETCH_RETRY_MS = 50
|
||||
|
||||
export type Config = Required<AsyncContext> & {
|
||||
export type Config = {
|
||||
preflight: () => Promise<boolean>
|
||||
retryMs: number
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param url The URL to fetch
|
||||
* @param config { preflight: ()=>Promise<boolean>, retryMs: number}
|
||||
* @returns Promise<Response>
|
||||
*
|
||||
* tryFetch will contiously try to fetch a URL every `retryMs`, with an optinoal `preflight`.
|
||||
* If `preflight` is specificed, it must resolve to `true` before tryFetch
|
||||
* will attempt to fetch the URL. If `preflight` resolves to `false`, tryFetch
|
||||
* will delay by `retryMs` and then check again. If `preflight` rejects,
|
||||
* then tryFetch rejects and exits.
|
||||
*
|
||||
* Note: tryFetch exits ONLY on success or a rejected preflight.
|
||||
*/
|
||||
export const tryFetch = async (url: string, config?: Partial<Config>) => {
|
||||
const { logger, preflight, retryMs }: Config = {
|
||||
logger: LoggerService().create(`tryFetch`),
|
||||
const { preflight, retryMs }: Config = {
|
||||
preflight: async () => true,
|
||||
retryMs: TRYFETCH_RETRY_MS,
|
||||
...config,
|
||||
}
|
||||
const _logger = logger.create(`tryFetch`)
|
||||
const { dbg } = _logger
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const tryFetch = async () => {
|
||||
const logger = LoggerService().create(`tryFetch`).breadcrumb(url)
|
||||
const { dbg } = logger
|
||||
return new Promise<Response>((resolve, reject) => {
|
||||
const again = () => setTimeout(_real_tryFetch, retryMs)
|
||||
const _real_tryFetch = async () => {
|
||||
if (preflight) {
|
||||
dbg(`Checking preflight`)
|
||||
try {
|
||||
dbg(`Preflight: CHECK`)
|
||||
const shouldFetch = await preflight()
|
||||
if (!shouldFetch) {
|
||||
reject(new Error(`failed preflight, aborting`))
|
||||
dbg(`Preflight: NOT READY`)
|
||||
again()
|
||||
return
|
||||
}
|
||||
dbg(`Preflight: READY`)
|
||||
} catch (e) {
|
||||
reject(new Error(`preflight threw error, aborting`))
|
||||
dbg(`Preflight: ABORT`)
|
||||
reject(e)
|
||||
return
|
||||
}
|
||||
}
|
||||
try {
|
||||
dbg(`Trying to fetch ${url} `)
|
||||
dbg(`Fetch: START`)
|
||||
const res = await fetch(url)
|
||||
dbg(`Fetch ${url} successful`)
|
||||
resolve()
|
||||
dbg(`Fetch: SUCCESS`)
|
||||
resolve(res)
|
||||
} catch (e) {
|
||||
dbg(
|
||||
`Could not fetch ${url}, trying again in ${retryMs}ms. Raw error ${e}`,
|
||||
)
|
||||
setTimeout(tryFetch, retryMs)
|
||||
dbg(`Fetch: ERROR: trying again in ${retryMs}ms | ${e}`)
|
||||
again()
|
||||
}
|
||||
}
|
||||
tryFetch()
|
||||
_real_tryFetch()
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user