🚨 Watchdog-Script to prevent hanging Scripts🚨

Started by Theo Gottwald, May 25, 2024, 02:07:26 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

🚨 Watchdog Functionality:

This script acts as a watchdog, ensuring the main script stays within the communicated time frame. Use this template in your scripts where a watchdog is needed to monitor the main script's processing.

Example Scenario:
Let's take this case: we have a Dictate Script that calls WHISPER from OpenAI. Sometimes WHISPER is just busy and not responding. To prevent the script from "waiting forever" (which it would not do anyway), we can embed the functionality in a "20 Seconds Watchdog".

If the script does not return within the specified 20 seconds, the Main Script is killed, and the Watchdog will end itself automatically with the Main Script.

General Usage:
' Set the Watchdog to 20 Seconds
%SetWatchdog 20

' After using the Watchdog, switch it off
%ClrWatchdog


Sample Use:
'-----------------------------------------------------------
' Dictate
'-----------------------------------------------------------
:But_Dictate
%SetWatchdog 20
GSB.HiColor
GSB.Dictate
GSB.LoButton
%ClrWatchdog
RET.

Usage Instructions:
The code below is ready to be used. Call the Subprogram that starts the Watchdog-Robot. Then use:

The Watchdog will automatically end itself when the Main Script ends.

Watchdog-Code:
'-----------------------------------------------------------
' Dictate
'-----------------------------------------------------------
:But_Dictate
%SetWatchdog 2
GSB.HiColor
GSB.Dictate
GSB.LoButton
%ClrWatchdog
RET.

'**********************************************
' PRR./PRE./PRV. - Watchdog
' This script demonstrates a simple watchdog timer
' using the $$TIM variable to monitor the main script.
'
' Usage:
' - Set $$TIM to a non-zero value to start the watchdog.
' - The watchdog monitors the elapsed time.
' - If $$TIM is zero, the watchdog is off.
' - If the elapsed time exceeds $$TIM, an action is taken.
'**********************************************
:Watchdog_Start
MOD.New|Watchdog
' Variables Initialization
VAR.$$MWH=#cbw#
' Default State is off
VAR.$$TIM=0
'-----------------------------------------------------------
' Parallel Robot Routine
PRR.0
  MOD.Enter|Watchdog
  ' Print robot information
  'PRT.I am (Parallel-)Robot-Nr. #pri#.
  'PRT.I am Son #pri# of $$MWH.

  :Loop
  PRT.Watchloop A
    ' Wait for timer value to be set
  WFV.$$TIM|3
  JIZ.$$TIM|off

    ' Record the start datetime
  $$STA=#dtime#

    ' Main watchdog loop
  :WatchdogLoop
  PRT.Watchloop B: $$TIM
   ' Pause for a short period
  PAU.1

      ' Calculate the elapsed time
  CAL.$$CUT=(#dtime#-$$STA)
  PRT.Test: $$CUT>$$TIM
      ' Check if the elapsed time exceeds the timer value
  IVV.$$CUT>$$TIM
    ' Print the elapsed time and the timer value
    PRT.Watchdog-Timeout: $$CUT>$$TIM

        ' Get and execute the command
    GDF.hp|$$MWH|$$PID
    CPR.$$PID
  EIF.

  ' Check if the timer is reset to zero (watchdog off)
  JIZ.$$TIM|off
      ' Repeat the watchdog loop
  JMP.WatchdogLoop

    ' Off condition handling
  :off
      ' Pause for 1 second
  PAU.1

      ' Jump back to the beginning of the loop
  JMP.Loop

  END.

  ' Set return value for the main program
PRE.$$WDG|0
MOD.Copy to Last|$$WDG
'-----------------------------------------------------------
MOD.Return
RET.

: %SetWatchdog 1
MOD.Enter|Watchdog
VIN.$$TIM=§§§01
PRV.$$WDG|$$TIM
MOD.Return
END%

: %ClrWatchdog
MOD.Enter|Watchdog
VIN.$$TIM=0
PRV.$$WDG|$$TIM
MOD.Return
END%


Hint: From Update 06/05/24 Parallel-Robots inherit not only all Variables but also all Module-Spaces from their "Father Robot". If you have an older version use "MOD.Enter|1" or "MOD.New|Watchdog" instead of "MOD.Enter|Watchdog". I recommend to ask for the newest Update to use this feature.