Scheduling and events
Scheduling and events connect time-based automation with the server state that users can see. A schedule starts a template task or workflow at a future time, while events record important changes and provide a history for users and integrations. The server exposes both stored event history and live task messages through separate interfaces.
Scheduled automation
Schedule records
A schedule is a project resource that links either a task template or a workflow to a repeat rule or one future time. The record also carries whether the schedule is active, optional task parameters for template runs, and optional repository state used to start work when source content changes. The shared model is defined in db/Schedule.go.
Schedules have two time modes:
- Cron schedules repeat according to a cron expression.
- Run-at schedules fire once at a future time and then become inactive or are deleted when configured to do so.
Each schedule has exactly one target. A template schedule can use repository commit checks, while a workflow schedule starts the workflow with its survey defaults and cannot use a repository check. The schedule API validates these relationships before it writes them. A schedule change refreshes the active scheduler so the new state takes effect without waiting for a process restart; see api/…/schedules.go.
References: db/Schedule.go• L14-38 db/Schedule.go• L49-71 api/…/schedules.go• L73-115 api/…/schedules.go• L127-164
Schedule dispatch
The scheduler loads eligible records from storage and turns each one into a timed runner. At the scheduled time, the runner checks the current schedule and its target, then starts either a task from a template or a workflow run. The task pool or workflow service takes over after that point, so scheduling decides when work starts while the selected execution system decides how it runs; see services/…/SchedulePool.go and services/…/TaskPool.go.
This flow keeps schedule state in the database and timed state in the scheduler. A refresh replaces the in-memory timed entries with the current stored records. The scheduler also uses the configured schedule time zone when it evaluates times and cron rules.
The following diagram shows the path from a stored schedule to the selected execution system.
flowchart LR
A[Schedule record] --> B[Scheduler refresh]
B --> C[Future time or repeat rule]
C --> D[Schedule runner]
D --> E{Target}
E --> F[Task pool]
E --> G[Workflow service]
F --> H[Task execution]
G --> I[Workflow run]
Template runs retain the schedule as their source so later task history can identify the trigger. Workflow runs retain the schedule on the root workflow run and use the scheduler as their actor. A workflow run needs the workflow service to be available; otherwise the scheduler records the failure and does not start work.
References: services/…/SchedulePool.go• L117-264 services/…/SchedulePool.go• L346-455 services/…/TaskPool.go• L1244-1377 services/…/SchedulePool_test.go• L249-328
Schedule coordination
Schedule changes, task creation, workflow starts, and cluster coordination must describe the same schedule occurrence. In a high-availability deployment, an optional deduplication service lets one node claim an occurrence before it starts work. This reduces duplicate starts when several server nodes hold the same schedule pool, but it is a best-effort guard rather than a guarantee if the service fails or loses its lock.
Repository-backed schedules add a source check before dispatch. The scheduler records the last seen commit and creates work only when the source state has changed. This makes the schedule act as a time-based change detector rather than a blind repeat trigger.
For the detailed pool lifecycle, see Schedule pool processing.
References: services/…/SchedulePool.go• L65-115 services/…/SchedulePool.go• L149-175 services/…/SchedulePool.go• L266-309
Event history
Event records
An event is a stored record of an action on a project or system object. It can point to a user, project, integration, object type, object ID, description, and creation time. The event model is defined in db/Event.go.
Event object types identify the kind of resource affected. Schedule changes use the schedule object type, while task, template, workflow, and other resource changes use their matching types. The server can enrich returned records with names such as the related user or task object when that information is available.
References: db/Event.go• L9-24 db/Event.go• L53-73 db/Event.go• L75-146
Event recording
The event log helper gives server handlers one path to record a resource action. It writes the event to persistent storage and also sends a matching record to the configured log writer. This produces a durable history for the API while preserving a separate operational log stream.
Schedule create, update, activation, deactivation, and delete actions use this event path. They also produce audit records for the more structured audit system; the event log is the user-facing action history, while the audit record carries the audit contract for security and resource tracking.
References: api/…/event_log.go• L11-65 api/…/schedules.go• L147-162 api/…/schedules.go• L202-231 api/…/schedules.go• L279-303
Event retrieval
The server exposes event history at global and project scopes. A global request returns events visible to the user. A project request returns that project's events after the route has checked project access. The short history endpoint limits the result to the latest 200 records, while the full endpoint returns the available history.
The data layer orders events from newest to oldest and fills related display data before the response is sent. This keeps access filtering and event enrichment close to event retrieval instead of making clients rebuild the history.
References: api/events.go• L10-40 api/router.go• L200-214 api/router.go• L362-366 db/…/event.go• L9-29 db/…/event.go• L53-91
Server event handling
Request handling
Server handlers use the request context to choose the event scope and the current user. The route middleware supplies the project context for project history, while the global route uses the authenticated user to apply visibility rules. Event creation uses the same store abstraction as event retrieval, so handlers do not depend on a particular database implementation.
This boundary separates three concerns: a handler decides that an action should be recorded, the event helper builds the common record, and the store persists or reads it. That separation lets resource handlers record actions without owning event storage rules.
References: api/events.go• L10-31 api/…/event_log.go• L29-65 db/Store.go• L543-549
Live task messages
Stored events provide history, but task output and status need a live path. Authenticated clients open /api/ws, and the server sends messages to the user-specific WebSocket channel. The server keeps connection health with ping and pong messages and removes connections that close or cannot accept more data.
In a high-availability deployment, the message path can hand delivery to a cluster broadcaster. The broadcaster sends a message across nodes and then delivers it to local clients. This allows a client connected to any node to receive task updates created elsewhere.
References: api/router.go• L196-198 api/…/handler.go• L24-40 api/…/handler.go• L150-193 api/…/pool.go• L5-25 api/…/pool.go• L55-101
Client update connection
The web client keeps a session-aware WebSocket connection through web/…/Socket.js. It starts the real connection only for an active session, parses incoming JSON messages, and retries after an unexpected close. When no session is active, it uses a safe placeholder instead of opening an unauthenticated connection.
The connection is created for /api/ws by web/…/socket.js. Components can listen to the shared socket for live task updates without knowing whether the message came from the local server or another cluster node.
For the client update path and its message consumers, see Web realtime updates.
References: web/…/Socket.js• L4-90 web/…/socket.js• L1-8
Time-based automation lifecycle
Automation sequence
Time-based automation moves through a short lifecycle. The schedule is saved first, the scheduler loads it, and a due occurrence starts a task or workflow. The selected execution path then emits live progress and may create stored resource events as related server actions occur.
The sequence below shows the relationship between the time path, the task or workflow path, and the two event paths.
sequenceDiagram
participant User
participant API
participant Store
participant Scheduler
participant Execution
participant Client
User->>API: Create or change schedule
API->>Store: Save schedule
API->>Scheduler: Refresh timed entries
Scheduler->>Store: Read current schedule
Scheduler->>Execution: Start task or workflow at due time
Execution-->>Client: Live task message
API->>Store: Record resource event
Client->>API: Read event history
The time path is separate from the history path. A stored event does not trigger a schedule, and a scheduled run does not depend on a client being connected. This lets automation continue when no browser is open while still giving connected users immediate task feedback.
References: api/…/schedules.go• L140-164 services/…/SchedulePool.go• L117-175 services/…/SchedulePool.go• L227-264 api/…/event_log.go• L29-65 api/…/handler.go• L182-193
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to schedule targets or time fields must stay aligned across db/Schedule.go, schedule payload validation, storage reads and writes, and scheduler refresh behavior.
- Changes to template or workflow dispatch must preserve the target-specific inputs: task parameters for templates and survey defaults for workflows.
- Changes to schedule activation or deletion must still refresh the scheduler so stale timed entries cannot fire.
- Changes to schedule-triggered starts must preserve the schedule ID and the schedule audit trigger so task or workflow history can identify its source.
- Changes to event visibility must stay aligned between global event routes, project event routes, and the store queries that apply access rules.
- Changes to WebSocket message routing must preserve user filtering and the local delivery step used after cluster broadcast.
- Changes to live message shape must be coordinated between the server JSON sender and the client JSON parser.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A schedule has neither a template nor a workflow target, or has both | The schedule is rejected because the scheduler has no single execution path. | Validate that exactly one target is set before saving. |
| A workflow schedule includes a repository check | The schedule cannot use a valid source-change rule. | Reject repository-backed workflow schedules. |
| A run-at schedule has no future time | The request is rejected or the stale entry is not scheduled. | Validate run_at and reject past values before saving. |
| A cron expression is empty or invalid | The schedule cannot produce timed task runs. | Validate the cron expression and skip invalid entries during refresh. |
| A schedule is changed without a pool refresh | The old timing can remain active until a later reload. | Refresh the schedule pool after create, update, activation, and delete. |
| Multiple HA nodes claim one occurrence | Duplicate tasks or workflow runs can be created for the same schedule occurrence. | Use the configured schedule deduplicator before starting work. |
| A workflow service is unavailable when a workflow schedule fires | The scheduled workflow does not start. | Wire the workflow store and service before enabling workflow schedules. |
| A WebSocket send queue fills | The affected connection is closed and that client misses live messages. | Keep consumers responsive and reconnect the client after close. |
| An event points to a removed task | The event remains useful but has no task display name. | Treat missing task objects as an empty display name during enrichment. |
Key files
| File | Why to read it |
|---|---|
| db/Schedule.go | Defines the business shape and valid targets of repeat and run-at schedules. |
| api/…/schedules.go | Shows the schedule API contract and when the scheduler is refreshed. |
| services/…/SchedulePool.go | Shows how stored schedules become timed task or workflow starts. |
| services/…/SchedulePool_test.go | Verifies the schedule target, workflow start, and HA coordination contracts. |
| services/…/TaskPool.go | Shows the handoff from a template schedule trigger to task creation and execution. |
| db/Event.go | Defines stored event fields, resource types, and display enrichment. |
| api/…/event_log.go | Shows how server actions enter persistent and operational event logs. |
| api/events.go | Shows global and project event history responses. |
| api/…/handler.go | Shows authenticated live message delivery and connection health. |
| api/…/pool.go | Shows local fan-out and the high-availability broadcast boundary. |
| web/…/Socket.js | Shows session-aware connection, message parsing, and reconnect behavior. |