Flow Execution Policy & Automagic Emergency Stop
Posted: 21 Sep 2017 10:12
This is a test flow to help us to understand how Flow Execution Policy and Automagic Emergency Stop works. It doesn't serve any other purpose.
For you who just wanna test the flow, you can jump directly to the "Flow Execution Policy Test flow" below. Flow Execution Policy (FEP)
Automagic has 4 ways to determine what happen when a trigger is received and the current flow is running.
1. Parallel : multiple trigger will run on its own flow execution
2. Skip : multiple trigger will miss if previous triggered one still running
3. Wait (Queue) : Multiple trigger will queue and executed one by one after the running one finish.
4. Stop : stop current running execution and execute new trigger.
King and minister Analogy
If i can make an analogy for it, using king (flow), his minister (trigger) and minister's program (execution).
1. Parallel : The king allow the ministers to execute their program at the same time (parallel execution). Since He has separated all of them based on their department (independent parallel branching), so no interference to each other. Minister of telecommunication won't interfere much to the program from minister of health care.
2. Skip : The king will only allow one program at one time. No other minister can interfere with the running program until it is finished. So all other minister will be eliminated if they disobey (triggers ignored)
3. Wait (Queue) : Wait is more civilized. There is only one program at one time. If other minister (new trigger) try to propose new program, they will be put on queue (trigger wait), until the current program has been finished. So every minister proposal will be accepted (no trigger ignored), but executed based on the queue.
4. Stop : Every new minister program (new trigger) will stop the previous minister program (stop current execution). So it is still only one program at one time. There will be a lot of unfinished program out there if the current program keep replaced by the new one.
I hope the analogy make sense. Now we move to the example of usage.
Usage of each FEP
1. Parallel
is used when you have no specific usage. This is the default when you create new flow. The triggers usually not so frequent, may be ignored if appear in quick succession, and the flow usually finished as soon as it get triggered (very fast). Usually we just deal with local variable, no GloVar/external output involve; since it may be chaos if the trigger execute parallelly and change GloVar in those successive parallel execution. (Example : 1st trigger change GloVar to "hello", 2nd trigger change it "World", while 1st execution still using it as reference). It is also use when you have multiple trigger separate using parallel expression. Each branch execute its own logic, not interfering other branch. See here for more info Multiple Choice - Series vs Parallel
I have a bunch of one trick pony flow before. Each consist of one trigger with 3-5 actions. Those all has similiar function, example to do something with network or send intent; but not necessary related one to each other. So I just combined them into single flow with 5-10 triggers and separate them using parallel expression to check for multiple trigger. This is the example of 7 flow combined into single flow with 7 triggers. Some of the triggers are shortcuts, notification, UI event or media storage event. Each event/action are not related to each other, so I should let them execute parallelly. If i use another FEP, then it will behave differently. Skip FEP will ignored some of the trigger, causing certain event not triggered properly. Wait FEP will delay the flow and make the event not instantly reacted to. Stop FEP will cause some consecutive trigger not executed until finish.
But not all parallel expression checking should use Parallel FEP. It may use another if they have related action in each branch. By default, just use this one first if you are unsure about other FEP.
2. Skip
is used when you need only single execution of certain flow and don't care about the new trigger (or must ignore the new trigger) until this execution is finished. Any new trigger happened during the execution will be ignored (missed) anda can't interfere with current execution in any way. Usually the flow takes more time to finish, since there are timeout or delay in the flow. (Example are take screenshot, downloading file, pinging host etc). When you use GloVar or external file from the beginning until the end of the flow, you surely don't want those value changed by the parallel execution of the same flow. So you must wait until this execution finish first before running another execution.
It is also used when you have action in the flow which can trigger the flow again. You maybe have flow with trigger Wifi State off, then during certain part of the flow, you turn it on and turn it off again. This will trigger the flow again. It also happen to bluetooth, GPS, GloVar, or any trigger that can be triggered again by its own action. The best example for this is clipboard. You may have flow with Clipboard changed trigger, to process the clipboard (example change certain youtube shortlink to full link before sharing it). Then at the end of the flow, you use action Copy Text to Clipboard to set the processed text back to clipboard. This action will trigger the Clipboard changed again, and execute the flow again and again and again until you hit automagic emergency stop. The flow will always runaway anytime you copy something and get disabled in the end. This is the example of my Multiclipboard flow. You can't use parallel FEP in this case, as it is a certain recipe for runaway/infinite loop. Wait FEP also won't work, since the the clipboard trigger still get queue and executed after current one finish. Stop FEP also still result in runaway, as it will stop current execution and re-execute with the new clipboard copied. So the only way you can protect this kind of loop is to use Skip FEP. You must ignore the new trigger caused by the action Copy Text to Clipboard. But if you just leave that action at the end of the flow, since the execution time per element is quite fast (only about 3-5 ms), it will still trigger the flow and loop again. You must put action sleep, example 1 second, after the Copy Text to Clipboard. So it will wait at least 1 second before starting to accept new trigger again. At my phone, i can put the sleep as low as 500 ms to protect it.
Extra note : For you who came from tasker, you maybe know something called Cooldown timer, to protect the profile from looping infinitely. Automagic Skip FEP + sleep (time) at the end of the flow works exactly just like the cooldown timer. This Skip FEP can be used to protect your flow from multiple execution. If you have a flow to backup files and it takes several minutes. You have time trigger and manual trigger via shortcut. Sometimes you press the shortcut and think you haven't tap it. So you tap it again. Or maybe the time trigger also happen around the time you execute it manually. If the FEP is parallel, you might have a corrupted backup in the end, since both parallel execution will try to write to the same file. Using skip will ensure only first backup works, the rest of the trigger won't do anything to the executing flow.
3. Wait
is almost the same as skip, except you can't ignore the trigger. You use it when you need only single execution and MUST react to every trigger happened. No trigger can be ignored, any new trigger must be registered and put into queue; but they may not interfere with current execution. This is the only FEP that can have multiple trigger stacked up and executed in order. If you open the flow, the Trigger element will show number of triggers queuing up since current execution. You can remove the queue by stopping the flow from 3 dot menu or stop flows action. This FEP behaves almost the same as Skip FEP, where the flow takes more time to finish (usually longer), deal with GloVar and external output. The difference are wait FEP appreciates every trigger and maintain sequence (order). The key point is the sequence of the triggers.
I don't use this Wait FEP so far. I have a flow concept since the first week I use Automagic, but I haven't finish it until now LOL I know it can be done, it just takes a long time to finish the whole script. The flow purpose is to Scan Tax document automagically and output it to csv, ready to be imported to the program. It deals with GloVar, take several seconds to connect to the server, parse xml and output to csv; hence it supposed to use this Wait FEP. But can't say much until I finish it. So I will just take another example : reading notification from messaging app.
You create a flow which will read out the sender name, app name and the message from the notification, the flow active when you are driving. Speech output will need several seconds to complete single trigger, since you want to read out something like : "Alan, Whatsapp. where are you?" A lot of the time, people just text us several short text consecutively. This will trigger the flow several times, while the flow haven't finish the speech yet. If you use Parallel FEP, depends on the phone, probably there will be chaos since each trigger will try to compete for the speech output resource. Skip FEP will miss several notification if previous one hasn't finished yet when the new notification arrived. We will miss some important messages. Stop FEP also won't work either. If the message arrived in bulk, probably only the last one will be read out. By using Wait FEP, we will ensure every message will be read out, even though it will take more time as they will be executed sequentially.
For you who just wanna test the flow, you can jump directly to the "Flow Execution Policy Test flow" below. Flow Execution Policy (FEP)
Automagic has 4 ways to determine what happen when a trigger is received and the current flow is running.
1. Parallel : multiple trigger will run on its own flow execution
2. Skip : multiple trigger will miss if previous triggered one still running
3. Wait (Queue) : Multiple trigger will queue and executed one by one after the running one finish.
4. Stop : stop current running execution and execute new trigger.
King and minister Analogy
If i can make an analogy for it, using king (flow), his minister (trigger) and minister's program (execution).
1. Parallel : The king allow the ministers to execute their program at the same time (parallel execution). Since He has separated all of them based on their department (independent parallel branching), so no interference to each other. Minister of telecommunication won't interfere much to the program from minister of health care.
2. Skip : The king will only allow one program at one time. No other minister can interfere with the running program until it is finished. So all other minister will be eliminated if they disobey (triggers ignored)
3. Wait (Queue) : Wait is more civilized. There is only one program at one time. If other minister (new trigger) try to propose new program, they will be put on queue (trigger wait), until the current program has been finished. So every minister proposal will be accepted (no trigger ignored), but executed based on the queue.
4. Stop : Every new minister program (new trigger) will stop the previous minister program (stop current execution). So it is still only one program at one time. There will be a lot of unfinished program out there if the current program keep replaced by the new one.
I hope the analogy make sense. Now we move to the example of usage.
Usage of each FEP
1. Parallel
is used when you have no specific usage. This is the default when you create new flow. The triggers usually not so frequent, may be ignored if appear in quick succession, and the flow usually finished as soon as it get triggered (very fast). Usually we just deal with local variable, no GloVar/external output involve; since it may be chaos if the trigger execute parallelly and change GloVar in those successive parallel execution. (Example : 1st trigger change GloVar to "hello", 2nd trigger change it "World", while 1st execution still using it as reference). It is also use when you have multiple trigger separate using parallel expression. Each branch execute its own logic, not interfering other branch. See here for more info Multiple Choice - Series vs Parallel
I have a bunch of one trick pony flow before. Each consist of one trigger with 3-5 actions. Those all has similiar function, example to do something with network or send intent; but not necessary related one to each other. So I just combined them into single flow with 5-10 triggers and separate them using parallel expression to check for multiple trigger. This is the example of 7 flow combined into single flow with 7 triggers. Some of the triggers are shortcuts, notification, UI event or media storage event. Each event/action are not related to each other, so I should let them execute parallelly. If i use another FEP, then it will behave differently. Skip FEP will ignored some of the trigger, causing certain event not triggered properly. Wait FEP will delay the flow and make the event not instantly reacted to. Stop FEP will cause some consecutive trigger not executed until finish.
But not all parallel expression checking should use Parallel FEP. It may use another if they have related action in each branch. By default, just use this one first if you are unsure about other FEP.
2. Skip
is used when you need only single execution of certain flow and don't care about the new trigger (or must ignore the new trigger) until this execution is finished. Any new trigger happened during the execution will be ignored (missed) anda can't interfere with current execution in any way. Usually the flow takes more time to finish, since there are timeout or delay in the flow. (Example are take screenshot, downloading file, pinging host etc). When you use GloVar or external file from the beginning until the end of the flow, you surely don't want those value changed by the parallel execution of the same flow. So you must wait until this execution finish first before running another execution.
It is also used when you have action in the flow which can trigger the flow again. You maybe have flow with trigger Wifi State off, then during certain part of the flow, you turn it on and turn it off again. This will trigger the flow again. It also happen to bluetooth, GPS, GloVar, or any trigger that can be triggered again by its own action. The best example for this is clipboard. You may have flow with Clipboard changed trigger, to process the clipboard (example change certain youtube shortlink to full link before sharing it). Then at the end of the flow, you use action Copy Text to Clipboard to set the processed text back to clipboard. This action will trigger the Clipboard changed again, and execute the flow again and again and again until you hit automagic emergency stop. The flow will always runaway anytime you copy something and get disabled in the end. This is the example of my Multiclipboard flow. You can't use parallel FEP in this case, as it is a certain recipe for runaway/infinite loop. Wait FEP also won't work, since the the clipboard trigger still get queue and executed after current one finish. Stop FEP also still result in runaway, as it will stop current execution and re-execute with the new clipboard copied. So the only way you can protect this kind of loop is to use Skip FEP. You must ignore the new trigger caused by the action Copy Text to Clipboard. But if you just leave that action at the end of the flow, since the execution time per element is quite fast (only about 3-5 ms), it will still trigger the flow and loop again. You must put action sleep, example 1 second, after the Copy Text to Clipboard. So it will wait at least 1 second before starting to accept new trigger again. At my phone, i can put the sleep as low as 500 ms to protect it.
Extra note : For you who came from tasker, you maybe know something called Cooldown timer, to protect the profile from looping infinitely. Automagic Skip FEP + sleep (time) at the end of the flow works exactly just like the cooldown timer. This Skip FEP can be used to protect your flow from multiple execution. If you have a flow to backup files and it takes several minutes. You have time trigger and manual trigger via shortcut. Sometimes you press the shortcut and think you haven't tap it. So you tap it again. Or maybe the time trigger also happen around the time you execute it manually. If the FEP is parallel, you might have a corrupted backup in the end, since both parallel execution will try to write to the same file. Using skip will ensure only first backup works, the rest of the trigger won't do anything to the executing flow.
3. Wait
is almost the same as skip, except you can't ignore the trigger. You use it when you need only single execution and MUST react to every trigger happened. No trigger can be ignored, any new trigger must be registered and put into queue; but they may not interfere with current execution. This is the only FEP that can have multiple trigger stacked up and executed in order. If you open the flow, the Trigger element will show number of triggers queuing up since current execution. You can remove the queue by stopping the flow from 3 dot menu or stop flows action. This FEP behaves almost the same as Skip FEP, where the flow takes more time to finish (usually longer), deal with GloVar and external output. The difference are wait FEP appreciates every trigger and maintain sequence (order). The key point is the sequence of the triggers.
I don't use this Wait FEP so far. I have a flow concept since the first week I use Automagic, but I haven't finish it until now LOL I know it can be done, it just takes a long time to finish the whole script. The flow purpose is to Scan Tax document automagically and output it to csv, ready to be imported to the program. It deals with GloVar, take several seconds to connect to the server, parse xml and output to csv; hence it supposed to use this Wait FEP. But can't say much until I finish it. So I will just take another example : reading notification from messaging app.
You create a flow which will read out the sender name, app name and the message from the notification, the flow active when you are driving. Speech output will need several seconds to complete single trigger, since you want to read out something like : "Alan, Whatsapp. where are you?" A lot of the time, people just text us several short text consecutively. This will trigger the flow several times, while the flow haven't finish the speech yet. If you use Parallel FEP, depends on the phone, probably there will be chaos since each trigger will try to compete for the speech output resource. Skip FEP will miss several notification if previous one hasn't finished yet when the new notification arrived. We will miss some important messages. Stop FEP also won't work either. If the message arrived in bulk, probably only the last one will be read out. By using Wait FEP, we will ensure every message will be read out, even though it will take more time as they will be executed sequentially.