@urbicon-ui/sveltekit-utils 6.23.0 → 6.25.0
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/dist/cron.d.ts +10 -4
- package/dist/cron.js +14 -2
- package/package.json +1 -1
package/dist/cron.d.ts
CHANGED
|
@@ -34,10 +34,16 @@ export interface CronRunnerConfig {
|
|
|
34
34
|
/** Jobs to schedule; each runs on its own independent interval. */
|
|
35
35
|
jobs: CronJob[];
|
|
36
36
|
/**
|
|
37
|
-
* Called when a job
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
37
|
+
* Called when a job fails, with the failing {@link CronJob} and an `Error`.
|
|
38
|
+
* Fires in two cases:
|
|
39
|
+
* - the job's `fetch` **rejects** (network error, DNS failure, abort) — the
|
|
40
|
+
* `Error` is whatever `fetch` threw.
|
|
41
|
+
* - the endpoint answers with a **non-2xx** status — the runner synthesises
|
|
42
|
+
* an `Error` naming the job and status, with the numeric code attached as
|
|
43
|
+
* `error.status` (e.g. `500`, `403`).
|
|
44
|
+
*
|
|
45
|
+
* Without a handler both failure modes are swallowed silently — pass one to
|
|
46
|
+
* observe per-run outcomes.
|
|
41
47
|
*/
|
|
42
48
|
onError?: (job: CronJob, error: Error) => void;
|
|
43
49
|
}
|
package/dist/cron.js
CHANGED
|
@@ -39,14 +39,26 @@ export function createCronRunner(config) {
|
|
|
39
39
|
running = true;
|
|
40
40
|
for (const job of config.jobs) {
|
|
41
41
|
const timer = setInterval(async () => {
|
|
42
|
+
const base = config.baseUrl ?? 'http://localhost:3000';
|
|
43
|
+
let response;
|
|
42
44
|
try {
|
|
43
|
-
|
|
44
|
-
await fetch(`${base}${job.path}`, {
|
|
45
|
+
response = await fetch(`${base}${job.path}`, {
|
|
45
46
|
method: job.method ?? 'POST',
|
|
46
47
|
headers: { [config.secretHeader ?? 'x-cron-secret']: config.secret }
|
|
47
48
|
});
|
|
48
49
|
}
|
|
49
50
|
catch (err) {
|
|
51
|
+
// Network-level failure (DNS, connection refused, abort): fetch rejected.
|
|
52
|
+
config.onError?.(job, err);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
// The request completed; a non-2xx status is still a failure. Handle it
|
|
56
|
+
// outside the try so a throwing `onError` escapes as an unhandled
|
|
57
|
+
// rejection rather than being re-caught and re-invoked here — symmetric
|
|
58
|
+
// with the rejection path above.
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
const err = new Error(`Cron job "${job.path}" returned ${response.status} ${response.statusText}`);
|
|
61
|
+
err.status = response.status;
|
|
50
62
|
config.onError?.(job, err);
|
|
51
63
|
}
|
|
52
64
|
}, job.intervalSeconds * 1000);
|