|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 4 | +import { ScheduleTrigger } from './schedule-trigger.js'; |
| 5 | +import type { FlowTrigger, JobServiceSurface } from './schedule-trigger.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * The slice of the automation engine this plugin needs: register a trigger on |
| 9 | + * its `FlowTrigger` extension point. Declared structurally so the plugin does |
| 10 | + * not take a build dependency on `@objectstack/service-automation`. |
| 11 | + */ |
| 12 | +interface AutomationTriggerRegistry { |
| 13 | + registerTrigger(trigger: FlowTrigger): void; |
| 14 | + unregisterTrigger?(type: string): void; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * ScheduleTriggerPlugin |
| 19 | + * |
| 20 | + * Makes schedule-triggered flows actually fire. The automation engine ships the |
| 21 | + * `FlowTrigger` wiring (it parses each flow's start node — `flow.type === |
| 22 | + * 'schedule'` or a start-node `config.schedule` descriptor — into a binding and |
| 23 | + * calls `trigger.start(...)`), but the *concrete* schedule trigger lives here as |
| 24 | + * a plugin and delegates timing to the platform `IJobService` (the `'job'` |
| 25 | + * service). This mirrors the connector / record-change split (engine baseline + |
| 26 | + * trigger plugin). |
| 27 | + * |
| 28 | + * With this plugin (and a job service) installed, a flow whose start node |
| 29 | + * declares `config: { schedule: { type: 'cron', expression: '0 1 * * *' } }` |
| 30 | + * auto-launches on that schedule — no manual `engine.execute()`. |
| 31 | + * |
| 32 | + * Depends on the job service plugin so its `kernel:ready` upgrade (to the |
| 33 | + * durable DbJobAdapter) runs before ours; the job service is nonetheless |
| 34 | + * resolved lazily per `start()` so we always use its current adapter. |
| 35 | + */ |
| 36 | +export class ScheduleTriggerPlugin implements Plugin { |
| 37 | + name = 'com.objectstack.trigger.schedule'; |
| 38 | + type = 'standard'; |
| 39 | + version = '7.3.0'; |
| 40 | + dependencies = ['com.objectstack.service.job']; |
| 41 | + |
| 42 | + async init(ctx: PluginContext): Promise<void> { |
| 43 | + ctx.logger.info('Schedule trigger plugin initialized'); |
| 44 | + } |
| 45 | + |
| 46 | + async start(ctx: PluginContext): Promise<void> { |
| 47 | + // The automation service + job service are resolvable once the kernel is |
| 48 | + // ready (kernel:ready fires after AutomationServicePlugin.start() has |
| 49 | + // pulled flows in and after the job service upgrades its adapter). |
| 50 | + ctx.hook('kernel:ready', async () => { |
| 51 | + const automation = this.resolveService<AutomationTriggerRegistry>(ctx, 'automation'); |
| 52 | + if (!automation || typeof automation.registerTrigger !== 'function') { |
| 53 | + ctx.logger.warn( |
| 54 | + 'ScheduleTriggerPlugin: automation service not available — schedule trigger NOT installed', |
| 55 | + ); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Probe once for a clear startup warning; the trigger re-resolves |
| 60 | + // lazily on each start() so adapter upgrades are always picked up. |
| 61 | + if (!this.resolveService<JobServiceSurface>(ctx, 'job')) { |
| 62 | + ctx.logger.warn( |
| 63 | + 'ScheduleTriggerPlugin: job service not available — scheduled flows will not run until one is registered', |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + const trigger = new ScheduleTrigger( |
| 68 | + () => this.resolveService<JobServiceSurface>(ctx, 'job'), |
| 69 | + ctx.logger, |
| 70 | + ); |
| 71 | + automation.registerTrigger(trigger); |
| 72 | + ctx.logger.info('ScheduleTriggerPlugin: schedule trigger registered'); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + private resolveService<T>(ctx: PluginContext, name: string): T | null { |
| 77 | + try { |
| 78 | + return ctx.getService<T>(name) ?? null; |
| 79 | + } catch { |
| 80 | + return null; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments