(id: string, step: string, run: () => Promise<A>, signal?: AbortSignal)
| 34 | } |
| 35 | |
| 36 | const lifecycle = async <A>(id: string, step: string, run: () => Promise<A>, signal?: AbortSignal): Promise<A> => { |
| 37 | if (!lifecycleEnabled) return run() |
| 38 | const started = Date.now() |
| 39 | const log = (state: string) => lifecycleLog(id, step, state, Date.now() - started) |
| 40 | log("start") |
| 41 | const pending = setInterval(() => log("pending"), 5_000) |
| 42 | pending.unref() |
| 43 | const interrupted = () => { |
| 44 | clearInterval(pending) |
| 45 | log("interrupted") |
| 46 | } |
| 47 | signal?.addEventListener("abort", interrupted, { once: true }) |
| 48 | try { |
| 49 | const value = await run() |
| 50 | log("done") |
| 51 | return value |
| 52 | } catch (error) { |
| 53 | lifecycleLog(id, step, "failed", Date.now() - started, error) |
| 54 | throw error |
| 55 | } finally { |
| 56 | clearInterval(pending) |
| 57 | signal?.removeEventListener("abort", interrupted) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | const marker = (id: string, step: string) => Effect.sync(() => lifecycleLog(id, step, "mark", 0)) |
| 62 |
no test coverage detected