Waking Timers in Transactions
A little knowledge about how timer jobs run helps us write better code.
Imagine we have a button that calls an action. That action wakes a timer, which adds ten records to the database, then throws an exception. Will the timer job succeed, or not?
The timer job runs, despite what we might expect! Why? When you wake a timer, it runs in a parallel thread as a separate transaction. Anything that happens in the parent flow has no bearing on the timer job, and vice versa, once it's initiated.
Why does this matter?
Now, say we have an action that wakes a timer, then goes on to execute some more code. If an exception is thrown by any code after the timer is woken, the timer job will execute regardless. This can cause issues if we expect our code to run all together or not at all.
What should we do instead? When you build an action that call a wake timer, be sure to run all your code first then call the wake timer at the very end. This way, if an error occurs, the transaction will abort and timer will never be woken.
When you wake a timer it runs as its own transaction in a separate thread, independent of the action that calls it. In the parent flow, run all your code first then wake the timer at the end.