@hardlydifficult/daemon 1.0.8 → 1.0.10
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 +57 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -88,7 +88,38 @@ untrap();
|
|
|
88
88
|
### Behavior Notes
|
|
89
89
|
|
|
90
90
|
- Errors in teardown functions are caught and logged, allowing remaining functions to complete.
|
|
91
|
-
- Signal handlers are added only once per process and cleaned up automatically when untrap() is called.
|
|
91
|
+
- Signal handlers are added only once per process and cleaned up automatically when `untrap()` is called.
|
|
92
|
+
|
|
93
|
+
### Example: Basic Teardown
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { createTeardown } from "@hardlydifficult/daemon";
|
|
97
|
+
|
|
98
|
+
const teardown = createTeardown();
|
|
99
|
+
|
|
100
|
+
teardown.add(() => console.log("Closing database"));
|
|
101
|
+
teardown.add(() => console.log("Stopping server"));
|
|
102
|
+
teardown.trapSignals();
|
|
103
|
+
|
|
104
|
+
await teardown.run();
|
|
105
|
+
// Output:
|
|
106
|
+
// Stopping server
|
|
107
|
+
// Closing database
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Unregistering Teardown Functions
|
|
111
|
+
|
|
112
|
+
Teardown functions can be unregistered before `run()` is called.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const teardown = createTeardown();
|
|
116
|
+
|
|
117
|
+
const unregister = teardown.add(() => cleanupA());
|
|
118
|
+
teardown.add(() => cleanupB());
|
|
119
|
+
|
|
120
|
+
unregister(); // Removes cleanupA from the teardown list
|
|
121
|
+
await teardown.run(); // Only cleanupB runs
|
|
122
|
+
```
|
|
92
123
|
|
|
93
124
|
## Continuous Loop with `runContinuousLoop`
|
|
94
125
|
|
|
@@ -99,11 +130,11 @@ Runs a recurring task in a loop with built-in signal handling, dynamic delays, a
|
|
|
99
130
|
| Option | Type | Description |
|
|
100
131
|
|--------|------|-------------|
|
|
101
132
|
| `intervalSeconds` | `number` | Default delay between cycles in seconds |
|
|
102
|
-
| `runCycle` | `() => Promise<RunCycleResult>` | Main function executed per cycle |
|
|
103
|
-
| `getNextDelayMs
|
|
104
|
-
| `onCycleError
|
|
105
|
-
| `onShutdown
|
|
106
|
-
| `logger
|
|
133
|
+
| `runCycle` | `(isShutdownRequested: () => boolean) => Promise<RunCycleResult>` | Main function executed per cycle |
|
|
134
|
+
| `getNextDelayMs?` | `(result, context) => ContinuousLoopDelay \| undefined` | Optional custom delay resolver |
|
|
135
|
+
| `onCycleError?` | `ContinuousLoopErrorHandler` | Custom error handling strategy |
|
|
136
|
+
| `onShutdown?` | `() => void \| Promise<void>` | Cleanup hook called on shutdown |
|
|
137
|
+
| `logger?` | `ContinuousLoopLogger` | Optional logger (defaults to console) |
|
|
107
138
|
|
|
108
139
|
### Return Values from `runCycle`
|
|
109
140
|
|
|
@@ -158,6 +189,26 @@ await runContinuousLoop({
|
|
|
158
189
|
});
|
|
159
190
|
```
|
|
160
191
|
|
|
192
|
+
### Dynamic Delays
|
|
193
|
+
|
|
194
|
+
Use `getNextDelayMs` to derive delays from domain results:
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
type CycleResult = { delaySeconds: number } | { stop: true };
|
|
198
|
+
|
|
199
|
+
await runContinuousLoop({
|
|
200
|
+
intervalSeconds: 60, // default fallback
|
|
201
|
+
async runCycle() {
|
|
202
|
+
const result = await fetchStatus();
|
|
203
|
+
if (result.stopped) return { stop: true };
|
|
204
|
+
return { delaySeconds: result.delay };
|
|
205
|
+
},
|
|
206
|
+
getNextDelayMs(result) {
|
|
207
|
+
return "delaySeconds" in result ? result.delaySeconds * 1000 : undefined;
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
161
212
|
### Shutdown Signals
|
|
162
213
|
|
|
163
214
|
The loop responds to `SIGINT` and `SIGTERM` by stopping after the current cycle completes. Use `isShutdownRequested()` inside `runCycle` to abort long-running work:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hardlydifficult/daemon",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"vitest": "4.0.18"
|
|
21
21
|
},
|
|
22
22
|
"engines": {
|
|
23
|
-
"node": ">=
|
|
23
|
+
"node": ">=20.19.0"
|
|
24
24
|
},
|
|
25
25
|
"type": "commonjs",
|
|
26
26
|
"exports": {
|