Question about some attributes of a trigger
- cohen.avrahamOP 1
G-d willing
Hello,
Can please give me an example of when we should use theinitialize?Also, can someone please explain to me the
updateStateof a trigger? When exactly it is executed? How is it different from therule?Regarding the
heartbeat, can you give me an example of when it is a good idea to use it? When the heartbeat is set toSome $ minutes 5, still, if a contract is created, theruleis being executed right away. So why do I need to use the heartbeat?
In the documentation, it says about the heartbeatSend a heartbeat message at the given interval.What exactly does it mean?Thanks,
- Mate_Varga 3
Hi @cohen.avraham,
You can use the
initializefunction to initialize the instance of the type for your user defined state. It runs before your trigger performs any other action.
TheupdateStatefunction is executed whenever a new transaction or command completion is observed by the trigger runner.
Please find an example of the usage for both here: trigger-failure/Trigger.daml at 54ca0edacf5b9bb9415fdff9a14a078d0e106454 · digital-asset/trigger-failure · GitHubThe heartbeat is enabling you to ensure that you run your trigger rule at least once in the period you define. This does not mean that it cannot run more frequently than that. I think you can utilize this in batched processing, aggregation etc. If you pair the heartbeat with an
updateStatefunction that only reacts to theMHeartbeatmessage, you could write a trigger that runs only once per the specified interval. The trigger runner utilizes an internal a timer that generates theMHeartbeatmessage invoking the execution ofupdateStateandrule. - cohen.avraham
Hi @Mate_Varga, first thank you very much for your answer.
The example you attached is excellent and helped me understand triggers better. - cohen.avraham
Hi @Mate_Varga
What is the advantage of restarting the trigger, like there is in thewithFailuresfunction?
Can you give me a good use case so I can understand it? - Mate_Varga
Hi @cohen.avraham,
The advantage is that you do not have to redeploy/restart your trigger from an operational point of view, it only enables the trigger failure state to be reset upon observing a new FailedTriggerStartRequest contract, so processing of the trigger to be continued.