Gateway Events that trigger multiple actions: sequential or parallel?

I created a Rule that triggers multiple instances of the “Run Program” addon. Each program then executes the same Netcat program to send commands to Mochad instance running on the same server. This in turn controls X10 devices behind an X10 CM15 controller. This “hack” works ok so far.

Here is an example Run Program command for reference:
/bin/echo “RF O3 OFF” | /home/node/.mozilla-iot/addons/nc -w 5 127.0.0.1 1099

A question or 2 on GW events. I’ve included a logfile snippet showing time stamps

Q: When a scheduled event fires does it execute actions sequentially or in parallel?

After looking at the time stamps for each action in the log file it appears that they overlap indicating to me that they are forked and run in parallel. Mochad seems to operate correctly and must be designed to handle multiple incoming TCP connections natively.

I’ve recently combined the “Run Program” and “X10 CM11” addons into a mostly-working “X10 Mochad” addon. This addon has code to queue and manage multiple commands simultaneously and send them to Mochad sequentially. It works, but still needs improvement to parse return status from Mochad to correctly manage on/off states.

Q: When this new “X10 Mochad” addon is instantiated is there only 1 instance ever created or are these cases were multiple addon instances exist? If there are cases were multiple instances exist how would one coordinate a common command queue?

Thanks,

Eric L. Edberg

2020-12-04 00:00:00.037 INFO   : run-program-adapter: Front Door Light OFF (O5): Running: /bin/echo "RF O5 OFF" | /home/node/.mozilla-iot/addons/nc -w 5 127.0.0.1  1099
2020-12-04 00:00:00.066 INFO   : run-program-adapter: Master Bedroom OFF (C2): Running: /bin/echo "RF C2 OFF" | /home/node/.mozilla-iot/addons/nc -w 5 127.0.0.1  1099
2020-12-04 00:00:00.102 INFO   : run-program-adapter: Garage Indoor OFF (O4): Running: /bin/echo "RF O4 OFF" | /home/node/.mozilla-iot/addons/nc -w 5 127.0.0.1  1099
2020-12-04 00:00:00.142 INFO   : run-program-adapter: Christmas Outdoor OFF (O7): Running: /bin/echo "RF O7 OFF" | /home/node/.mozilla-iot/addons/nc -w 5 127.0.0.1  1099
2020-12-04 00:00:12.984 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx RF HouseUnit: O5 Func: Off
2020-12-04 00:00:12.988 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx PL HouseUnit: O5
2020-12-04 00:00:12.990 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx PL House: O Func: Off
2020-12-04 00:00:12.992 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx RF HouseUnit: C2 Func: Off
2020-12-04 00:00:12.994 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx PL HouseUnit: C2
2020-12-04 00:00:12.997 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx PL House: C Func: Off
2020-12-04 00:00:13.000 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx RF HouseUnit: O4 Func: Off
2020-12-04 00:00:13.003 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx PL HouseUnit: O4
2020-12-04 00:00:13.005 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx PL House: O Func: Off
2020-12-04 00:00:13.008 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx RF HouseUnit: O7 Func: Off
2020-12-04 00:00:13.011 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx PL HouseUnit: O7
2020-12-04 00:00:13.014 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:00 Tx PL House: O Func: Off
2020-12-04 00:00:13.017 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:01 Rx PL HouseUnit: O5
2020-12-04 00:00:13.019 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:01 Rx PL House: O Func: Off
2020-12-04 00:00:13.024 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:05 Rx PL HouseUnit: O4
2020-12-04 00:00:13.026 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:05 Rx PL House: O Func: Off
2020-12-04 00:00:13.029 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:07 Rx PL HouseUnit: O7
2020-12-04 00:00:13.031 INFO   : run-program-adapter: Front Door Light OFF (O5): 12/04 00:00:07 Rx PL House: O Func: Off
2020-12-04 00:00:13.033 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:00 Tx RF HouseUnit: C2 Func: Off
2020-12-04 00:00:13.035 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:00 Tx PL HouseUnit: C2
2020-12-04 00:00:13.037 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:00 Tx PL House: C Func: Off
2020-12-04 00:00:13.039 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:00 Tx RF HouseUnit: O4 Func: Off
2020-12-04 00:00:13.041 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:00 Tx PL HouseUnit: O4
2020-12-04 00:00:13.043 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:00 Tx PL House: O Func: Off
2020-12-04 00:00:13.046 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:00 Tx RF HouseUnit: O7 Func: Off
2020-12-04 00:00:13.048 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:00 Tx PL HouseUnit: O7
2020-12-04 00:00:13.050 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:00 Tx PL House: O Func: Off
2020-12-04 00:00:13.052 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:01 Rx PL HouseUnit: O5
2020-12-04 00:00:13.054 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:01 Rx PL House: O Func: Off
2020-12-04 00:00:13.056 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:05 Rx PL HouseUnit: O4
2020-12-04 00:00:13.058 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:05 Rx PL House: O Func: Off
2020-12-04 00:00:13.060 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:07 Rx PL HouseUnit: O7
2020-12-04 00:00:13.063 INFO   : run-program-adapter: Master Bedroom OFF (C2): 12/04 00:00:07 Rx PL House: O Func: Off
2020-12-04 00:00:13.065 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:00 Tx RF HouseUnit: O4 Func: Off
2020-12-04 00:00:13.067 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:00 Tx PL HouseUnit: O4
2020-12-04 00:00:13.069 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:00 Tx PL House: O Func: Off
2020-12-04 00:00:13.071 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:00 Tx RF HouseUnit: O7 Func: Off
2020-12-04 00:00:13.073 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:00 Tx PL HouseUnit: O7
2020-12-04 00:00:13.076 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:00 Tx PL House: O Func: Off
2020-12-04 00:00:13.083 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:01 Rx PL HouseUnit: O5
2020-12-04 00:00:13.086 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:01 Rx PL House: O Func: Off
2020-12-04 00:00:13.091 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:05 Rx PL HouseUnit: O4
2020-12-04 00:00:13.094 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:05 Rx PL House: O Func: Off
2020-12-04 00:00:13.097 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:07 Rx PL HouseUnit: O7
2020-12-04 00:00:13.099 INFO   : run-program-adapter: Garage Indoor OFF (O4): 12/04 00:00:07 Rx PL House: O Func: Off
2020-12-04 00:00:13.101 INFO   : run-program-adapter: Christmas Outdoor OFF (O7): 12/04 00:00:00 Tx RF HouseUnit: O7 Func: Off
2020-12-04 00:00:13.103 INFO   : run-program-adapter: Christmas Outdoor OFF (O7): 12/04 00:00:00 Tx PL HouseUnit: O7
2020-12-04 00:00:13.106 INFO   : run-program-adapter: Christmas Outdoor OFF (O7): 12/04 00:00:00 Tx PL House: O Func: Off
2020-12-04 00:00:13.108 INFO   : run-program-adapter: Christmas Outdoor OFF (O7): 12/04 00:00:01 Rx PL HouseUnit: O5
2020-12-04 00:00:13.110 INFO   : run-program-adapter: Christmas Outdoor OFF (O7): 12/04 00:00:01 Rx PL House: O Func: Off
2020-12-04 00:00:13.112 INFO   : run-program-adapter: Christmas Outdoor OFF (O7): 12/04 00:00:05 Rx PL HouseUnit: O4
2020-12-04 00:00:13.114 INFO   : run-program-adapter: Christmas Outdoor OFF (O7): 12/04 00:00:05 Rx PL House: O Func: Off
2020-12-04 00:00:13.116 INFO   : run-program-adapter: Christmas Outdoor OFF (O7): 12/04 00:00:07 Rx PL HouseUnit: O7
2020-12-04 00:00:13.118 INFO   : run-program-adapter: Christmas Outdoor OFF (O7): 12/04 00:00:07 Rx PL House: O Func: Off

Only answering so you have something quickly, but my answer may be imperfect.

While I do not know the answer to your exact question, if it works like the handleDeviceSaved of the Adapter add-on API, then I can say that it is parallel and not thread safe. It is likely that they used the same mechanisms, for these two types of events. You could test this by adding a “sleep” in your handling code and produce some output before and after the sleep to see which order they come out.

Thanks for the suggestion.

A sleep is already implemented using the “-w 5” netcat (nc) option. Netcat attaches to the Mochad process and waits 5 seconds capturing all output from currently-executing Mochad commands.

The log file above has the same output logged in multiple Run Command netcat execution sequences. This is how I realized Run Command commands were probably executing in parallel.

They’re run sequentially, but you cannot necessarily count on what order they’ll be run in. They should be run in the order displayed in the UI, but I won’t guarantee that.

There will only be one instance of the add-on itself. However, the add-on can create multiple adapters and such, if it chooses to.

As understand it, the rules engine will trigger each action in sequence. However, from the rules engine point of view, it’s work is done when the command is successfully forked, not when each command completes.

Correct. I reviewed the Run Program addon and it instantiates a Promise (non-blocking fork) to Exec the program. This is confirmed in the original logfile example above after comparing time stamps.

Since Mochad supports at last 3-4 simultaneous incoming TCP connections, and commands, I won’t attempt to “improve” the Run Command addon to optionally implement sequential operation of a command queue.

I’ll move on to polishing my X10 Mochad addon which maintains a aCommandRunQueue array to implement sequential operation (and not block the rules engine).

Thanks for the feedback!