curfew 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Run batches from an (async) iterable and stop **before** a time budget runs out.
|
|
4
4
|
|
|
5
|
-
Built for
|
|
5
|
+
Built for AWS Lambda (15 min hard limit) and similar time-limited runtimes: instead of being killed mid-batch, `curfew` won't start a batch that probably can't finish, and tells you to reschedule the rest.
|
|
6
6
|
|
|
7
7
|
- No runtime dependencies
|
|
8
8
|
- Pluggable time budget and duration estimator
|
|
@@ -22,16 +22,26 @@ Requires Node.js 22+. Ships ESM and CommonJS.
|
|
|
22
22
|
import { LambdaBudget, runBatches } from 'curfew'
|
|
23
23
|
|
|
24
24
|
export const handler = async (event, context) => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
let offset = event.offset
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const { batchesProcessed, mayHaveMore } = await runBatches(
|
|
29
|
+
fetchPages(offset),
|
|
30
|
+
async (page, { signal }) => {
|
|
31
|
+
await processPage(page, { signal })
|
|
32
|
+
offset++
|
|
33
|
+
},
|
|
34
|
+
{ budget: new LambdaBudget(context, 60_000) },
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
if (mayHaveMore) {
|
|
38
|
+
await reenqueue({ offset: event.offset + batchesProcessed })
|
|
39
|
+
}
|
|
40
|
+
} catch (error) {
|
|
41
|
+
// The signal aborted mid-page: an expected timeout, not a failure.
|
|
42
|
+
// `curfew` passes handler errors through, so catch it here and resume from the interrupted page.
|
|
43
|
+
if (!(error instanceof Error && ['AbortError', 'TimeoutError'].includes(error.name))) throw error
|
|
44
|
+
await reenqueue({ offset })
|
|
35
45
|
}
|
|
36
46
|
}
|
|
37
47
|
```
|
|
@@ -40,7 +50,7 @@ export const handler = async (event, context) => {
|
|
|
40
50
|
|
|
41
51
|
[`examples/basic.ts`](examples/basic.ts) processes 10 pages with a 1-second budget per run. It shows the three ways a run ends, and how to resume after each:
|
|
42
52
|
|
|
43
|
-
- **Budget runs out mid-page.** `saveItem` passes `ctx.signal` to its async work, so it rejects with an `AbortError`. The
|
|
53
|
+
- **Budget runs out mid-page.** `saveItem` passes `ctx.signal` to its async work, so it rejects with an `AbortError`. The example's `runOnce` catches that error (`curfew` passes handler errors through) and resumes from the interrupted page, which is why page processing must be idempotent.
|
|
44
54
|
- **The next page won't fit.** `reason` is `estimate-exceeds-budget`, and the next run starts at the following page.
|
|
45
55
|
- **All pages done.** `reason` is `done`.
|
|
46
56
|
|