| 210 | |
| 211 | /** Run `fn` over `items` with at most `concurrency` in flight. */ |
| 212 | export async function runPool<T>( |
| 213 | items: T[], |
| 214 | concurrency: number, |
| 215 | fn: (item: T, index: number) => Promise<void> |
| 216 | ): Promise<void> { |
| 217 | let next = 0; |
| 218 | const workers = Array.from({ length: Math.min(concurrency, items.length) }, async () => { |
| 219 | while (true) { |
| 220 | const i = next++; |
| 221 | if (i >= items.length) return; |
| 222 | await fn(items[i]!, i); |
| 223 | } |
| 224 | }); |
| 225 | await Promise.all(workers); |
| 226 | } |
| 227 | |
| 228 | export async function shutdown(failed: number): Promise<never> { |
| 229 | await redis.quit().catch(() => {}); |