Search in sources :

Example 1 with SseWorkerConnectionFunction

use of io.mantisrx.server.worker.client.SseWorkerConnectionFunction in project mantis by Netflix.

the class SampleClient method getMetricsData.

private static void getMetricsData(WorkerMetricsClient mantisClient, final String localJobId) {
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch finishLatch = new CountDownLatch(1);
    final MetricsClient metricsClient = mantisClient.getMetricsClientByJobId(localJobId, new SseWorkerConnectionFunction(true, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.error("Sink connection error: " + throwable.getMessage());
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
                logger.error("Interrupted waiting for retrying connection");
            }
        }
    }), null);
    logger.info("Getting results observable for job {}", localJobId);
    Observable<MantisServerSentEvent> resultsObservable = Observable.merge(metricsClient.getResults());
    logger.info("Subscribing to it");
    final AtomicReference<Subscription> ref = new AtomicReference<>(null);
    final Thread t = new Thread() {

        @Override
        public void run() {
            try {
                startLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                sleep(300000);
            } catch (InterruptedException ie) {
            }
            logger.info("Closing client conx");
            try {
                ref.get().unsubscribe();
                finishLatch.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    t.setDaemon(true);
    final Subscription s = resultsObservable.doOnCompleted(new Action0() {

        @Override
        public void call() {
            finishLatch.countDown();
        }
    }).subscribe(new Action1<MantisServerSentEvent>() {

        @Override
        public void call(MantisServerSentEvent event) {
            if (startLatch.getCount() > 0) {
                startLatch.countDown();
            }
            logger.info("{} Got SSE: {}", localJobId, event.getEventAsString());
        }
    });
    ref.set(s);
    t.start();
    logger.info("SUBSCRIBED to job metrics changes");
    try {
        finishLatch.await();
        logger.info("Sink observable completed");
    } catch (InterruptedException e) {
        logger.error("thread interrupted", e);
    }
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) MetricsClient(io.mantisrx.server.worker.client.MetricsClient) WorkerMetricsClient(io.mantisrx.server.worker.client.WorkerMetricsClient) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) SseWorkerConnectionFunction(io.mantisrx.server.worker.client.SseWorkerConnectionFunction) Subscription(rx.Subscription)

Aggregations

MantisServerSentEvent (io.mantisrx.common.MantisServerSentEvent)1 MetricsClient (io.mantisrx.server.worker.client.MetricsClient)1 SseWorkerConnectionFunction (io.mantisrx.server.worker.client.SseWorkerConnectionFunction)1 WorkerMetricsClient (io.mantisrx.server.worker.client.WorkerMetricsClient)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Subscription (rx.Subscription)1 Action0 (rx.functions.Action0)1 Action1 (rx.functions.Action1)1