Search in sources :

Example 1 with LowLevelHeartBeatEvent

use of com.carrotsearch.ant.tasks.junit4.events.LowLevelHeartBeatEvent in project randomizedtesting by randomizedtesting.

the class LocalSlaveStreamHandler method start.

@Override
public void start() throws IOException {
    lastActivity = System.currentTimeMillis();
    pumpers.add(new Thread(new StreamPumper(stdout, sysout), "pumper-stdout"));
    pumpers.add(new Thread(new StreamPumper(stderr, syserr), "pumper-stderr"));
    pumpers.add(new Thread("pumper-events") {

        public void run() {
            pumpEvents(eventStream);
        }
    });
    if (heartbeat > 0) {
        pumpers.add(watchdog = new Thread("pumper-watchdog") {

            public void run() {
                final long heartbeatMillis = TimeUnit.SECONDS.toMillis(heartbeat);
                final long HEARTBEAT = Math.max(500, heartbeatMillis / 5);
                final long HEARTBEAT_EVENT_THRESHOLD = heartbeatMillis;
                try {
                    long lastObservedUpdate = lastActivity;
                    long reportDeadline = lastObservedUpdate + HEARTBEAT_EVENT_THRESHOLD;
                    while (true) {
                        Thread.sleep(HEARTBEAT);
                        Long last = lastActivity;
                        if (last == null) {
                            // terminated.
                            break;
                        }
                        if (last != lastObservedUpdate) {
                            lastObservedUpdate = last;
                            reportDeadline = last + HEARTBEAT_EVENT_THRESHOLD;
                        } else {
                            final long current = System.currentTimeMillis();
                            if (current >= reportDeadline) {
                                eventBus.post(new LowLevelHeartBeatEvent(last, current));
                                reportDeadline = System.currentTimeMillis() + HEARTBEAT_EVENT_THRESHOLD;
                            }
                        }
                    }
                } catch (InterruptedException e) {
                // terminate on interrupt.
                }
            }
        });
    }
    // Start all pumper threads.
    UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {

        public void uncaughtException(Thread t, Throwable e) {
            warnStream.println("Unhandled exception in thread: " + t);
            e.printStackTrace(warnStream);
        }
    };
    for (Thread t : pumpers) {
        t.setUncaughtExceptionHandler(handler);
        t.setDaemon(true);
        t.start();
    }
}
Also used : LowLevelHeartBeatEvent(com.carrotsearch.ant.tasks.junit4.events.LowLevelHeartBeatEvent) StreamPumper(org.apache.tools.ant.taskdefs.StreamPumper) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Aggregations

LowLevelHeartBeatEvent (com.carrotsearch.ant.tasks.junit4.events.LowLevelHeartBeatEvent)1 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)1 StreamPumper (org.apache.tools.ant.taskdefs.StreamPumper)1