šŸ“ššŸŽ“ 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