@objectstack/trigger-schedule 17.0.0 → 17.2.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/CHANGELOG.md +239 -0
- package/README.md +4 -4
- package/dist/index.d.mts +66 -2
- package/dist/index.d.ts +66 -2
- package/dist/index.js +86 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +85 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/schedule-trigger.ts","../src/plugin.ts","../src/time-relative-trigger.ts","../src/time-relative-plugin.ts"],"sourcesContent":["// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { AutomationContext } from '@objectstack/spec/contracts';\nimport type { JobSchedule, JobHandler } from '@objectstack/spec/contracts';\n\n/**\n * Structural mirror of the automation engine's `FlowTriggerBinding`\n * (service-automation/src/engine.ts). Declared locally so this trigger plugin\n * stays decoupled from the automation package — same pattern the record-change\n * trigger and the connector / messaging integrations use. The engine parses the\n * flow's start node and hands us a binding whose `schedule` carries the\n * cron/interval/once descriptor.\n */\nexport interface FlowTriggerBinding {\n readonly flowName: string;\n readonly object?: string;\n readonly event?: string;\n readonly condition?: string | { dialect?: string; source?: string; ast?: unknown };\n readonly schedule?: unknown;\n readonly config?: Record<string, unknown>;\n}\n\n/**\n * Structural mirror of the engine's `FlowTrigger` extension point. The engine\n * calls {@link start} with a parsed binding + a callback that runs the flow,\n * and {@link stop} when the flow is unregistered/disabled.\n */\nexport interface FlowTrigger {\n readonly type: string;\n start(binding: FlowTriggerBinding, callback: (ctx: AutomationContext) => Promise<void>): void;\n stop(flowName: string): void;\n}\n\n/**\n * The slice of `IJobService` this trigger needs: schedule a named job and\n * cancel it. Typed structurally so the plugin depends on the spec contract\n * shape, not a concrete adapter.\n */\nexport interface JobServiceSurface {\n schedule(name: string, schedule: JobSchedule, handler: JobHandler): Promise<void>;\n cancel(name: string): Promise<void>;\n}\n\n/** Minimal logger surface (matches core's `ctx.logger`). */\nexport interface TriggerLogger {\n info(msg: string, ...args: unknown[]): void;\n warn(msg: string, ...args: unknown[]): void;\n debug?(msg: string, ...args: unknown[]): void;\n /**\n * Execution failures log here when available (falling back to `warn`).\n * ERROR matters operationally: the CLI's boot-quiet window swallows\n * stdout (debug/info/warn) but stderr (error/fatal) always lands — so a\n * per-record sweep failure stays visible. Mirrors the record-change\n * trigger's logger surface.\n */\n error?(msg: string, ...args: unknown[]): void;\n}\n\nconst JOB_PREFIX = 'flow-schedule';\n\n/**\n * Report a scheduled flow that failed to bind to the job service.\n *\n * **Why this is `error` and not `warn`** — the repo's degradation-log-level\n * rule (AGENTS.md) decides the level with one question: after the degradation,\n * does the system still look normal from the outside while something it claims\n * is in place has not landed? Here it does, completely: the flow stays\n * published and active in `sys_metadata`, Studio lists it, the metadata API\n * serves it and `verify_build` passes — while nothing will ever fire it. That\n * is persisted state and runtime state disagreeing, which the rule puts in the\n * `error` class, not the functional-degradation class.\n *\n * The neighbouring composition branch — \"no job service is registered at all\" —\n * deliberately stays at `warn`: the system is *visibly* smaller and the rule\n * names that exact message as correctly a `warn`. The distinction is not the\n * severity of the outcome, it is whether the outside can see it.\n *\n * An `error` here owes two things, both in the first line it prints: the\n * concrete consequence (including that everything else keeps looking healthy)\n * and the remedy. Kept in one helper so both triggers say it the same way.\n */\nexport function reportBindFailure(\n logger: TriggerLogger,\n tag: 'schedule' | 'time-relative',\n flowName: string,\n err: unknown,\n): void {\n const report = logger.error?.bind(logger) ?? logger.warn.bind(logger);\n report(\n `[${tag}] flow '${flowName}' FAILED to bind to the job service: ${(err as Error)?.message ?? String(err)}. ` +\n 'The flow stays published and active — Studio, the metadata API and verify_build all keep reporting it ' +\n 'healthy — but nothing will fire it until it binds. Re-publish the flow (or restart the environment) to retry.',\n );\n}\n\n/**\n * Normalize a flow's raw `schedule` descriptor into a {@link JobSchedule}, or\n * `null` if it can't be understood. Accepts the canonical\n * `{ type: 'cron'|'interval'|'once', ... }` shape plus a few ergonomic\n * shorthands (a bare cron string, `{ cron }`, `{ expression }`, `{ every }` /\n * `{ intervalMs }`, `{ at }`).\n */\nexport function normalizeSchedule(raw: unknown): JobSchedule | null {\n if (raw == null) return null;\n\n // Bare cron string, e.g. '0 1 * * *'.\n if (typeof raw === 'string') {\n const expr = raw.trim();\n return expr ? { type: 'cron', expression: expr } : null;\n }\n\n if (typeof raw !== 'object') return null;\n const s = raw as Record<string, unknown>;\n\n const type = typeof s.type === 'string' ? s.type : undefined;\n\n if (type === 'cron' || (!type && (typeof s.cron === 'string' || typeof s.expression === 'string'))) {\n const expression =\n (typeof s.expression === 'string' && s.expression) ||\n (typeof s.cron === 'string' && s.cron) ||\n undefined;\n if (!expression) return null;\n const out: JobSchedule = { type: 'cron', expression };\n if (typeof s.timezone === 'string') out.timezone = s.timezone;\n return out;\n }\n\n if (type === 'interval' || (!type && (typeof s.intervalMs === 'number' || typeof s.every === 'number'))) {\n const intervalMs =\n (typeof s.intervalMs === 'number' && s.intervalMs) ||\n (typeof s.every === 'number' && s.every) ||\n undefined;\n if (!intervalMs || intervalMs <= 0) return null;\n return { type: 'interval', intervalMs };\n }\n\n if (type === 'once' || (!type && typeof s.at === 'string')) {\n const at = typeof s.at === 'string' ? s.at : undefined;\n if (!at) return null;\n return { type: 'once', at };\n }\n\n return null;\n}\n\n/**\n * ScheduleTrigger\n *\n * Bridges the automation engine's {@link FlowTrigger} extension point to the\n * platform {@link JobServiceSurface}. For each schedule-triggered flow the\n * engine activates, it registers a job whose handler runs the flow; the job\n * service owns the actual cron/interval/once timing (so this trigger stays\n * adapter-agnostic — cron schedules need a cron-capable adapter, which the\n * job service selects).\n *\n * The job service is resolved lazily (per `start()`) via the supplied accessor,\n * so we always pick up the job service's *upgraded* adapter (e.g. the durable\n * DbJobAdapter that replaces the bootstrap interval adapter on `kernel:ready`).\n */\nexport class ScheduleTrigger implements FlowTrigger {\n readonly type = 'schedule';\n\n private readonly getJobService: () => JobServiceSurface | null;\n private readonly logger: TriggerLogger;\n /** flowName → job name registered for it, so stop() can cancel it. */\n private readonly bound = new Map<string, string>();\n\n constructor(getJobService: () => JobServiceSurface | null, logger: TriggerLogger) {\n this.getJobService = getJobService;\n this.logger = logger;\n }\n\n start(binding: FlowTriggerBinding, callback: (ctx: AutomationContext) => Promise<void>): void {\n const raw = binding.schedule ?? (binding.config as Record<string, unknown> | undefined)?.schedule;\n const schedule = normalizeSchedule(raw);\n if (!schedule) {\n this.logger.warn(\n `[schedule] flow '${binding.flowName}' has no recognizable schedule descriptor — not bound`,\n );\n return;\n }\n\n const jobService = this.getJobService();\n if (!jobService || typeof jobService.schedule !== 'function') {\n this.logger.warn(\n `[schedule] job service unavailable — flow '${binding.flowName}' not scheduled`,\n );\n return;\n }\n\n // Idempotent: drop any prior schedule for this flow before re-binding\n // (covers disable→enable cycles and hot reload).\n this.stop(binding.flowName);\n\n const jobName = `${JOB_PREFIX}:${binding.flowName}`;\n\n const handler: JobHandler = async ({ jobId }) => {\n try {\n const ctx: AutomationContext = {\n event: 'schedule',\n params: {\n jobId,\n flowName: binding.flowName,\n schedule,\n },\n };\n await callback(ctx);\n } catch (err) {\n // Error isolation: a scheduled flow failure must not crash the\n // job runner / ticker. Log and swallow.\n this.logger.warn(\n `[schedule] flow '${binding.flowName}' execution failed: ${(err as Error)?.message ?? String(err)}`,\n );\n }\n };\n\n this.bound.set(binding.flowName, jobName);\n // FlowTrigger.start is sync; the job service's schedule() is async.\n // Fire-and-forget with error logging.\n void Promise.resolve(jobService.schedule(jobName, schedule, handler))\n .then(() => {\n this.logger.info(\n `[schedule] bound flow '${binding.flowName}' → ${schedule.type}` +\n (schedule.expression ? ` '${schedule.expression}'` : '') +\n (schedule.intervalMs ? ` every ${schedule.intervalMs}ms` : '') +\n (schedule.at ? ` at ${schedule.at}` : ''),\n );\n })\n .catch((err) => {\n this.bound.delete(binding.flowName);\n reportBindFailure(this.logger, 'schedule', binding.flowName, err);\n });\n }\n\n stop(flowName: string): void {\n const jobName = this.bound.get(flowName);\n if (!jobName) return;\n this.bound.delete(flowName);\n const jobService = this.getJobService();\n if (!jobService || typeof jobService.cancel !== 'function') return;\n void Promise.resolve(jobService.cancel(jobName))\n .then(() => this.logger.debug?.(`[schedule] unbound flow '${flowName}'`))\n .catch((err) => {\n this.logger.warn(\n `[schedule] failed to unbind flow '${flowName}': ${(err as Error)?.message ?? String(err)}`,\n );\n });\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Plugin, PluginContext } from '@objectstack/core';\nimport { ScheduleTrigger } from './schedule-trigger.js';\nimport type { FlowTrigger, JobServiceSurface } from './schedule-trigger.js';\n\n/**\n * The slice of the automation engine this plugin needs: register a trigger on\n * its `FlowTrigger` extension point. Declared structurally so the plugin does\n * not take a build dependency on `@objectstack/service-automation`.\n */\ninterface AutomationTriggerRegistry {\n registerTrigger(trigger: FlowTrigger): void;\n unregisterTrigger?(type: string): void;\n}\n\n/**\n * ScheduleTriggerPlugin\n *\n * Makes schedule-triggered flows actually fire. The automation engine ships the\n * `FlowTrigger` wiring (it parses each flow's start node — `flow.type ===\n * 'schedule'` or a start-node `config.schedule` descriptor — into a binding and\n * calls `trigger.start(...)`), but the *concrete* schedule trigger lives here as\n * a plugin and delegates timing to the platform `IJobService` (the `'job'`\n * service). This mirrors the connector / record-change split (engine baseline +\n * trigger plugin).\n *\n * With this plugin (and a job service) installed, a flow whose start node\n * declares `config: { schedule: { type: 'cron', expression: '0 1 * * *' } }`\n * auto-launches on that schedule — no manual `engine.execute()`.\n *\n * Depends on the job service plugin so its `kernel:ready` upgrade (to the\n * durable DbJobAdapter) runs before ours; the job service is nonetheless\n * resolved lazily per `start()` so we always use its current adapter.\n */\nexport class ScheduleTriggerPlugin implements Plugin {\n name = 'com.objectstack.trigger.schedule';\n type = 'standard';\n version = '7.3.0';\n dependencies = ['com.objectstack.service.job'];\n\n async init(ctx: PluginContext): Promise<void> {\n ctx.logger.info('Schedule trigger plugin initialized');\n }\n\n async start(ctx: PluginContext): Promise<void> {\n // The automation service + job service are resolvable once the kernel is\n // ready (kernel:ready fires after AutomationServicePlugin.start() has\n // pulled flows in and after the job service upgrades its adapter).\n ctx.hook('kernel:ready', async () => {\n const automation = this.resolveService<AutomationTriggerRegistry>(ctx, 'automation');\n if (!automation || typeof automation.registerTrigger !== 'function') {\n ctx.logger.warn(\n 'ScheduleTriggerPlugin: automation service not available — schedule trigger NOT installed',\n );\n return;\n }\n\n // Probe once for a clear startup warning; the trigger re-resolves\n // lazily on each start() so adapter upgrades are always picked up.\n if (!this.resolveService<JobServiceSurface>(ctx, 'job')) {\n ctx.logger.warn(\n 'ScheduleTriggerPlugin: job service not available — scheduled flows will not run until one is registered',\n );\n }\n\n const trigger = new ScheduleTrigger(\n () => this.resolveService<JobServiceSurface>(ctx, 'job'),\n ctx.logger,\n );\n automation.registerTrigger(trigger);\n ctx.logger.info('ScheduleTriggerPlugin: schedule trigger registered');\n });\n }\n\n private resolveService<T>(ctx: PluginContext, name: string): T | null {\n try {\n return ctx.getService<T>(name) ?? null;\n } catch {\n return null;\n }\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { AutomationContext, JobSchedule, JobHandler } from '@objectstack/spec/contracts';\nimport {\n TimeRelativeTriggerSchema,\n TIME_RELATIVE_DEFAULT_CRON,\n TIME_RELATIVE_DEFAULT_MAX_RECORDS,\n} from '@objectstack/spec/automation';\nimport type { TimeRelativeTrigger as TimeRelativeDescriptor } from '@objectstack/spec/automation';\nimport { normalizeSchedule, reportBindFailure } from './schedule-trigger.js';\nimport type { FlowTrigger, FlowTriggerBinding, JobServiceSurface, TriggerLogger } from './schedule-trigger.js';\n\n/**\n * The slice of the ObjectQL data engine this trigger needs: run a filtered\n * `find` (to discover the records whose date field falls in the window) and,\n * optionally, probe whether an object is registered. Typed structurally — same\n * decoupling pattern the record-change trigger uses for its hook surface — so\n * this plugin does not take a build dependency on the engine package.\n */\nexport interface TimeRelativeDataEngine {\n find(\n objectName: string,\n query?: {\n where?: Record<string, unknown>;\n fields?: string[];\n limit?: number;\n /** Elevated context — a background sweep must see all rows, not RLS-scoped ones. */\n context?: { isSystem?: boolean };\n },\n ): Promise<Array<Record<string, unknown>> | undefined>;\n /**\n * Optional object-existence probe (the ObjectQL engine's `getObject`).\n * When present, {@link TimeRelativeTrigger.start} uses it to call out a\n * descriptor whose `object` matches no registered object at bind time —\n * otherwise the sweep just quietly finds nothing forever.\n */\n getObject?(name: string): unknown;\n}\n\n/** Job-name namespace so time-relative sweeps never collide with plain schedule jobs. */\nconst JOB_PREFIX = 'flow-time-relative';\n\nconst MS_PER_DAY = 86_400_000;\n\n/** A closed, inclusive instant window `[gte, lte]` as ISO-8601 strings. */\nexport interface DateWindow {\n /** Lower bound (inclusive), ISO-8601. */\n gte: string;\n /** Upper bound (inclusive), ISO-8601. */\n lte: string;\n}\n\n// ─── Pure window math (day-granular, UTC) ───────────────────────────\n\n/** Start of `d`'s UTC calendar day (00:00:00.000Z). */\nfunction startOfUtcDay(d: Date): Date {\n return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), 0, 0, 0, 0));\n}\n\n/** End of `d`'s UTC calendar day (23:59:59.999Z) — inclusive upper bound. */\nfunction endOfUtcDay(d: Date): Date {\n return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), 23, 59, 59, 999));\n}\n\n/** `d`'s UTC day shifted by `n` whole days (exact in UTC — no DST drift). */\nfunction addUtcDays(d: Date, n: number): Date {\n return new Date(startOfUtcDay(d).getTime() + n * MS_PER_DAY);\n}\n\n/**\n * Compute the inclusive date window(s) a descriptor selects, relative to `now`.\n *\n * - `offsetDays` → one single-day window per offset (`today + offset`), so the\n * sweep fires exactly on each threshold day (the robust T-minus reminder).\n * - `withinDays` → one range window: `[today, today + N]` when N ≥ 0 (upcoming),\n * or `[today − |N|, today]` when N < 0 (overdue lookback). Always includes today.\n *\n * Day-granular and computed in UTC. The upper bound is the *end* of its day\n * (`23:59:59.999Z`), so a `datetime` field matches for the whole day and a\n * `date` field (compared as `YYYY-MM-DD` after the driver truncates) is inclusive.\n */\nexport function computeDateWindows(desc: TimeRelativeDescriptor, now: Date): DateWindow[] {\n const today = startOfUtcDay(now);\n\n if (desc.offsetDays && desc.offsetDays.length > 0) {\n return desc.offsetDays.map((offset) => {\n const day = addUtcDays(today, offset);\n return { gte: startOfUtcDay(day).toISOString(), lte: endOfUtcDay(day).toISOString() };\n });\n }\n\n const n = desc.withinDays ?? 0;\n if (n >= 0) {\n return [{ gte: startOfUtcDay(today).toISOString(), lte: endOfUtcDay(addUtcDays(today, n)).toISOString() }];\n }\n // Negative: window extends into the past, still anchored to (and including) today.\n return [{ gte: startOfUtcDay(addUtcDays(today, n)).toISOString(), lte: endOfUtcDay(today).toISOString() }];\n}\n\n/**\n * Build the ObjectQL `where` map for one date window: the descriptor's static\n * `filter` (if any) ANDed with a `$gte`/`$lte` range on the date field. The map\n * form is the canonical filter shape both drivers evaluate verbatim (the same\n * shape the platform's own retention sweep uses).\n */\nexport function buildWindowWhere(desc: TimeRelativeDescriptor, window: DateWindow): Record<string, unknown> {\n return {\n ...(desc.filter ?? {}),\n [desc.dateField]: { $gte: window.gte, $lte: window.lte },\n };\n}\n\nfunction errMessage(err: unknown): string {\n return (err as Error)?.message ?? String(err);\n}\n\n/**\n * TimeRelativeTrigger\n *\n * The declarative answer to \"act on records whose date field is coming up (or\n * overdue)\" (#1874). Instead of the fragile date-equality-on-record-change\n * pattern (which only fires if the record happens to be edited on the threshold\n * day) or a hand-rolled cron + range query per flow, a flow whose start node\n * declares `config.timeRelative` is swept on a schedule (daily by default) and\n * launched **once per matching record**.\n *\n * It composes the schedule trigger's two collaborators:\n * - the platform {@link JobServiceSurface} owns the sweep cadence (like the\n * plain schedule trigger), and\n * - the {@link TimeRelativeDataEngine} runs the date-window query (like the\n * record-change trigger reaching ObjectQL).\n *\n * Both are resolved lazily (per call) so adapter upgrades — the durable job\n * adapter that replaces the bootstrap ticker on `kernel:ready`, a late-registered\n * data engine — are always picked up. The engine owns the start-node `condition`\n * gate and `runAs` identity, so this trigger only has to put the matched record\n * on the {@link AutomationContext}; `{record.<field>}` interpolation and the\n * condition work exactly as they do for a record-change flow.\n */\nexport class TimeRelativeTrigger implements FlowTrigger {\n readonly type = 'time_relative';\n\n private readonly getJobService: () => JobServiceSurface | null;\n private readonly getDataEngine: () => TimeRelativeDataEngine | null;\n private readonly logger: TriggerLogger;\n /** Injectable clock so window math is deterministic under test. */\n private readonly now: () => Date;\n /** flowName → job name registered for it, so stop() can cancel it. */\n private readonly bound = new Map<string, string>();\n\n constructor(\n getJobService: () => JobServiceSurface | null,\n getDataEngine: () => TimeRelativeDataEngine | null,\n logger: TriggerLogger,\n now: () => Date = () => new Date(),\n ) {\n this.getJobService = getJobService;\n this.getDataEngine = getDataEngine;\n this.logger = logger;\n this.now = now;\n }\n\n start(binding: FlowTriggerBinding, callback: (ctx: AutomationContext) => Promise<void>): void {\n const raw = (binding.config as Record<string, unknown> | undefined)?.timeRelative;\n const parsed = TimeRelativeTriggerSchema.safeParse(raw);\n if (!parsed.success) {\n this.logger.warn(\n `[time-relative] flow '${binding.flowName}' has no valid \\`timeRelative\\` descriptor — not bound. ` +\n `Provide { object, dateField, and exactly one of withinDays | offsetDays }. ` +\n `(${parsed.error.issues.map((i) => `${i.path.join('.') || '(root)'}: ${i.message}`).join('; ')})`,\n );\n return;\n }\n const desc = parsed.data;\n\n // Cadence: the flow's start-node schedule descriptor, or a daily default.\n // A daily sweep is the whole point (evaluate the window every day so a\n // threshold day is never missed), so an omitted schedule means \"daily\",\n // not \"never\".\n const schedule: JobSchedule =\n normalizeSchedule(binding.schedule) ?? { type: 'cron', expression: TIME_RELATIVE_DEFAULT_CRON };\n\n const jobService = this.getJobService();\n if (!jobService || typeof jobService.schedule !== 'function') {\n this.logger.warn(\n `[time-relative] job service unavailable — flow '${binding.flowName}' not scheduled`,\n );\n return;\n }\n\n // Best-effort object-existence probe at bind time (the engine may be\n // available now even though the sweep resolves it lazily). A descriptor\n // targeting an unknown object would sweep forever finding nothing.\n const engineNow = this.getDataEngine();\n if (desc.object && engineNow && typeof engineNow.getObject === 'function') {\n let known: unknown;\n try {\n known = engineNow.getObject(desc.object);\n } catch {\n known = undefined;\n }\n if (!known) {\n this.logger.warn(\n `[time-relative] flow '${binding.flowName}' targets unknown object '${desc.object}' — the sweep is bound but will match nothing until that object is registered. ` +\n `Object names match exactly; check config.timeRelative.object.`,\n );\n }\n }\n\n // Idempotent: drop any prior schedule for this flow before re-binding\n // (covers disable→enable cycles and hot reload).\n this.stop(binding.flowName);\n\n const jobName = `${JOB_PREFIX}:${binding.flowName}`;\n const maxRecords = desc.maxRecords ?? TIME_RELATIVE_DEFAULT_MAX_RECORDS;\n\n const handler: JobHandler = async () => {\n try {\n await this.sweep(binding.flowName, desc, maxRecords, callback);\n } catch (err) {\n // Error isolation: a sweep failure must not crash the job\n // runner / ticker. Log and swallow.\n this.logger.warn(\n `[time-relative] flow '${binding.flowName}' sweep failed: ${errMessage(err)}`,\n );\n }\n };\n\n this.bound.set(binding.flowName, jobName);\n // FlowTrigger.start is sync; the job service's schedule() is async.\n // Fire-and-forget with error logging (mirrors ScheduleTrigger).\n void Promise.resolve(jobService.schedule(jobName, schedule, handler))\n .then(() => {\n const mode = desc.offsetDays\n ? `offsets [${desc.offsetDays.join(', ')}]d`\n : `within ${desc.withinDays}d`;\n this.logger.info(\n `[time-relative] bound flow '${binding.flowName}' → sweep '${desc.object}.${desc.dateField}' ${mode} on ${schedule.type}` +\n (schedule.expression ? ` '${schedule.expression}'` : '') +\n (schedule.intervalMs ? ` every ${schedule.intervalMs}ms` : ''),\n );\n })\n .catch((err) => {\n this.bound.delete(binding.flowName);\n reportBindFailure(this.logger, 'time-relative', binding.flowName, err);\n });\n }\n\n /**\n * Run one sweep: query each date window, union the matched records (deduped\n * by id, capped at `maxRecords`), and launch the flow once per record. A\n * per-record failure is isolated so one bad row never aborts the batch.\n */\n private async sweep(\n flowName: string,\n desc: TimeRelativeDescriptor,\n maxRecords: number,\n callback: (ctx: AutomationContext) => Promise<void>,\n ): Promise<void> {\n const engine = this.getDataEngine();\n if (!engine || typeof engine.find !== 'function') {\n this.logger.warn(\n `[time-relative] data engine unavailable — flow '${flowName}' sweep skipped this tick`,\n );\n return;\n }\n\n const windows = computeDateWindows(desc, this.now());\n const seenIds = new Set<unknown>();\n const matched: Array<Record<string, unknown>> = [];\n\n for (const window of windows) {\n if (matched.length >= maxRecords) break;\n const where = buildWindowWhere(desc, window);\n const rows =\n (await engine.find(desc.object, {\n where,\n limit: maxRecords,\n context: { isSystem: true },\n })) ?? [];\n for (const row of rows) {\n const id = (row as { id?: unknown }).id;\n // Dedup across windows (offset mode) by id; rows without an id\n // are always kept (can't dedup, better than dropping).\n if (id != null) {\n if (seenIds.has(id)) continue;\n seenIds.add(id);\n }\n matched.push(row);\n if (matched.length >= maxRecords) break;\n }\n }\n\n if (matched.length >= maxRecords) {\n this.logger.warn(\n `[time-relative] flow '${flowName}' sweep hit the ${maxRecords}-record cap — some matching records were NOT processed this tick. ` +\n `Narrow the window/filter, or raise config.timeRelative.maxRecords.`,\n );\n }\n\n let launched = 0;\n let failed = 0;\n for (const record of matched) {\n try {\n const ctx: AutomationContext = {\n record,\n object: desc.object,\n event: 'time_relative',\n // Expose the record as params too, so flows with named `isInput`\n // variables matching record fields get them seeded (parity with\n // the record-change trigger).\n params: record,\n };\n await callback(ctx);\n launched++;\n } catch (err) {\n failed++;\n // Error isolation per record: one failing flow run must not stop\n // the sweep. ERROR when available (stderr survives the CLI's\n // boot-quiet stdout window), else warn.\n const log = this.logger.error?.bind(this.logger) ?? this.logger.warn.bind(this.logger);\n log(\n `[time-relative] flow '${flowName}' failed for record '${String((record as { id?: unknown }).id ?? '?')}': ${errMessage(err)}`,\n );\n }\n }\n\n this.logger.debug?.(\n `[time-relative] flow '${flowName}' swept '${desc.object}': ${matched.length} matched, ${launched} launched, ${failed} failed`,\n );\n }\n\n stop(flowName: string): void {\n const jobName = this.bound.get(flowName);\n if (!jobName) return;\n this.bound.delete(flowName);\n const jobService = this.getJobService();\n if (!jobService || typeof jobService.cancel !== 'function') return;\n void Promise.resolve(jobService.cancel(jobName))\n .then(() => this.logger.debug?.(`[time-relative] unbound flow '${flowName}'`))\n .catch((err) => {\n this.logger.warn(\n `[time-relative] failed to unbind flow '${flowName}': ${errMessage(err)}`,\n );\n });\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Plugin, PluginContext } from '@objectstack/core';\nimport { TimeRelativeTrigger } from './time-relative-trigger.js';\nimport type { TimeRelativeDataEngine } from './time-relative-trigger.js';\nimport type { FlowTrigger, JobServiceSurface } from './schedule-trigger.js';\n\n/**\n * The slice of the automation engine this plugin needs: register a trigger on\n * its `FlowTrigger` extension point. Declared structurally so the plugin does\n * not take a build dependency on `@objectstack/service-automation`.\n */\ninterface AutomationTriggerRegistry {\n registerTrigger(trigger: FlowTrigger): void;\n unregisterTrigger?(type: string): void;\n}\n\n/**\n * TimeRelativeTriggerPlugin\n *\n * Arms **declarative time-relative flows** (#1874): a flow whose start node\n * declares `config.timeRelative` (object + dateField + `withinDays`/`offsetDays`)\n * is swept on a schedule and launched once per record whose date field falls in\n * the window — no hand-written cron + range query, no fragile\n * date-equality-on-record-change.\n *\n * It ships in `@objectstack/trigger-schedule` alongside the plain schedule\n * trigger (both are schedule-driven) but is a **separate** plugin: the\n * time-relative trigger additionally needs the ObjectQL engine (for the sweep\n * query), so keeping it separate leaves the plain `ScheduleTriggerPlugin`'s\n * dependency surface unchanged. Depends on the job service (sweep cadence) and\n * the ObjectQL engine (record discovery); both are resolved lazily per `start()`\n * so adapter upgrades are always picked up.\n */\nexport class TimeRelativeTriggerPlugin implements Plugin {\n name = 'com.objectstack.trigger.time-relative';\n type = 'standard';\n version = '1.0.0';\n dependencies = ['com.objectstack.service.job', 'com.objectstack.engine.objectql'];\n\n async init(ctx: PluginContext): Promise<void> {\n ctx.logger.info('Time-relative trigger plugin initialized');\n }\n\n async start(ctx: PluginContext): Promise<void> {\n // The automation service, job service, and ObjectQL engine are all\n // resolvable once the kernel is ready (kernel:ready fires after\n // AutomationServicePlugin.start() has pulled flows in and after the job\n // service upgrades its adapter).\n ctx.hook('kernel:ready', async () => {\n const automation = this.resolveService<AutomationTriggerRegistry>(ctx, 'automation');\n if (!automation || typeof automation.registerTrigger !== 'function') {\n ctx.logger.warn(\n 'TimeRelativeTriggerPlugin: automation service not available — time-relative trigger NOT installed',\n );\n return;\n }\n\n // Probe once for a clear startup warning; the trigger re-resolves\n // both collaborators lazily on each start()/sweep so late upgrades\n // are always picked up.\n if (!this.resolveService<JobServiceSurface>(ctx, 'job')) {\n ctx.logger.warn(\n 'TimeRelativeTriggerPlugin: job service not available — time-relative sweeps will not run until one is registered',\n );\n }\n if (!this.resolveDataEngine(ctx)) {\n ctx.logger.warn(\n 'TimeRelativeTriggerPlugin: ObjectQL engine not available — time-relative sweeps will find no records until it is',\n );\n }\n\n const trigger = new TimeRelativeTrigger(\n () => this.resolveService<JobServiceSurface>(ctx, 'job'),\n () => this.resolveDataEngine(ctx),\n ctx.logger,\n );\n automation.registerTrigger(trigger);\n ctx.logger.info('TimeRelativeTriggerPlugin: time-relative trigger registered');\n });\n }\n\n private resolveService<T>(ctx: PluginContext, name: string): T | null {\n try {\n return ctx.getService<T>(name) ?? null;\n } catch {\n return null;\n }\n }\n\n private resolveDataEngine(ctx: PluginContext): TimeRelativeDataEngine | null {\n // Primary alias 'objectql', fallback 'data' (some kernels register the\n // engine under both) — same lookup the record-change trigger uses.\n return (\n this.resolveService<TimeRelativeDataEngine>(ctx, 'objectql') ??\n this.resolveService<TimeRelativeDataEngine>(ctx, 'data')\n );\n }\n}\n"],"mappings":";AA0DA,IAAM,aAAa;AAuBZ,SAAS,kBACZ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AY;AACjC,YAAM,aAAa,KAAK,eAA0C,KAAK,YAAY;AACnF,UAAI,CAAC,cAAc,OAAO,WAAW,oBAAoB,YAAY;AACjE,YAAI,OAAO;AAAA,UACP;AAAA,QACJ;AACA;AAAA,MACJ;AAKA,UAAI,CAAC,KAAK,eAAkC,KAAK,KAAK,GAAG;AACrD,YAAI,OAAO;AAAA,UACP;AAAA,QACJ;AAAA,MACJ;AACA,UAAI,CAAC,KAAK,kBAAkB,GAAG,GAAG;AAC9B,YAAI,OAAO;AAAA,UACP;AAAA,QACJ;AAAA,MACJ;AAEA,YAAM,UAAU,IAAI;AAAA,QAChB,MAAM,KAAK,eAAkC,KAAK,KAAK;AAAA,QACvD,MAAM,KAAK,kBAAkB,GAAG;AAAA,QAChC,IAAI;AAAA,MACR;AACA,iBAAW,gBAAgB,OAAO;AAClC,UAAI,OAAO,KAAK,6DAA6D;AAAA,IACjF,CAAC;AAAA,EACL;AAAA,EAEQ,eAAkB,KAAoB,MAAwB;AAClE,QAAI;AACA,aAAO,IAAI,WAAc,IAAI,KAAK;AAAA,IACtC,QAAQ;AACJ,aAAO;AAAA,IACX;AAAA,EACJ;AAAA,EAEQ,kBAAkB,KAAmD;AAGzE,WACI,KAAK,eAAuC,KAAK,UAAU,KAC3D,KAAK,eAAuC,KAAK,MAAM;AAAA,EAE/D;AACJ;","names":["JOB_PREFIX"]}
|
|
1
|
+
{"version":3,"sources":["../src/schedule-trigger.ts","../src/plugin.ts","../src/time-relative-trigger.ts","../src/time-relative-plugin.ts"],"sourcesContent":["// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { AutomationContext } from '@objectstack/spec/contracts';\nimport type { JobSchedule, JobHandler } from '@objectstack/spec/contracts';\n\n/**\n * Structural mirror of the automation engine's `FlowTriggerBinding`\n * (service-automation/src/engine.ts). Declared locally so this trigger plugin\n * stays decoupled from the automation package — same pattern the record-change\n * trigger and the connector / messaging integrations use. The engine parses the\n * flow's start node and hands us a binding whose `schedule` carries the\n * cron/interval/once descriptor.\n */\nexport interface FlowTriggerBinding {\n readonly flowName: string;\n readonly object?: string;\n readonly event?: string;\n readonly condition?: string | { dialect?: string; source?: string; ast?: unknown };\n readonly schedule?: unknown;\n readonly config?: Record<string, unknown>;\n}\n\n/**\n * Structural mirror of the engine's `FlowTrigger` extension point. The engine\n * calls {@link start} with a parsed binding + a callback that runs the flow,\n * and {@link stop} when the flow is unregistered/disabled.\n */\nexport interface FlowTrigger {\n readonly type: string;\n start(binding: FlowTriggerBinding, callback: (ctx: AutomationContext) => Promise<void>): void;\n stop(flowName: string): void;\n}\n\n/**\n * The slice of `IJobService` this trigger needs: schedule a named job and\n * cancel it. Typed structurally so the plugin depends on the spec contract\n * shape, not a concrete adapter.\n */\nexport interface JobServiceSurface {\n schedule(name: string, schedule: JobSchedule, handler: JobHandler): Promise<void>;\n cancel(name: string): Promise<void>;\n}\n\n/** Minimal logger surface (matches core's `ctx.logger`). */\nexport interface TriggerLogger {\n info(msg: string, ...args: unknown[]): void;\n warn(msg: string, ...args: unknown[]): void;\n debug?(msg: string, ...args: unknown[]): void;\n /**\n * Execution failures log here when available (falling back to `warn`).\n * ERROR matters operationally: the CLI's boot-quiet window swallows\n * stdout (debug/info/warn) but stderr (error/fatal) always lands — so a\n * per-record sweep failure stays visible. Mirrors the record-change\n * trigger's logger surface.\n */\n error?(msg: string, ...args: unknown[]): void;\n}\n\nconst JOB_PREFIX = 'flow-schedule';\n\n/**\n * Report a scheduled flow that failed to bind to the job service.\n *\n * **Why this is `error` and not `warn`** — the repo's degradation-log-level\n * rule (AGENTS.md) decides the level with one question: after the degradation,\n * does the system still look normal from the outside while something it claims\n * is in place has not landed? Here it does, completely: the flow stays\n * published and active in `sys_metadata`, Studio lists it, the metadata API\n * serves it and `verify_build` passes — while nothing will ever fire it. That\n * is persisted state and runtime state disagreeing, which the rule puts in the\n * `error` class, not the functional-degradation class.\n *\n * The neighbouring composition branch — \"no job service is registered at all\" —\n * deliberately stays at `warn`: the system is *visibly* smaller and the rule\n * names that exact message as correctly a `warn`. The distinction is not the\n * severity of the outcome, it is whether the outside can see it.\n *\n * An `error` here owes two things, both in the first line it prints: the\n * concrete consequence (including that everything else keeps looking healthy)\n * and the remedy. Kept in one helper so both triggers say it the same way.\n */\nexport function reportBindFailure(\n logger: TriggerLogger,\n tag: 'schedule' | 'time-relative',\n flowName: string,\n err: unknown,\n): void {\n const report = logger.error?.bind(logger) ?? logger.warn.bind(logger);\n report(\n `[${tag}] flow '${flowName}' FAILED to bind to the job service: ${(err as Error)?.message ?? String(err)}. ` +\n 'The flow stays published and active — Studio, the metadata API and verify_build all keep reporting it ' +\n 'healthy — but nothing will fire it until it binds. Re-publish the flow (or restart the environment) to retry.',\n );\n}\n\n/**\n * Normalize a flow's raw `schedule` descriptor into a {@link JobSchedule}, or\n * `null` if it can't be understood. Accepts the canonical\n * `{ type: 'cron'|'interval'|'once', ... }` shape plus a few ergonomic\n * shorthands (a bare cron string, `{ cron }`, `{ expression }`, `{ every }` /\n * `{ intervalMs }`, `{ at }`).\n */\nexport function normalizeSchedule(raw: unknown): JobSchedule | null {\n if (raw == null) return null;\n\n // Bare cron string, e.g. '0 1 * * *'.\n if (typeof raw === 'string') {\n const expr = raw.trim();\n return expr ? { type: 'cron', expression: expr } : null;\n }\n\n if (typeof raw !== 'object') return null;\n const s = raw as Record<string, unknown>;\n\n const type = typeof s.type === 'string' ? s.type : undefined;\n\n if (type === 'cron' || (!type && (typeof s.cron === 'string' || typeof s.expression === 'string'))) {\n const expression =\n (typeof s.expression === 'string' && s.expression) ||\n (typeof s.cron === 'string' && s.cron) ||\n undefined;\n if (!expression) return null;\n const out: JobSchedule = { type: 'cron', expression };\n if (typeof s.timezone === 'string') out.timezone = s.timezone;\n return out;\n }\n\n if (type === 'interval' || (!type && (typeof s.intervalMs === 'number' || typeof s.every === 'number'))) {\n const intervalMs =\n (typeof s.intervalMs === 'number' && s.intervalMs) ||\n (typeof s.every === 'number' && s.every) ||\n undefined;\n if (!intervalMs || intervalMs <= 0) return null;\n return { type: 'interval', intervalMs };\n }\n\n if (type === 'once' || (!type && typeof s.at === 'string')) {\n const at = typeof s.at === 'string' ? s.at : undefined;\n if (!at) return null;\n return { type: 'once', at };\n }\n\n return null;\n}\n\n/**\n * ScheduleTrigger\n *\n * Bridges the automation engine's {@link FlowTrigger} extension point to the\n * platform {@link JobServiceSurface}. For each schedule-triggered flow the\n * engine activates, it registers a job whose handler runs the flow; the job\n * service owns the actual cron/interval/once timing (so this trigger stays\n * adapter-agnostic — cron schedules need a cron-capable adapter, which the\n * job service selects).\n *\n * The job service is resolved lazily (per `start()`) via the supplied accessor,\n * so we always pick up the job service's *upgraded* adapter (e.g. the durable\n * DbJobAdapter that replaces the bootstrap interval adapter on `kernel:ready`).\n */\nexport class ScheduleTrigger implements FlowTrigger {\n readonly type = 'schedule';\n\n private readonly getJobService: () => JobServiceSurface | null;\n private readonly logger: TriggerLogger;\n /** flowName → job name registered for it, so stop() can cancel it. */\n private readonly bound = new Map<string, string>();\n\n constructor(getJobService: () => JobServiceSurface | null, logger: TriggerLogger) {\n this.getJobService = getJobService;\n this.logger = logger;\n }\n\n start(binding: FlowTriggerBinding, callback: (ctx: AutomationContext) => Promise<void>): void {\n const raw = binding.schedule ?? (binding.config as Record<string, unknown> | undefined)?.schedule;\n const schedule = normalizeSchedule(raw);\n if (!schedule) {\n this.logger.warn(\n `[schedule] flow '${binding.flowName}' has no recognizable schedule descriptor — not bound`,\n );\n return;\n }\n\n const jobService = this.getJobService();\n if (!jobService || typeof jobService.schedule !== 'function') {\n this.logger.warn(\n `[schedule] job service unavailable — flow '${binding.flowName}' not scheduled`,\n );\n return;\n }\n\n // Idempotent: drop any prior schedule for this flow before re-binding\n // (covers disable→enable cycles and hot reload).\n this.stop(binding.flowName);\n\n const jobName = `${JOB_PREFIX}:${binding.flowName}`;\n\n const handler: JobHandler = async ({ jobId }) => {\n try {\n const ctx: AutomationContext = {\n event: 'schedule',\n params: {\n jobId,\n flowName: binding.flowName,\n schedule,\n },\n };\n await callback(ctx);\n } catch (err) {\n // Error isolation: a scheduled flow failure must not crash the\n // job runner / ticker. Log and swallow.\n this.logger.warn(\n `[schedule] flow '${binding.flowName}' execution failed: ${(err as Error)?.message ?? String(err)}`,\n );\n }\n };\n\n this.bound.set(binding.flowName, jobName);\n // FlowTrigger.start is sync; the job service's schedule() is async.\n // Fire-and-forget with error logging.\n void Promise.resolve(jobService.schedule(jobName, schedule, handler))\n .then(() => {\n this.logger.info(\n `[schedule] bound flow '${binding.flowName}' → ${schedule.type}` +\n (schedule.expression ? ` '${schedule.expression}'` : '') +\n (schedule.intervalMs ? ` every ${schedule.intervalMs}ms` : '') +\n (schedule.at ? ` at ${schedule.at}` : ''),\n );\n })\n .catch((err) => {\n this.bound.delete(binding.flowName);\n reportBindFailure(this.logger, 'schedule', binding.flowName, err);\n });\n }\n\n stop(flowName: string): void {\n const jobName = this.bound.get(flowName);\n if (!jobName) return;\n this.bound.delete(flowName);\n const jobService = this.getJobService();\n if (!jobService || typeof jobService.cancel !== 'function') return;\n void Promise.resolve(jobService.cancel(jobName))\n .then(() => this.logger.debug?.(`[schedule] unbound flow '${flowName}'`))\n .catch((err) => {\n this.logger.warn(\n `[schedule] failed to unbind flow '${flowName}': ${(err as Error)?.message ?? String(err)}`,\n );\n });\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Plugin, PluginContext } from '@objectstack/core';\nimport { ScheduleTrigger } from './schedule-trigger.js';\nimport type { FlowTrigger, JobServiceSurface } from './schedule-trigger.js';\n\n/**\n * The slice of the automation engine this plugin needs: register a trigger on\n * its `FlowTrigger` extension point. Declared structurally so the plugin does\n * not take a build dependency on `@objectstack/service-automation`.\n */\ninterface AutomationTriggerRegistry {\n registerTrigger(trigger: FlowTrigger): void;\n unregisterTrigger?(type: string): void;\n}\n\n/**\n * ScheduleTriggerPlugin\n *\n * Makes schedule-triggered flows actually fire. The automation engine ships the\n * `FlowTrigger` wiring (it parses each flow's start node — `flow.type ===\n * 'schedule'` or a start-node `config.schedule` descriptor — into a binding and\n * calls `trigger.start(...)`), but the *concrete* schedule trigger lives here as\n * a plugin and delegates timing to the platform `IJobService` (the `'job'`\n * service). This mirrors the connector / record-change split (engine baseline +\n * trigger plugin).\n *\n * With this plugin (and a job service) installed, a flow whose start node\n * declares `config: { schedule: { type: 'cron', expression: '0 1 * * *' } }`\n * auto-launches on that schedule — no manual `engine.execute()`.\n *\n * Depends on the job service plugin so its `kernel:ready` upgrade (to the\n * durable DbJobAdapter) runs before ours; the job service is nonetheless\n * resolved lazily per `start()` so we always use its current adapter.\n */\nexport class ScheduleTriggerPlugin implements Plugin {\n name = 'com.objectstack.trigger.schedule';\n type = 'standard';\n version = '7.3.0';\n dependencies = ['com.objectstack.service.job'];\n\n async init(ctx: PluginContext): Promise<void> {\n ctx.logger.info('Schedule trigger plugin initialized');\n }\n\n async start(ctx: PluginContext): Promise<void> {\n // The automation service + job service are resolvable once the kernel is\n // ready (kernel:ready fires after AutomationServicePlugin.start() has\n // pulled flows in and after the job service upgrades its adapter).\n ctx.hook('kernel:ready', async () => {\n const automation = this.resolveService<AutomationTriggerRegistry>(ctx, 'automation');\n if (!automation || typeof automation.registerTrigger !== 'function') {\n ctx.logger.warn(\n 'ScheduleTriggerPlugin: automation service not available — schedule trigger NOT installed',\n );\n return;\n }\n\n // Probe once for a clear startup warning; the trigger re-resolves\n // lazily on each start() so adapter upgrades are always picked up.\n if (!this.resolveService<JobServiceSurface>(ctx, 'job')) {\n ctx.logger.warn(\n 'ScheduleTriggerPlugin: job service not available — scheduled flows will not run until one is registered',\n );\n }\n\n const trigger = new ScheduleTrigger(\n () => this.resolveService<JobServiceSurface>(ctx, 'job'),\n ctx.logger,\n );\n automation.registerTrigger(trigger);\n ctx.logger.info('ScheduleTriggerPlugin: schedule trigger registered');\n });\n }\n\n private resolveService<T>(ctx: PluginContext, name: string): T | null {\n try {\n return ctx.getService<T>(name) ?? null;\n } catch {\n return null;\n }\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { AutomationContext, JobSchedule, JobHandler } from '@objectstack/spec/contracts';\nimport {\n TimeRelativeTriggerSchema,\n TIME_RELATIVE_DEFAULT_CRON,\n TIME_RELATIVE_DEFAULT_MAX_RECORDS,\n} from '@objectstack/spec/automation';\nimport type { TimeRelativeTrigger as TimeRelativeDescriptor } from '@objectstack/spec/automation';\nimport { normalizeSchedule, reportBindFailure } from './schedule-trigger.js';\nimport type { FlowTrigger, FlowTriggerBinding, JobServiceSurface, TriggerLogger } from './schedule-trigger.js';\n\n/**\n * The slice of the ObjectQL data engine this trigger needs: run a filtered\n * `find` (to discover the records whose date field falls in the window) and,\n * optionally, probe whether an object is registered. Typed structurally — same\n * decoupling pattern the record-change trigger uses for its hook surface — so\n * this plugin does not take a build dependency on the engine package.\n */\nexport interface TimeRelativeDataEngine {\n find(\n objectName: string,\n query?: {\n where?: Record<string, unknown>;\n fields?: string[];\n limit?: number;\n /** Elevated context — a background sweep must see all rows, not RLS-scoped ones. */\n context?: { isSystem?: boolean };\n },\n ): Promise<Array<Record<string, unknown>> | undefined>;\n /**\n * Optional object-existence probe (the ObjectQL engine's `getObject`).\n * When present, {@link TimeRelativeTrigger.start} uses it to call out a\n * descriptor whose `object` matches no registered object at bind time —\n * otherwise the sweep just quietly finds nothing forever.\n */\n getObject?(name: string): unknown;\n}\n\n/**\n * The slice of the automation service this trigger needs for dispatch\n * idempotency (#10220): claim a dispatch key against the persisted\n * `sys_flow_dispatch` ledger. `true` = this caller owns the dispatch; `false` =\n * an earlier sweep (possibly in a previous process lifetime) already made it.\n * Typed structurally — like {@link TimeRelativeDataEngine} — so this plugin\n * never learns the ledger's table name and takes no build dependency on\n * `@objectstack/service-automation`.\n */\nexport interface FlowDispatchClaimSurface {\n claim(key: string): Promise<boolean>;\n}\n\n/** Job-name namespace so time-relative sweeps never collide with plain schedule jobs. */\nconst JOB_PREFIX = 'flow-time-relative';\n\nconst MS_PER_DAY = 86_400_000;\n\n/**\n * TTL for the trigger's IN-PROCESS claim fallback (#10220): every dispatch key\n * embeds a calendar day, so no key is producible more than ~48h after it was\n * first claimable — pruning at that age keeps the fallback map bounded without\n * ever forgetting a key a sweep could still produce.\n */\nconst LOCAL_CLAIM_TTL_MS = 48 * 60 * 60 * 1000;\n\n/** A closed, inclusive instant window `[gte, lte]` as ISO-8601 strings. */\nexport interface DateWindow {\n /** Lower bound (inclusive), ISO-8601. */\n gte: string;\n /** Upper bound (inclusive), ISO-8601. */\n lte: string;\n}\n\n// ─── Pure window math (day-granular, UTC) ───────────────────────────\n\n/** Start of `d`'s UTC calendar day (00:00:00.000Z). */\nfunction startOfUtcDay(d: Date): Date {\n return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), 0, 0, 0, 0));\n}\n\n/** End of `d`'s UTC calendar day (23:59:59.999Z) — inclusive upper bound. */\nfunction endOfUtcDay(d: Date): Date {\n return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), 23, 59, 59, 999));\n}\n\n/** `d`'s UTC day shifted by `n` whole days (exact in UTC — no DST drift). */\nfunction addUtcDays(d: Date, n: number): Date {\n return new Date(startOfUtcDay(d).getTime() + n * MS_PER_DAY);\n}\n\n/**\n * Compute the inclusive date window(s) a descriptor selects, relative to `now`.\n *\n * - `offsetDays` → one single-day window per offset (`today + offset`), so the\n * sweep fires exactly on each threshold day (the robust T-minus reminder).\n * - `withinDays` → one range window: `[today, today + N]` when N ≥ 0 (upcoming),\n * or `[today − |N|, today]` when N < 0 (overdue lookback). Always includes today.\n *\n * Day-granular and computed in UTC. The upper bound is the *end* of its day\n * (`23:59:59.999Z`), so a `datetime` field matches for the whole day and a\n * `date` field (compared as `YYYY-MM-DD` after the driver truncates) is inclusive.\n */\nexport function computeDateWindows(desc: TimeRelativeDescriptor, now: Date): DateWindow[] {\n return computeWindowClaimScopes(desc, now).map((s) => s.window);\n}\n\n/**\n * A date window paired with the **claim scope** naming its identity for the\n * dispatch dedup key (#10220, maintainer ruling 2026-08-20).\n */\nexport interface WindowClaimScope {\n window: DateWindow;\n /**\n * Window-identity fragment of the dispatch key — what makes a re-scan of\n * the SAME window dedup while a genuinely new window fires again:\n *\n * - offset mode → `<windowDay>:offset<n>`: the window day is the date the\n * record's field must fall on, so editing the field to a new day (or a\n * different offset matching) yields a new key and legitimately re-fires.\n * Re-scans of one window all derive the same day → deduped.\n * - range mode → `<sweepDay>:within<n>`: keyed on the SWEEP day, not the\n * (constant) field value, so the documented `withinDays` semantic —\n * \"fires every day the record stays in range\" — remains true: each new\n * day is a new key, but never twice in one day.\n */\n scope: string;\n}\n\n/**\n * {@link computeDateWindows}, with each window's claim scope (#10220). One\n * derivation for both so the matching rule and the dedup key can never drift.\n */\nexport function computeWindowClaimScopes(desc: TimeRelativeDescriptor, now: Date): WindowClaimScope[] {\n const today = startOfUtcDay(now);\n\n if (desc.offsetDays && desc.offsetDays.length > 0) {\n return desc.offsetDays.map((offset) => {\n const day = addUtcDays(today, offset);\n const window = { gte: startOfUtcDay(day).toISOString(), lte: endOfUtcDay(day).toISOString() };\n return { window, scope: `${window.gte.slice(0, 10)}:offset${offset}` };\n });\n }\n\n const n = desc.withinDays ?? 0;\n const sweepDay = today.toISOString().slice(0, 10);\n const window: DateWindow =\n n >= 0\n ? { gte: startOfUtcDay(today).toISOString(), lte: endOfUtcDay(addUtcDays(today, n)).toISOString() }\n // Negative: window extends into the past, still anchored to (and including) today.\n : { gte: startOfUtcDay(addUtcDays(today, n)).toISOString(), lte: endOfUtcDay(today).toISOString() };\n return [{ window, scope: `${sweepDay}:within${n}` }];\n}\n\n/**\n * Build the ObjectQL `where` map for one date window: the descriptor's static\n * `filter` (if any) ANDed with a `$gte`/`$lte` range on the date field. The map\n * form is the canonical filter shape both drivers evaluate verbatim (the same\n * shape the platform's own retention sweep uses).\n */\nexport function buildWindowWhere(desc: TimeRelativeDescriptor, window: DateWindow): Record<string, unknown> {\n return {\n ...(desc.filter ?? {}),\n [desc.dateField]: { $gte: window.gte, $lte: window.lte },\n };\n}\n\nfunction errMessage(err: unknown): string {\n return (err as Error)?.message ?? String(err);\n}\n\n/**\n * TimeRelativeTrigger\n *\n * The declarative answer to \"act on records whose date field is coming up (or\n * overdue)\" (#1874). Instead of the fragile date-equality-on-record-change\n * pattern (which only fires if the record happens to be edited on the threshold\n * day) or a hand-rolled cron + range query per flow, a flow whose start node\n * declares `config.timeRelative` is swept on a schedule (daily by default) and\n * launched **once per matching record**.\n *\n * It composes the schedule trigger's two collaborators:\n * - the platform {@link JobServiceSurface} owns the sweep cadence (like the\n * plain schedule trigger), and\n * - the {@link TimeRelativeDataEngine} runs the date-window query (like the\n * record-change trigger reaching ObjectQL).\n *\n * Both are resolved lazily (per call) so adapter upgrades — the durable job\n * adapter that replaces the bootstrap ticker on `kernel:ready`, a late-registered\n * data engine — are always picked up. The engine owns the start-node `condition`\n * gate and `runAs` identity, so this trigger only has to put the matched record\n * on the {@link AutomationContext}; `{record.<field>}` interpolation and the\n * condition work exactly as they do for a record-change flow.\n */\nexport class TimeRelativeTrigger implements FlowTrigger {\n readonly type = 'time_relative';\n\n private readonly getJobService: () => JobServiceSurface | null;\n private readonly getDataEngine: () => TimeRelativeDataEngine | null;\n private readonly logger: TriggerLogger;\n /** Injectable clock so window math is deterministic under test. */\n private readonly now: () => Date;\n /** flowName → job name registered for it, so stop() can cancel it. */\n private readonly bound = new Map<string, string>();\n /** Dispatch-idempotency claim surface (#10220), resolved lazily per sweep. */\n private readonly getClaimSurface: () => FlowDispatchClaimSurface | null;\n /**\n * In-process claim fallback when no claim surface resolves (#10220):\n * key → claim time (epoch ms), TTL-pruned. Dedups re-scans within THIS\n * process only — which is why falling to it is warned once, below.\n */\n private readonly localClaims = new Map<string, number>();\n /** Whether the in-process-only dedup degradation has been said (once). */\n private claimDegradationWarned = false;\n\n constructor(\n getJobService: () => JobServiceSurface | null,\n getDataEngine: () => TimeRelativeDataEngine | null,\n logger: TriggerLogger,\n now: () => Date = () => new Date(),\n getClaimSurface: () => FlowDispatchClaimSurface | null = () => null,\n ) {\n this.getJobService = getJobService;\n this.getDataEngine = getDataEngine;\n this.logger = logger;\n this.now = now;\n this.getClaimSurface = getClaimSurface;\n }\n\n start(binding: FlowTriggerBinding, callback: (ctx: AutomationContext) => Promise<void>): void {\n const raw = (binding.config as Record<string, unknown> | undefined)?.timeRelative;\n const parsed = TimeRelativeTriggerSchema.safeParse(raw);\n if (!parsed.success) {\n this.logger.warn(\n `[time-relative] flow '${binding.flowName}' has no valid \\`timeRelative\\` descriptor — not bound. ` +\n `Provide { object, dateField, and exactly one of withinDays | offsetDays }. ` +\n `(${parsed.error.issues.map((i) => `${i.path.join('.') || '(root)'}: ${i.message}`).join('; ')})`,\n );\n return;\n }\n const desc = parsed.data;\n\n // Cadence: the flow's start-node schedule descriptor, or a daily default.\n // A daily sweep is the whole point (evaluate the window every day so a\n // threshold day is never missed), so an omitted schedule means \"daily\",\n // not \"never\".\n const schedule: JobSchedule =\n normalizeSchedule(binding.schedule) ?? { type: 'cron', expression: TIME_RELATIVE_DEFAULT_CRON };\n\n const jobService = this.getJobService();\n if (!jobService || typeof jobService.schedule !== 'function') {\n this.logger.warn(\n `[time-relative] job service unavailable — flow '${binding.flowName}' not scheduled`,\n );\n return;\n }\n\n // Best-effort object-existence probe at bind time (the engine may be\n // available now even though the sweep resolves it lazily). A descriptor\n // targeting an unknown object would sweep forever finding nothing.\n const engineNow = this.getDataEngine();\n if (desc.object && engineNow && typeof engineNow.getObject === 'function') {\n let known: unknown;\n try {\n known = engineNow.getObject(desc.object);\n } catch {\n known = undefined;\n }\n if (!known) {\n this.logger.warn(\n `[time-relative] flow '${binding.flowName}' targets unknown object '${desc.object}' — the sweep is bound but will match nothing until that object is registered. ` +\n `Object names match exactly; check config.timeRelative.object.`,\n );\n }\n }\n\n // Idempotent: drop any prior schedule for this flow before re-binding\n // (covers disable→enable cycles and hot reload).\n this.stop(binding.flowName);\n\n const jobName = `${JOB_PREFIX}:${binding.flowName}`;\n const maxRecords = desc.maxRecords ?? TIME_RELATIVE_DEFAULT_MAX_RECORDS;\n\n const handler: JobHandler = async () => {\n try {\n await this.sweep(binding.flowName, desc, maxRecords, callback);\n } catch (err) {\n // Error isolation: a sweep failure must not crash the job\n // runner / ticker. Log and swallow.\n this.logger.warn(\n `[time-relative] flow '${binding.flowName}' sweep failed: ${errMessage(err)}`,\n );\n }\n };\n\n this.bound.set(binding.flowName, jobName);\n // FlowTrigger.start is sync; the job service's schedule() is async.\n // Fire-and-forget with error logging (mirrors ScheduleTrigger).\n void Promise.resolve(jobService.schedule(jobName, schedule, handler))\n .then(() => {\n const mode = desc.offsetDays\n ? `offsets [${desc.offsetDays.join(', ')}]d`\n : `within ${desc.withinDays}d`;\n this.logger.info(\n `[time-relative] bound flow '${binding.flowName}' → sweep '${desc.object}.${desc.dateField}' ${mode} on ${schedule.type}` +\n (schedule.expression ? ` '${schedule.expression}'` : '') +\n (schedule.intervalMs ? ` every ${schedule.intervalMs}ms` : ''),\n );\n })\n .catch((err) => {\n this.bound.delete(binding.flowName);\n reportBindFailure(this.logger, 'time-relative', binding.flowName, err);\n });\n }\n\n /**\n * Run one sweep: query each date window, union the matched records (deduped\n * by id, capped at `maxRecords`), and launch the flow once per record. A\n * per-record failure is isolated so one bad row never aborts the batch.\n */\n private async sweep(\n flowName: string,\n desc: TimeRelativeDescriptor,\n maxRecords: number,\n callback: (ctx: AutomationContext) => Promise<void>,\n ): Promise<void> {\n const engine = this.getDataEngine();\n if (!engine || typeof engine.find !== 'function') {\n this.logger.warn(\n `[time-relative] data engine unavailable — flow '${flowName}' sweep skipped this tick`,\n );\n return;\n }\n\n const scopes = computeWindowClaimScopes(desc, this.now());\n const seenIds = new Set<unknown>();\n const matched: Array<{ record: Record<string, unknown>; claimKey: string | null }> = [];\n\n for (const { window, scope } of scopes) {\n if (matched.length >= maxRecords) break;\n const where = buildWindowWhere(desc, window);\n const rows =\n (await engine.find(desc.object, {\n where,\n limit: maxRecords,\n context: { isSystem: true },\n })) ?? [];\n for (const row of rows) {\n const id = (row as { id?: unknown }).id;\n // Dedup across windows (offset mode) by id; rows without an id\n // are always kept (can't dedup, better than dropping).\n if (id != null) {\n if (seenIds.has(id)) continue;\n seenIds.add(id);\n }\n // #10220 — dispatch key: the MATCHED WINDOW's identity + the\n // record. A row without an id can't be keyed; it is dispatched\n // unconditionally, exactly as it was never dedupable before.\n const claimKey = id != null ? `time-relative:${flowName}:${scope}:${String(id)}` : null;\n matched.push({ record: row, claimKey });\n if (matched.length >= maxRecords) break;\n }\n }\n\n if (matched.length >= maxRecords) {\n this.logger.warn(\n `[time-relative] flow '${flowName}' sweep hit the ${maxRecords}-record cap — some matching records were NOT processed this tick. ` +\n `Narrow the window/filter, or raise config.timeRelative.maxRecords.`,\n );\n }\n\n let launched = 0;\n let failed = 0;\n let deduped = 0;\n for (const { record, claimKey } of matched) {\n // #10220 — idempotency gate: launch only if this (flow, record,\n // window) key has not been dispatched before. A re-scan of the same\n // window (denser schedule, kernel rebuild + persisted ledger,\n // future catch-up sweep) skips instead of re-minting.\n if (claimKey != null && !(await this.claimDispatch(flowName, claimKey))) {\n deduped++;\n continue;\n }\n try {\n const ctx: AutomationContext = {\n record,\n object: desc.object,\n event: 'time_relative',\n // Expose the record as params too, so flows with named `isInput`\n // variables matching record fields get them seeded (parity with\n // the record-change trigger).\n params: record,\n };\n await callback(ctx);\n launched++;\n } catch (err) {\n failed++;\n // Error isolation per record: one failing flow run must not stop\n // the sweep. ERROR when available (stderr survives the CLI's\n // boot-quiet stdout window), else warn.\n const log = this.logger.error?.bind(this.logger) ?? this.logger.warn.bind(this.logger);\n log(\n `[time-relative] flow '${flowName}' failed for record '${String((record as { id?: unknown }).id ?? '?')}': ${errMessage(err)}`,\n );\n }\n }\n\n this.logger.debug?.(\n `[time-relative] flow '${flowName}' swept '${desc.object}': ${matched.length} matched, ${launched} launched, ${deduped} already dispatched, ${failed} failed`,\n );\n }\n\n /**\n * Claim one dispatch key (#10220): `true` = launch, `false` = an earlier\n * sweep already dispatched this (flow, record, window).\n *\n * Degradation contract:\n * - Claim surface resolves (the automation service's `claim()`, backed by\n * the persisted `sys_flow_dispatch` ledger) → its answer is used; if the\n * CALL throws, the failure is logged and the dispatch proceeds —\n * availability over strict-once: a broken ledger must never silently\n * swallow reminders.\n * - No claim surface (automation service missing, or one predating\n * `claim()`) → in-process dedup only, warned ONCE: a silent fallback\n * would hide that the once-per-window guarantee no longer survives a\n * kernel rebuild.\n */\n private async claimDispatch(flowName: string, key: string): Promise<boolean> {\n const surface = this.getClaimSurface();\n if (surface && typeof surface.claim === 'function') {\n try {\n return await surface.claim(key);\n } catch (err) {\n this.logger.warn(\n `[time-relative] flow '${flowName}' dispatch-claim failed for key '${key}' — dispatching anyway ` +\n `(availability over strict-once; the same window may re-fire until the claim store recovers): ${errMessage(err)}`,\n );\n return true;\n }\n }\n if (!this.claimDegradationWarned) {\n this.claimDegradationWarned = true;\n this.logger.warn(\n `[time-relative] no dispatch-claim surface (automation service missing or without claim()) — ` +\n `sweep dedup is IN-PROCESS ONLY and will NOT survive a kernel rebuild: ` +\n `the same record/window can re-fire after a restart.`,\n );\n }\n const now = this.now().getTime();\n const cutoff = now - LOCAL_CLAIM_TTL_MS;\n for (const [k, t] of this.localClaims) {\n if (t < cutoff) this.localClaims.delete(k);\n }\n if (this.localClaims.has(key)) return false;\n this.localClaims.set(key, now);\n return true;\n }\n\n stop(flowName: string): void {\n const jobName = this.bound.get(flowName);\n if (!jobName) return;\n this.bound.delete(flowName);\n const jobService = this.getJobService();\n if (!jobService || typeof jobService.cancel !== 'function') return;\n void Promise.resolve(jobService.cancel(jobName))\n .then(() => this.logger.debug?.(`[time-relative] unbound flow '${flowName}'`))\n .catch((err) => {\n this.logger.warn(\n `[time-relative] failed to unbind flow '${flowName}': ${errMessage(err)}`,\n );\n });\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Plugin, PluginContext } from '@objectstack/core';\nimport { TimeRelativeTrigger } from './time-relative-trigger.js';\nimport type { FlowDispatchClaimSurface, TimeRelativeDataEngine } from './time-relative-trigger.js';\nimport type { FlowTrigger, JobServiceSurface } from './schedule-trigger.js';\n\n/**\n * The slice of the automation engine this plugin needs: register a trigger on\n * its `FlowTrigger` extension point. Declared structurally so the plugin does\n * not take a build dependency on `@objectstack/service-automation`.\n */\ninterface AutomationTriggerRegistry {\n registerTrigger(trigger: FlowTrigger): void;\n unregisterTrigger?(type: string): void;\n}\n\n/**\n * TimeRelativeTriggerPlugin\n *\n * Arms **declarative time-relative flows** (#1874): a flow whose start node\n * declares `config.timeRelative` (object + dateField + `withinDays`/`offsetDays`)\n * is swept on a schedule and launched once per record whose date field falls in\n * the window — no hand-written cron + range query, no fragile\n * date-equality-on-record-change.\n *\n * It ships in `@objectstack/trigger-schedule` alongside the plain schedule\n * trigger (both are schedule-driven) but is a **separate** plugin: the\n * time-relative trigger additionally needs the ObjectQL engine (for the sweep\n * query), so keeping it separate leaves the plain `ScheduleTriggerPlugin`'s\n * dependency surface unchanged. Depends on the job service (sweep cadence) and\n * the ObjectQL engine (record discovery); both are resolved lazily per `start()`\n * so adapter upgrades are always picked up.\n */\nexport class TimeRelativeTriggerPlugin implements Plugin {\n name = 'com.objectstack.trigger.time-relative';\n type = 'standard';\n version = '1.0.0';\n dependencies = ['com.objectstack.service.job', 'com.objectstack.engine.objectql'];\n\n async init(ctx: PluginContext): Promise<void> {\n ctx.logger.info('Time-relative trigger plugin initialized');\n }\n\n async start(ctx: PluginContext): Promise<void> {\n // The automation service, job service, and ObjectQL engine are all\n // resolvable once the kernel is ready (kernel:ready fires after\n // AutomationServicePlugin.start() has pulled flows in and after the job\n // service upgrades its adapter).\n ctx.hook('kernel:ready', async () => {\n const automation = this.resolveService<AutomationTriggerRegistry>(ctx, 'automation');\n if (!automation || typeof automation.registerTrigger !== 'function') {\n ctx.logger.warn(\n 'TimeRelativeTriggerPlugin: automation service not available — time-relative trigger NOT installed',\n );\n return;\n }\n\n // Probe once for a clear startup warning; the trigger re-resolves\n // both collaborators lazily on each start()/sweep so late upgrades\n // are always picked up.\n if (!this.resolveService<JobServiceSurface>(ctx, 'job')) {\n ctx.logger.warn(\n 'TimeRelativeTriggerPlugin: job service not available — time-relative sweeps will not run until one is registered',\n );\n }\n if (!this.resolveDataEngine(ctx)) {\n ctx.logger.warn(\n 'TimeRelativeTriggerPlugin: ObjectQL engine not available — time-relative sweeps will find no records until it is',\n );\n }\n\n const trigger = new TimeRelativeTrigger(\n () => this.resolveService<JobServiceSurface>(ctx, 'job'),\n () => this.resolveDataEngine(ctx),\n ctx.logger,\n undefined, // default wall clock\n // #10220 — dispatch-idempotency claims go through the SAME\n // automation service this plugin already resolves; the trigger\n // computes the key and never learns the ledger's table name. An\n // automation service predating claim() resolves to null and the\n // trigger degrades (honestly, warned once) to in-process dedup.\n () => {\n const svc = this.resolveService<Partial<FlowDispatchClaimSurface>>(ctx, 'automation');\n return svc && typeof svc.claim === 'function' ? (svc as FlowDispatchClaimSurface) : null;\n },\n );\n automation.registerTrigger(trigger);\n ctx.logger.info('TimeRelativeTriggerPlugin: time-relative trigger registered');\n });\n }\n\n private resolveService<T>(ctx: PluginContext, name: string): T | null {\n try {\n return ctx.getService<T>(name) ?? null;\n } catch {\n return null;\n }\n }\n\n private resolveDataEngine(ctx: PluginContext): TimeRelativeDataEngine | null {\n // Primary alias 'objectql', fallback 'data' (some kernels register the\n // engine under both) — same lookup the record-change trigger uses.\n return (\n this.resolveService<TimeRelativeDataEngine>(ctx, 'objectql') ??\n this.resolveService<TimeRelativeDataEngine>(ctx, 'data')\n );\n }\n}\n"],"mappings":";AA0DA,IAAM,aAAa;AAuBZ,SAAS,kBACZ,QACA,KACA,UACA,KACI;AACJ,QAAM,SAAS,OAAO,OAAO,KAAK,MAAM,KAAK,OAAO,KAAK,KAAK,MAAM;AACpE;AAAA,IACI,I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window"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@objectstack/trigger-schedule",
|
|
3
|
-
"version": "17.
|
|
3
|
+
"version": "17.2.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "Schedule flow trigger for ObjectStack — auto-launches flows on a cron/interval/once schedule via the IJobService (ADR-0018)",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -13,15 +13,15 @@
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@objectstack/core": "17.
|
|
17
|
-
"@objectstack/spec": "17.
|
|
16
|
+
"@objectstack/core": "17.2.0",
|
|
17
|
+
"@objectstack/spec": "17.2.0"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@types/node": "^26.
|
|
20
|
+
"@types/node": "^26.2.0",
|
|
21
21
|
"croner": "^10.0.1",
|
|
22
22
|
"typescript": "^6.0.3",
|
|
23
23
|
"vitest": "^4.1.10",
|
|
24
|
-
"@objectstack/service-automation": "17.
|
|
24
|
+
"@objectstack/service-automation": "17.2.0"
|
|
25
25
|
},
|
|
26
26
|
"keywords": [
|
|
27
27
|
"objectstack",
|