Trigger Rules & Scheduled Jobs
Overview
A trigger rule is a "when X happens, do Y" rule that connects real events in the system to the actions the business wants to take.
- Sources include an attribute changing value, a form submission, a campaign link click, a new friend adding the account, or a scheduled point in time.
- Actions include sending a message, adding the user to an audience, switching the rich menu, or resuming a workflow from the node where it paused.
Actions that need to wait are not executed immediately. They are queued into scheduled_action
until their due time. Rules that run on a schedule rely on scheduled_trigger_execution
to guarantee that a given cycle is never executed twice.
Core Data Structure
trigger_rule — the rule itself
Holds the definition of each rule, keeping the source side and the action side cleanly separated.
| Group | Columns | Notes |
|---|---|---|
| Source | source_type, source_config | Event type plus its parameters as JSONB |
| Action | action_type, action_config | Action type plus its parameters as JSONB |
| Rate control | enabled, frequency, cooldown_seconds | frequency defaults to once_per_hour |
| Workflow binding | workflow_id, workflow_node_path | Used when the rule is part of a workflow |
| Scope | line_oa_id, organization_id | Isolates data per channel and organization |
The table uses soft deletes via deleted_date and carries indexes on
(line_oa_id, organization_id) and (deleted_date) so that fetching the live rules of a
channel stays cheap.
trigger_log — execution history
Records every evaluation and execution of a rule. Its primary key is a BIGINT because of the volume involved.
- Back-references:
trigger_rule_id,audience_id,user_id(LINE userId),trigger_on - Stores the
action_typeandaction_configas they were at that moment, so history stays auditable even after the rule is edited - Outcome:
status(defaults tosuccess) anderror_message - The
(trigger_rule_id, created_date DESC)index backs the per-rule history screen
scheduled_action — the deferred work queue
Holds work created by a rule but not yet due.
- Job data:
trigger_rule_id(nullable),user_id,action_type,action_config,source_config - Timing:
execute_at,expires_at,executed_at - State:
status(defaults topending) andretry_count workflow_idandworkflow_node_pathlet a workflow resume from where it paused
The indexes map directly onto the access patterns: idx_scheduled_action_pending (execute_at)
picks up due work, idx_scheduled_action_processing (updated_at) serves the reaper that sweeps
up jobs stuck in processing, and idx_scheduled_action_user (user_id, trigger_rule_id) checks
whether a user already has pending work from the same rule.
scheduled_trigger_execution — one row per scheduled cycle
Each row represents a single cycle of a time-based rule. The core of the table is the unique
constraint (trigger_rule_id, scheduled_at), named uq_scheduled_trigger_execution: even when
several workers race to run the same cycle, only one of them can insert the row and proceed.
The table also tracks the state and statistics of the cycle through started_at, completed_at,
status (defaults to processing), total_users, processed_users, failed_users
and error_message.
Related Files
prisma/schema.prisma:1340— theTriggerRulemodelprisma/schema.prisma:1369— theTriggerLogmodelprisma/schema.prisma:1494— theScheduledActionmodelprisma/schema.prisma:1520— theScheduledTriggerExecutionmodelmanual-sql/1.notify_attribute_change.sql,manual-sql/2.notify_attribute_change.sql,manual-sql/3.bank.sql,manual-sql/4.sql,manual-sql/5.sql— thepg_notifytriggers that act as rule sourcesschema-dumps/2026-07-24/schema.sql:3077,:3038,:2706,:2751
Connections to Other Services
- worker-go is the heart of this domain. Its pg-listener consumes the
attribute_changed,form_submitted,campaign_click,friend_track_event_insertedandbooking_eventevents, evaluates the matching rules, writestrigger_log, and creates or executesscheduled_actionentries. - cms-api-go owns rule authoring through its
trigger-rulemodule, along with the execution-history screens. - Connects to Workflow, pg_notify & Event Outbox, Audience and Link Tracking.