How can use Sikuli to wait for a button for a long time, with perhaps some maintenance task in between?
===

I have a webpage where I am waiting for a button to appear, and when it appears I would like to click it. The button is on a timer and may take as long as an hour to appear. Also, if the button takes longer than a certain length of time to appear, I'd like to move the mouse (otherwise the website will log me out automatically).

So, to wait for a button to appear I devised this Sikuli script:

    button = "button.png"
    
    while(1):
        if exists(button):
            print("found it")
            click(button)
            break
        else:
            print("wait longer")
            wait(button,30*60)
            # do a regular task
    
    print "all done!"

The above does not seem to be functional. If the button is on screen, the script will find it... However, if it has to wait it will simply time out quickly with a FindFailed exception (on the click() even though the button does not exist on screen). I considered writing a handler, but seems like overkill.

What am I doing wrong and what is the best way to wait a long period for a visual event like this?