@prunellejs/jobs 0.7.2 → 0.7.4

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 +123 -11
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.4",
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.4"
25
25
  }
26
26
  }
package/src/cron.ts CHANGED
@@ -199,13 +199,14 @@ function parseFieldSegment(segment: string, spec: FieldSpec, expr: string, out:
199
199
  throw new RangeError(`@prunellejs/jobs: invalid cron expression "${expr}": malformed ${spec.label} range "${segment}".`)
200
200
 
201
201
  if (dashParts.length === 1) {
202
- const start = parseAtom(dashParts[0]!, spec, expr)
202
+ const rawStart = parseAtom(dashParts[0]!, spec, expr)
203
+ const start = wrapSeven && rawStart === 7 ? 0 : rawStart
203
204
  if (slashParts.length === 2) {
204
205
  const end = wrapSeven ? 6 : spec.max
205
206
  addRange(out, start, end, step, wrapSeven)
206
207
  }
207
208
  else {
208
- out.add(wrapSeven && start === 7 ? 0 : start)
209
+ out.add(start)
209
210
  }
210
211
  return
211
212
  }
@@ -276,37 +277,148 @@ function matchesDay(schedule: ParsedCron, day: number, dow: number): boolean {
276
277
  return domOk && dowOk
277
278
  }
278
279
 
279
- export function matchesCron(schedule: ParsedCron, tsMs: number, timeZone = 'UTC'): boolean {
280
- const parts = getZonedParts(tsMs, timeZone)
280
+ const MINUTE_MS = 60_000
281
+ const MAX_DST_SHIFT_MS = 3 * 60 * MINUTE_MS
282
+ const MAX_SEARCH_SPAN_MS = 5 * 366 * 24 * 60 * MINUTE_MS
283
+
284
+ function isUtcZone(timeZone: string): boolean {
285
+ return timeZone === 'UTC' || timeZone === 'Etc/UTC'
286
+ }
287
+
288
+ function wallMsFromParts(parts: ZonedDateParts): number {
289
+ return Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute)
290
+ }
291
+
292
+ function offsetAt(minuteMs: number, timeZone: string): number {
293
+ return wallMsFromParts(getZonedParts(minuteMs, timeZone)) - minuteMs
294
+ }
295
+
296
+ function matchesParts(schedule: ParsedCron, parts: ZonedDateParts): boolean {
281
297
  return schedule.minutes.has(parts.minute)
282
298
  && schedule.hours.has(parts.hour)
283
299
  && schedule.months.has(parts.month)
284
300
  && matchesDay(schedule, parts.day, parts.dow)
285
301
  }
286
302
 
287
- const MINUTE_MS = 60_000
288
- const MAX_SEARCH_SPAN_MS = 5 * 366 * 24 * 60 * MINUTE_MS
303
+ /** True when `minuteMs` is the first valid instant after a spring-forward jump whose skipped wall-clock minutes match `schedule`. */
304
+ function matchesSpringGap(schedule: ParsedCron, minuteMs: number, parts: ZonedDateParts, timeZone: string): boolean {
305
+ const currWall = wallMsFromParts(parts)
306
+ const prevWall = wallMsFromParts(getZonedParts(minuteMs - MINUTE_MS, timeZone))
307
+ const gap = currWall - prevWall
308
+ if (gap <= MINUTE_MS || gap > MAX_DST_SHIFT_MS + MINUTE_MS)
309
+ return false
310
+ for (let w = prevWall + MINUTE_MS; w < currWall; w += MINUTE_MS) {
311
+ const d = new Date(w)
312
+ if (
313
+ schedule.minutes.has(d.getUTCMinutes())
314
+ && schedule.hours.has(d.getUTCHours())
315
+ && schedule.months.has(d.getUTCMonth() + 1)
316
+ && matchesDay(schedule, d.getUTCDate(), d.getUTCDay())
317
+ ) {
318
+ return true
319
+ }
320
+ }
321
+ return false
322
+ }
323
+
324
+ /** True when `minuteMs` is the second (fall-back) occurrence of an ambiguous local wall-clock minute. */
325
+ function isRepeatedWallMinute(minuteMs: number, parts: ZonedDateParts, timeZone: string): boolean {
326
+ const currWall = wallMsFromParts(parts)
327
+ const currOffset = currWall - minuteMs
328
+ const probeOffset = offsetAt(minuteMs - MAX_DST_SHIFT_MS, timeZone)
329
+ if (probeOffset <= currOffset)
330
+ return false
331
+ const shift = probeOffset - currOffset
332
+ return wallMsFromParts(getZonedParts(minuteMs - shift, timeZone)) === currWall
333
+ }
334
+
335
+ /** Binary-searches `(loMs, hiMs]` for the first UTC minute whose timezone offset is strictly greater than `loOffset`. */
336
+ function findSpringTransition(loMs: number, hiMs: number, loOffset: number, timeZone: string): number {
337
+ let lo = loMs
338
+ let hi = hiMs
339
+ while (hi - lo > MINUTE_MS) {
340
+ const mid = floorToMinute(lo + Math.floor((hi - lo) / 2))
341
+ if (offsetAt(mid, timeZone) > loOffset)
342
+ hi = mid
343
+ else
344
+ lo = mid
345
+ }
346
+ return hi
347
+ }
348
+
349
+ export function matchesCron(schedule: ParsedCron, tsMs: number, timeZone = 'UTC'): boolean {
350
+ const minuteMs = floorToMinute(tsMs)
351
+ const parts = getZonedParts(minuteMs, timeZone)
352
+ const direct = matchesParts(schedule, parts)
353
+
354
+ // Wildcard-hour schedules (e.g. `0 * * * *`, `*/15 * * * *`) run on every matching local clock tick (once per real hour across DST).
355
+ if (isUtcZone(timeZone) || schedule.hours.size === 24)
356
+ return direct
357
+
358
+ if (direct)
359
+ return !isRepeatedWallMinute(minuteMs, parts, timeZone)
360
+
361
+ return matchesSpringGap(schedule, minuteMs, parts, timeZone)
362
+ }
363
+
364
+ function advanceCursor(
365
+ cursor: number,
366
+ currOffset: number,
367
+ step: number,
368
+ timeZone: string,
369
+ utc: boolean,
370
+ ): { cursor: number, prevOffset: number } {
371
+ const next = cursor + step
372
+ if (!utc && step > MINUTE_MS) {
373
+ const nextOffset = offsetAt(next, timeZone)
374
+ if (nextOffset > currOffset) {
375
+ const transition = findSpringTransition(cursor, next, currOffset, timeZone)
376
+ return { cursor: transition, prevOffset: offsetAt(transition - MINUTE_MS, timeZone) }
377
+ }
378
+ return { cursor: next, prevOffset: nextOffset }
379
+ }
380
+ return { cursor: next, prevOffset: currOffset }
381
+ }
289
382
 
290
383
  /** Computes the next exact minute boundary (UTC epoch ms) strictly after `afterMs` matching `schedule` in `timeZone`. */
291
384
  export function nextCronTick(schedule: ParsedCron, afterMs: number, timeZone = 'UTC'): number {
292
385
  assertValidTimeZone(timeZone)
293
386
  let cursor = (Math.floor(afterMs / MINUTE_MS) + 1) * MINUTE_MS
294
387
  const limit = cursor + MAX_SEARCH_SPAN_MS
388
+ const utc = isUtcZone(timeZone)
389
+ const dstAware = !utc && schedule.hours.size < 24
390
+ let prevOffset = utc ? 0 : offsetAt(cursor - MINUTE_MS, timeZone)
295
391
 
296
392
  while (cursor <= limit) {
297
393
  const parts = getZonedParts(cursor, timeZone)
394
+ const currOffset = utc ? 0 : wallMsFromParts(parts) - cursor
395
+
396
+ if (dstAware && currOffset > prevOffset && matchesSpringGap(schedule, cursor, parts, timeZone))
397
+ return cursor
398
+
298
399
  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
400
+ const step = parts.hour < 21
401
+ ? (21 - parts.hour) * 60 * MINUTE_MS
402
+ : Math.min(30, 60 - parts.minute) * MINUTE_MS
403
+ const adv = advanceCursor(cursor, currOffset, step, timeZone, utc)
404
+ cursor = adv.cursor
405
+ prevOffset = adv.prevOffset
303
406
  continue
304
407
  }
305
408
  if (!schedule.hours.has(parts.hour)) {
306
- cursor += Math.min(30, 60 - parts.minute) * MINUTE_MS
409
+ const step = Math.min(30, 60 - parts.minute) * MINUTE_MS
410
+ const adv = advanceCursor(cursor, currOffset, step, timeZone, utc)
411
+ cursor = adv.cursor
412
+ prevOffset = adv.prevOffset
307
413
  continue
308
414
  }
309
415
  if (!schedule.minutes.has(parts.minute)) {
416
+ prevOffset = currOffset
417
+ cursor += MINUTE_MS
418
+ continue
419
+ }
420
+ if (dstAware && isRepeatedWallMinute(cursor, parts, timeZone)) {
421
+ prevOffset = currOffset
310
422
  cursor += MINUTE_MS
311
423
  continue
312
424
  }