Skip to content

Trigger

A Trigger Business Object runs code when something happens: manually, on a timer, when a mail arrives, when a file changes, or when an Entity changes. In code, each Trigger is a class with static methods under This.$Trigger.<TriggerName>: execute() runs the Trigger’s code directly, and timer and file Triggers can be started and stopped.

Availability: Server

This.$Trigger.<TriggerName>

Triggers are grouped under the $Trigger namespace. This.<TriggerName> does not work; the $Trigger. part is required.

Which methods a Trigger has depends on its trigger type:

Trigger typeMethods
manualexecute()
timerexecute(), start(), stop(), isRunning()
mailexecute()
fileexecute(), start(), stop(), isRunning()
entityChangenone

Note

This.$Trigger is also typed in shared code (Entity methods with availability serverAndClient or calculation), but it exists only on the server. When shared code runs on the client, This.$Trigger is undefined.

// manual
execute(): Promise<void>
// timer
execute(executionDate?: Date): Promise<void>
// mail
execute(mail: Sys.Types.Mail, imap: SimpleImap): Promise<void>
// file
execute(eventName: WatcherEventName, path: string, watcher?: Stats): Promise<void>

Runs the Trigger’s code once, immediately, with the given arguments. The call does not depend on whether the Trigger is running.

ParameterTrigger typeTypeDescription
executionDatetimerDateThe time the run is for. Default: now.
mailmailSys.Types.MailThe mail to process.
imapmailSimpleImapThe IMAP connection the mail came from.
eventNamefileWatcherEventNameThe kind of file system change.
pathfilestringAbsolute path of the changed file or directory.
watcherfileStatsThe file’s Node.js fs.Stats. If omitted, Lowgile reads them from path, and the call throws if path no longer exists.

Returns: Promise<void> · Availability: Server

start(): void

Starts a timer or file Trigger and saves it as active, so it also runs after a restart.

Returns: void · Availability: Server

Note

start() returns before the change is saved. isRunning() called right after it can still return false.

stop(): void

Stops a timer or file Trigger and saves it as inactive.

Returns: void · Availability: Server

Note

stop() returns before the change is saved. isRunning() called right after it can still return true.

isRunning(): boolean

Tells whether a timer or file Trigger is active. Returns false if the Trigger is not known to the Trigger manager.

Returns: boolean · Availability: Server

type WatcherEventName = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'

The kind of change a file Trigger reacts to: a file or directory was added, a file changed, or a file or directory was removed.

Availability: Server