@prunellejs/jobs 0.7.2 → 0.7.3

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.
Files changed (3) hide show
  1. package/llms.txt +1 -1
  2. package/package.json +2 -2
  3. package/src/cron.ts +120 -9
package/llms.txt CHANGED
@@ -40,7 +40,7 @@
40
40
 
41
41
  - `jobs.schedule(name, cronExpr, handler, { timeZone })`:
42
42
  - Standard 5-field cron (`minute hour dom month dow`: `*`, `*/n`, `a-b`, `a-b/n`, lists `a,b,c`, month/weekday names `JAN-DEC`/`SUN-SAT`, macros `@hourly`/`@daily`/`@weekly`/`@monthly`/`@yearly`).
43
- - Evaluated in `timeZone` (default `'UTC'`) via `Intl.DateTimeFormat`; both `cronExpr` and `timeZone` are validated eagerly.
43
+ - Evaluated in `timeZone` (default `'UTC'`) via `Intl.DateTimeFormat`; both `cronExpr` and `timeZone` are validated eagerly. Fixed-hour schedules skipped by a spring-forward DST gap run at the next valid instant and repeated autumn hours run once; wildcard-hour schedules (`0 * * * *`) keep running once per real hour.
44
44
  - Each `(schedule_name, tick_ms)` is claimed atomically in SQLite (`WAL` + `BEGIN IMMEDIATE` + primary key), guaranteeing two processes sharing the same SQLite file never run the same tick twice.
45
45
 
46
46
  ## Server Lifecycle, Health & Mail Integration
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/jobs",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "Zero-dependency durable SQLite job queue and cron scheduler plugin for Prunelle",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -21,6 +21,6 @@
21
21
  "directory": "packages/plugin-jobs"
22
22
  },
23
23
  "peerDependencies": {
24
- "prunelle": "^0.7.2"
24
+ "prunelle": "^0.7.3"
25
25
  }
26
26
  }
package/src/cron.ts CHANGED
@@ -276,37 +276,148 @@ function matchesDay(schedule: ParsedCron, day: number, dow: number): boolean {
276
276
  return domOk && dowOk
277
277
  }
278
278
 
279
- export function matchesCron(schedule: ParsedCron, tsMs: number, timeZone = 'UTC'): boolean {
280
- const parts = getZonedParts(tsMs, timeZone)
279
+ const MINUTE_MS = 60_000
280
+ const MAX_DST_SHIFT_MS = 3 * 60 * MINUTE_MS
281
+ const MAX_SEARCH_SPAN_MS = 5 * 366 * 24 * 60 * MINUTE_MS
282
+
283
+ function isUtcZone(timeZone: string): boolean {
284
+ return timeZone === 'UTC' || timeZone === 'Etc/UTC'
285
+ }
286
+
287
+ function wallMsFromParts(parts: ZonedDateParts): number {
288
+ return Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute)
289
+ }
290
+
291
+ function offsetAt(minuteMs: number, timeZone: string): number {
292
+ return wallMsFromParts(getZonedParts(minuteMs, timeZone)) - minuteMs
293
+ }
294
+
295
+ function matchesParts(schedule: ParsedCron, parts: ZonedDateParts): boolean {
281
296
  return schedule.minutes.has(parts.minute)
282
297
  && schedule.hours.has(parts.hour)
283
298
  && schedule.months.has(parts.month)
284
299
  && matchesDay(schedule, parts.day, parts.dow)
285
300
  }
286
301
 
287
- const MINUTE_MS = 60_000
288
- const MAX_SEARCH_SPAN_MS = 5 * 366 * 24 * 60 * MINUTE_MS
302
+ /** True when `minuteMs` is the first valid instant after a spring-forward jump whose skipped wall-clock minutes match `schedule`. */
303
+ function matchesSpringGap(schedule: ParsedCron, minuteMs: number, parts: ZonedDateParts, timeZone: string): boolean {
304
+ const currWall = wallMsFromParts(parts)
305
+ const prevWall = wallMsFromParts(getZonedParts(minuteMs - MINUTE_MS, timeZone))
306
+ const gap = currWall - prevWall
307
+ if (gap <= MINUTE_MS || gap > MAX_DST_SHIFT_MS + MINUTE_MS)
308
+ return false
309
+ for (let w = prevWall + MINUTE_MS; w < currWall; w += MINUTE_MS) {
310
+ const d = new Date(w)
311
+ if (
312
+ schedule.minutes.has(d.getUTCMinutes())
313
+ && schedule.hours.has(d.getUTCHours())
314
+ && schedule.months.has(d.getUTCMonth() + 1)
315
+ && matchesDay(schedule, d.getUTCDate(), d.getUTCDay())
316
+ ) {
317
+ return true
318
+ }
319
+ }
320
+ return false
321
+ }
322
+
323
+ /** True when `minuteMs` is the second (fall-back) occurrence of an ambiguous local wall-clock minute. */
324
+ function isRepeatedWallMinute(minuteMs: number, parts: ZonedDateParts, timeZone: string): boolean {
325
+ const currWall = wallMsFromParts(parts)
326
+ const currOffset = currWall - minuteMs
327
+ const probeOffset = offsetAt(minuteMs - MAX_DST_SHIFT_MS, timeZone)
328
+ if (probeOffset <= currOffset)
329
+ return false
330
+ const shift = probeOffset - currOffset
331
+ return wallMsFromParts(getZonedParts(minuteMs - shift, timeZone)) === currWall
332
+ }
333
+
334
+ /** Binary-searches `(loMs, hiMs]` for the first UTC minute whose timezone offset is strictly greater than `loOffset`. */
335
+ function findSpringTransition(loMs: number, hiMs: number, loOffset: number, timeZone: string): number {
336
+ let lo = loMs
337
+ let hi = hiMs
338
+ while (hi - lo > MINUTE_MS) {
339
+ const mid = floorToMinute(lo + Math.floor((hi - lo) / 2))
340
+ if (offsetAt(mid, timeZone) > loOffset)
341
+ hi = mid
342
+ else
343
+ lo = mid
344
+ }
345
+ return hi
346
+ }
347
+
348
+ export function matchesCron(schedule: ParsedCron, tsMs: number, timeZone = 'UTC'): boolean {
349
+ const minuteMs = floorToMinute(tsMs)
350
+ const parts = getZonedParts(minuteMs, timeZone)
351
+ const direct = matchesParts(schedule, parts)
352
+
353
+ // Wildcard-hour schedules (e.g. `0 * * * *`, `*/15 * * * *`) run on every matching local clock tick (once per real hour across DST).
354
+ if (isUtcZone(timeZone) || schedule.hours.size === 24)
355
+ return direct
356
+
357
+ if (direct)
358
+ return !isRepeatedWallMinute(minuteMs, parts, timeZone)
359
+
360
+ return matchesSpringGap(schedule, minuteMs, parts, timeZone)
361
+ }
362
+
363
+ function advanceCursor(
364
+ cursor: number,
365
+ currOffset: number,
366
+ step: number,
367
+ timeZone: string,
368
+ utc: boolean,
369
+ ): { cursor: number, prevOffset: number } {
370
+ const next = cursor + step
371
+ if (!utc && step > MINUTE_MS) {
372
+ const nextOffset = offsetAt(next, timeZone)
373
+ if (nextOffset > currOffset) {
374
+ const transition = findSpringTransition(cursor, next, currOffset, timeZone)
375
+ return { cursor: transition, prevOffset: offsetAt(transition - MINUTE_MS, timeZone) }
376
+ }
377
+ return { cursor: next, prevOffset: nextOffset }
378
+ }
379
+ return { cursor: next, prevOffset: currOffset }
380
+ }
289
381
 
290
382
  /** Computes the next exact minute boundary (UTC epoch ms) strictly after `afterMs` matching `schedule` in `timeZone`. */
291
383
  export function nextCronTick(schedule: ParsedCron, afterMs: number, timeZone = 'UTC'): number {
292
384
  assertValidTimeZone(timeZone)
293
385
  let cursor = (Math.floor(afterMs / MINUTE_MS) + 1) * MINUTE_MS
294
386
  const limit = cursor + MAX_SEARCH_SPAN_MS
387
+ const utc = isUtcZone(timeZone)
388
+ const dstAware = !utc && schedule.hours.size < 24
389
+ let prevOffset = utc ? 0 : offsetAt(cursor - MINUTE_MS, timeZone)
295
390
 
296
391
  while (cursor <= limit) {
297
392
  const parts = getZonedParts(cursor, timeZone)
393
+ const currOffset = utc ? 0 : wallMsFromParts(parts) - cursor
394
+
395
+ if (dstAware && currOffset > prevOffset && matchesSpringGap(schedule, cursor, parts, timeZone))
396
+ return cursor
397
+
298
398
  if (!schedule.months.has(parts.month) || !matchesDay(schedule, parts.day, parts.dow)) {
299
- if (parts.hour < 22)
300
- cursor += (22 - parts.hour) * 60 * MINUTE_MS
301
- else
302
- cursor += Math.min(30, 60 - parts.minute) * MINUTE_MS
399
+ const step = parts.hour < 21
400
+ ? (21 - parts.hour) * 60 * MINUTE_MS
401
+ : Math.min(30, 60 - parts.minute) * MINUTE_MS
402
+ const adv = advanceCursor(cursor, currOffset, step, timeZone, utc)
403
+ cursor = adv.cursor
404
+ prevOffset = adv.prevOffset
303
405
  continue
304
406
  }
305
407
  if (!schedule.hours.has(parts.hour)) {
306
- cursor += Math.min(30, 60 - parts.minute) * MINUTE_MS
408
+ const step = Math.min(30, 60 - parts.minute) * MINUTE_MS
409
+ const adv = advanceCursor(cursor, currOffset, step, timeZone, utc)
410
+ cursor = adv.cursor
411
+ prevOffset = adv.prevOffset
307
412
  continue
308
413
  }
309
414
  if (!schedule.minutes.has(parts.minute)) {
415
+ prevOffset = currOffset
416
+ cursor += MINUTE_MS
417
+ continue
418
+ }
419
+ if (dstAware && isRepeatedWallMinute(cursor, parts, timeZone)) {
420
+ prevOffset = currOffset
310
421
  cursor += MINUTE_MS
311
422
  continue
312
423
  }