I have a use case where certain actions need to be executed automatically when a specified time is reached.
Examples:
Close all open trades at End-Of-Day (EOD).
Execute periodic interest payments every month.
Trigger a settlement process when a contract’s maturity date is reached.
I understand that choices are normally exercised by a party/application. However, I am looking for a mechanism that can initiate these actions automatically based on time.
Questions:
Is there a built-in scheduler available in Daml 3.5?
Are Daml Triggers still supported/recommended?
What is the recommended pattern for implementing one-time and recurring scheduled actions?
Should this logic be implemented using an external scheduler (e.g., Kubernetes CronJob, Quartz Scheduler, Temporal, Airflow) that invokes Ledger API commands?
Any guidance or sample implementations would be appreciated.
There is indeed no built-in scheduler in Daml 3.5. All recurring actions need an external automation that calls the Ledger API. The documentation has a note on automation that might interest you: link. Daml Triggers were deprecated in Daml 2.x and are not part of Daml 3.
All technologies you listed can be used to call Ledger API. The important part is smart contract design to ensure no double runs happen, no out of schedule executions, or other desired properties. Daml has a time API that can aid with on-ledger validation, see additional notes here.
Here is an example template:
template RecurringAction
with
operator : Party
scheduleId : Text
occurrence : Int
dueAt : Time
nextDueAt : Optional Time
where
signatory operator
choice Run : ()
controller operator
do
assertDeadlineExceeded "Action is not due yet" dueAt
-- Action code goes here
forA_ nextDueAt $ \next ->
create this with
occurrence = occurrence + 1
dueAt = next
nextDueAt = determineNextDueAt next
The important bits:
Run is a consuming choice, so each occurrence can succeed only once. It does not prevent duplicate attempts, so keep off-ledger side effects idempotent.
assertDeadlineExceeded is a function from Daml time API that blocks early runs. assertWithinDeadline can handle late runs the same way.
determineNextDueAt needs to be implemented according to your setup and requirements. It might require some subtlety so that your scheduling software will remain in-sync with this contract. For example, do not hardcode adding 24 hours for daily tasks, calculate the right time for the next execution instead. For terminating or one-time actions set the nextDueAt to None.
Hope this helps. Happy to expand on this if something is still unclear.