Community discussions

MikroTik App
 
lordwhiskey
newbie
Topic Author
Posts: 27
Joined: Wed Jun 26, 2013 8:33 am

Async execution of blocking scripts

Sun Mar 10, 2019 12:29 pm

Hi everyone,

I would like to share an approach I adopted for the execution of a script that was blocked by a "failing" /tool fetch command.

Specifically, I was trying to write a script that -- among the other instructions -- performs the remote reboot of an ADSL modem. This is done through the HTTP request (POST) with proper formatting and data exchange.

However, even though the command is successful (i.e., the modem is rebooted properly), the HTTP request returns 303 and it is read by the fetch command as a failure. Since the reboot command was part of a bigger script, it was the reason of the non proper execution of it.

I know that there are different proposals about how to tackle this problem, however I came up with this possible other solution that resembles somehow the concept of running tasks asynchronously.

To tackle this problem, I have 2 actors here:
  • a scheduled script and
  • a regular script.
The role of the regular script is to enable the scheduled script, set the start-time to be the current time (i.e., when the regular script is executed) and set the interval for its execution.

Then we have the scheduled script that carries out the "failing" /tool fetch request (in my case the reboot of the ADSL modem).

Then, when you need to run the /tool fetch part, what you actually do is to run the regular script. This will ensure the execution of the /tool fetch command, but it won't represent a blocking element.

Here below a snippet for example

Regular script

/system scheduler set [/system scheduler find name=SCHEDULED_SCRIPT] start-time=[/system clock get time] 
/system scheduler set [/system scheduler find name=SCHEDULED_SCRIPT] interval=30
/system scheduler set [/system scheduler find name=SCHEDULED_SCRIPT] disabled=no
/delay 45s
/system scheduler set [/system scheduler find name=SCHEDULED_SCRIPT] disabled=yes
Scheduled script (Remark: this script, once created, should be set disabled to avoid problems)

/tool fetch url="FINAL ULR"
I hope this could be helpful for anyone in a similar situaion

Thanks