How to implement a watchdog timer?

Asked by matteoa on 2018-11-06

hello all,
I have a Sikulix script that automate a sw that sometimes hangs or behave in unusual ways.
So far the script sometimes waits for the appearance of an image and if the image doesn't appears it remains like "stuck" and I have to stop the java process.
I'd like to implement a watchdog timer that when the internal status of the script is not updated for some time closes the script(or tries workaround).
I've checked on the net some options, like to put all the code in a try catch and then from a timer in a different thread raise an exception but I'd like to have something that doesn't force me to include all the script in such clauses.
Any idea?
Thanks for support and "have nice vacations to Raiman!"
Matteo

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
masuo
Solved:
2018-11-06
Last query:
2018-11-06
Last reply:
2018-11-06
masuo (masuo-ohara) said : #1

This is a way to use hotkey handler.

import thread
#-----count down and type hotkey
def Type_Hotkey(name,*args):
    cnt = args[0]
    while cnt > 0:
        cnt = cnt - 1
        sleep(1)
    type(Key.END,Key.CTRL) #type hotkey

#-----hotkey handler
def Exit_Main(event):
    type(Key.CTRL) #dummy type to cancel CTRL
    exit()

#-----Main script
Env.addHotkey(Key.END, KeyModifier.CTRL, Exit_Main)
duration = 5
thread.start_new_thread(Type_Hotkey,("Type_Hotkey",duration))
while True:
    sleep(1)

matteoa (matteoa) said : #2

Hello Masuo,
thanks a lot for helping.
I tried to copy and paste your script directly in the 1.1.4 ide and run from there.
It closes the ide, and the java process that's running it.
I was expecting that the Ide wouldn't be closed
What I did wrong?
Thanks in advance
Matteo
I added some debug to understand better:
import thread
b=getBundleFolder()
Debug.setUserLogFile(b + "DebugSikuliX.log")
Debug.user('getBundleFolder='+ b)

#-----count down and type hotkey
def Type_Hotkey(name,*args):
    Debug.user('Type_Hotkey='+ name)
    cnt = args[0]
    while cnt > 0:
        cnt = cnt - 1
        sleep(1)
    type(Key.END,Key.CTRL) #type hotkey

#-----hotkey handler
def Exit_Main(event):
    Debug.user('Exit_Main')
    type(Key.CTRL) #dummy type to cancel CTRL
    exit()

#-----Main script
Debug.user('Start')
Env.addHotkey(Key.END, KeyModifier.CTRL, Exit_Main)
duration = 5
thread.start_new_thread(Type_Hotkey,("Type_Hotkey",duration))
Debug.user('start_new_thread')
while True:
    sleep(1)
Debug.user('Stop')

And this is the content of the corresponding debug file:
[user (06/11/18 11.23.35)] getBundleFolder=C:\TEst\sikulix\watchdogExample.sikuli\
[user (06/11/18 11.23.35)] Start
[user (06/11/18 11.23.35)] start_new_thread
[user (06/11/18 11.23.35)] Type_Hotkey=Type_Hotkey
[user (06/11/18 11.23.40)] Exit_Main

Best masuo (masuo-ohara) said : #3

This script run continuously, if main script do not delay.

import thread
duration = 10
#-----count down and type hotkey
def Type_Hotkey(name,*args):
    global duration
    while duration > 0:
        duration = duration - 1
        Do.popup("%d" % duration, "remaining time", 1)
    type(Key.END,Key.CTRL) #type hotkey

#-----hotkey handler
def Exit_Main(event):
    type(Key.CTRL) #dummy type to cancel CTRL
    exit()

#-----Main script
Env.addHotkey(Key.END, KeyModifier.CTRL, Exit_Main)
thread.start_new_thread(Type_Hotkey,("Type_Hotkey",""))

while True:
    sleep(5)
    duration = 10

masuo (masuo-ohara) said : #4

This is one of ideas for watchdog, please modify codes as what you want to do.

matteoa (matteoa) said : #5

Thanks masuo, that solved my question.

matteoa (matteoa) said : #6

Thanks a lot Masuo!