📚🎓 Toggle flow and all its child fibers [FS]

103 views
Skip to first unread message

Riku

unread,
Aug 31, 2017, 4:14:15 PM8/31/17
to automa...@googlegroups.com
This is a topic about the tutorial flow you can find in the community here.

Please, use it for any comments, suggestions and questions.

Specify the used version.

📚🎓 Toggle flow and all its child fibers [FS]

[TUTORIAL][v1.0]

Some flows/fibers work as services. They start and keep running in background awaiting some context to perform some tasks.

This will show you how such a flow can smartly:
- stop itself with all of its child fibers if it's already running 
- or just normally start if it's not already running.

This will allow you to use the Start button or a single widget/shortcut to toggle the flow between both state: started and stopped

This demo covers multiple cases/questions and provide a sub-flow for each one.

Use the start button to start multiple times the same flow to see the toggle feature in action. You can also create widget shortcuts for each flow and tap them multiple times for the same.

Let's begin with some vocabulary.

If the parallel launches are allowed in a beginning flow block, every time you start this flow, without stopping its previous instances, a new one will be created. Those instances can be called in this tutorial: parent fibers.

When a parent fiber uses blocks like Fork or Subroutine, those blocks create a new fiber in addition of the parent fiber. There will be hence called child fibers in this tutorial. Note that a parent fiber will continue to step along the blocks path after having launched a child fiber from a fork. Indeed, a fork doesn't halt the flow while a subroutine does, letting the parent fiber awaiting the subroutine ending before continuing the flow.

Child fibers launched by fork block don't necessarily end when parent fiber ends. Though there is an option in fork block to do so. In this tutorial, child fibers are launched with a fork block that allow concurrent running fibers.

In the following examples, only a delay block is used to keep the desired fibers alive during 2 hours or until you stop the flow or fibers in a way or the other (stop or toggle). In a real life flow, it would be tasks that run as services in background awaiting value change, data, etc. to perform a task and so on. Parallel launches are to be allowed in all the beginning flow blocks.

If the following explanation sound confusing, just study the flows and run them. They are very documented with dialogs, toast and log messages. It will help you understand how it works, how you can handle fibers and URI. Try to disable logging to only see the messages output by log blocks.

📌 Tip: Create a widget shortcut for each flow to easily start/stop the desired flow.

1⃣ How a flow can stop itself when a parent fiber is still running

This is the most simple case that you will need when the flow has no running child fibers or they are set to stop with their parent.

✒ The flow beginning block provide a variable that store the URI of each instance of the flow (parent fiber).
✒ The URI of the running parent fiber (flow) is stored in an atomic variable.
✒ On each start of the flow, before updating this variable with the new current parent fiber URI, it checks the variable value that is the previous launched flow URI. Then it uses an has fiber stopped block to check if that previous instance is still running.
✒ If so, it stops it and that's the end of the current instance too. Toggle stop success!
✒ If not, the flow continues normally. Toggle start success!

2⃣ How to toggle child fibers only, keeping the original running parent fiber untouched

This is not a common use case but still can be useful to a specific project. You will have to use the stop button from Automate UI or use another flow to stop the original flow. A widget toggle button will only toggle the child fibers. The current running tasks launched by the original parent fiber won't be affected.

✒ When you start this example the first time, it launches 3 child fibers dynamically generated by a for each block ➕ 1 static child fiber. Just to show how you can store their state and URI in a same atomic variable. When the last block of the flow is reached, the parent and its child will keep running in the same condition as in previous example.
✒ When you start the flow again, a new parent fiber will check if there are previous running child fibers just by checking the atomic variable that should contain their URI if exist. If so, the child fibers are stopped, the variable is reset and the current parent fiber exit. The original parent fiber is still running. Toggle stop child fibers only, success!
✒ When you start the flow the third time, a new parent fiber will check child fibers URI variable. Since it has been reset from the previous launch, new child fibers are launched. Just as in step 1. But then the flow will check if original parent is running to prevent running a duplicate parent task. So the new parent will exit at this point, leaving the original one untouched. Toggle start child fibers only, success!

Step 2 then 3 will be run at each start until you stop the flow.

3⃣ and 4⃣ How to toggle a flow, its parent fiber and its children ❓

It is just a mix of case 1 and 2 when you need to toggle a flow and its child fibers. A common use case.

I provide two examples that use different method.

Method 1

✒ This is similar to child fibers handling in case 2⃣ but rather than exit after having stopped the children, the new parent instance will check for previous parent instance just like in case 1⃣. Start and stop tasks are launched according to those checks.

Method 2

✒ All the fibers (parents and children) are added to the same atomic variable. When you start a new instance of the flow, it will check this variable that contains all previously launched fibers but the current instance of course. The current fiber just has to stop all these previous fibers, reset the variable and exit. The next start will check that empty variabe, signal that allow the flow to start all its fibers again.

Reply all
Reply to author
Forward
0 new messages