Windows PC, can I use the 'Watchdog' function?

Windows PC, can I use the ‘Watchdog’ function?
hello. Nice to meet you.
I want to do a ‘Watchdog’ function in ‘JAVA, Processing’.

[Watchdog means : ]
An electronic timer used to detect and recover from computer malfunctions. During normal operation, the computer periodically restarts the watchdog timer to prevent it from running out or timing out. That is, it means a function to reset the system after monitoring the system for a certain period of time.

link1 : GitHub - offbynull/watchdog: Java toolkit that helps guard against runaway threads.
link2 : Ant example - Watchdog.java - thread, thread, util, vector, watchdog, watchdog

Has anyone tried using ‘Processing’?

  • ref url :
  1. java - How to create a timing watchdog for method execution count protection - Stack Overflow
  2. Ant example - Watchdog.java - thread, thread, util, vector, watchdog, watchdog
  3. java - How to create a timing watchdog for method execution count protection - Stack Overflow
  1. WATCHDOG_v230811.pde
  2. Watchdog.java
  • WATCHDOG_v230811.pde Source Code :

Q1)

Watchdog ww = new Watchdog(10);

void setup(){  
  size(100,100);
  ww.start();  
}

void draw(){  
//  ww.run();  
}

Q2)

Can you write the source code for ‘Processing’ as above?

Nothing happens
What’s the reason?

Hi @GWAK,

Q1 (if you mean the ant implementation with it) makes no sense without an object which implements TimeoutObserver. The watchdog here starts and ends after 10ms and will not notify anything…
It could be used to see if ie a thread runs longer than a specified time. here 10ms, and do anything when timeout is fired.

Q2 what do you not understand regarding the straight forward implemented Observer Pattern from SO post ?
This can be used to check if s.th is called to often in a specific time range…

If you have concrete questions give some insights what you are trying to achieve…

Cheers
— mnse

1 Like
  1. PDE SOURCE
Watchdog ww;

void setup(){  
  size(100,100);  
  try {
    ww = new Watchdog(2000);
    ww.start();
    ww.run();
  }
  catch (Exception   e) {
      
  }  
}

void draw(){  
  
}

public synchronized void WATCHDOG_FUNC(){  
  println(" EXIT!");
  exit(); 
}
  1. Watchdog.java
package org.apache.tools.ant.util;

import java.util.Enumeration;
import java.util.Vector;


public class Watchdog implements Runnable {

    private Vector observers = new Vector(1);
    private long timeout = -1;

    private volatile boolean stopped = false;

    public static final String ERROR_INVALID_TIMEOUT = "timeout less than 1.";


    public Watchdog(long timeout) {
        if (timeout < 1) {
            throw new IllegalArgumentException(ERROR_INVALID_TIMEOUT);
        }
        this.timeout = timeout;
    }


    public void addTimeoutObserver(TimeoutObserver to) {
        //no need to synchronize, as Vector is always synchronized
        observers.addElement(to);
    }


    public void removeTimeoutObserver(TimeoutObserver to) {
        //no need to synchronize, as Vector is always synchronized
        observers.removeElement(to);
    }


    protected final void fireTimeoutOccured() {
        Enumeration e = observers.elements();
        while (e.hasMoreElements()) {
            ((TimeoutObserver) e.nextElement()).timeoutOccured(this);
        }
    }


    public synchronized void start() {
        stopped = false;
        Thread t = new Thread(this, "WATCHDOG_FUNC");
        t.setDaemon(true);
        t.start();

        
    }
    public synchronized void stop() {
        stopped = true;
        notifyAll();
    }


    public synchronized void run() {
        final long until = System.currentTimeMillis() + timeout;
        long now;
        while (!stopped && until > (now = System.currentTimeMillis())) {
            try {
                wait(until - now);
            } catch (InterruptedException e) {
                // Ignore exception
            }
        }
        if (!stopped) {
            fireTimeoutOccured();
        }
    }
}

@mnse

Q1) Does it not work on ‘general Windows PC’?
Q2) ‘Error’ does not appear, but ‘Operation’ does not work.

Why?

Hi @GWAK,

In principle it would work but you need to implement it correct. I could show but that’s not a processing topic for this forum, rather a topic for java and the ant implementation. Also I would not recommend this at all as it is generally provided to track external ant tasks.

But nevertheless I made an example of how this could be done with processing on-board means.

Hope that helps…

Cheers
— mnse

volatile boolean intime      = true;
volatile boolean hasFinished = false;

// tweak this parameters to see
// timeTillLongRunEnds > maxTimeItShouldUse = timeout 
// timeTillLongRunEnds <= maxTimeItShouldUse = in time
int maxTimeItShouldUse  = 5000;
int timeTillLongRunEnds = 10000;  

void setup() {
  size(500, 500);
  textAlign(CENTER, CENTER);
  textSize(100);
  thread("doSomethingLong"); // create a thread with possible long running action
  thread("doObserve");       // create a observer for the long running action
}

void draw() {
  background(0);
  if (intime && hasFinished)
    fill(0, 255, 0);    // green if finished in time
  else if (intime)
    fill(255, 255, 0);  // yellow if still in time but not finished,yet
  else
  fill(255, 0, 0);      // red if takes longer than expected

  text(frameCount, width/2, height/2);
}

void doSomethingLong() {
  try {
    Thread.sleep(timeTillLongRunEnds);
    hasFinished=true;
  }
  catch(InterruptedException e) {
  }
}

void doObserve() {
  int maxWait = millis() + maxTimeItShouldUse;
  while (!hasFinished && millis() < maxWait) {
    // possible sleep thread a bit
  }
  if (!hasFinished) // possible long runner still running trigger timeout
    timeout();
}

void timeout() {
  intime = false;  // or any other action
}

first case: timeout

// tweak this parameters to see
// timeTillLongRunEnds > maxTimeItShouldUse = timeout 
// timeTillLongRunEnds <= maxTimeItShouldUse = in time
int maxTimeItShouldUse  = 2500;
int timeTillLongRunEnds = 5000;   

12-08-2023_10-14-12

second case: intime

// tweak this parameters to see
// timeTillLongRunEnds > maxTimeItShouldUse = timeout 
// timeTillLongRunEnds <= maxTimeItShouldUse = in time
int maxTimeItShouldUse  = 5000;
int timeTillLongRunEnds = 2500;  

12-08-2023_10-14-45

2 Likes

mnse

Hello. You’re busy, but thank you for leaving a reply.

I understand what you mean.
For the above method, I understood about the ‘thread’ method.
Thank you for your attention and answering with examples.

But, you know
The problem with ‘threads’ in the program is that if the program crashes, it won’t work.

In the case of a Windows PC, there are cases in which it becomes jammed. At this point, you want the computer to restart automatically.
So, I want to write ‘Watchdog’.

Anyway, thank you for your example code and interest.

Hi @GWAK,

To do this you need to first checkout the markers for what is a “critical condition” regarding the system needs to be restarted.
But more, as it is almost on a very deep system level you need a low level service to handle this. Such things could be hardly made with java (almost written in c++). Further it needs to run ie as a system driver level with NT Authority\SYSTEM permission to do this on a serious level. Means the Watchdog is the simplest problem on your way … :slight_smile:

Cheers
— mnse

1 Like

@mnse

Thank you for answer. understand.