package com.hubspot.baragon.service.worker;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.hubspot.baragon.data.BaragonStateDatastore;

@Singleton
public class BaragonBackgroundStateUpdatingWorker implements Runnable {
  private static final Logger LOG = LoggerFactory.getLogger(BaragonBackgroundStateUpdatingWorker.class);

  private final BaragonStateDatastore stateDatastore;
  private Optional<Integer> lastUpdateId;

  @Inject
  public BaragonBackgroundStateUpdatingWorker(BaragonStateDatastore stateDatastore) {
    this.stateDatastore = stateDatastore;
    this.lastUpdateId = Optional.absent();
  }

  @Override
  public void run() {
    try {
      LOG.info("Updating state node in ZK...");

      final long start = System.currentTimeMillis();

      final Optional<Integer> currentUpdateId = stateDatastore.getStateVersion();

      if (!currentUpdateId.equals(lastUpdateId)) {
        stateDatastore.updateStateNode();
        LOG.info("Updated state node in ZK in {}ms (version: {} -> {})", System.currentTimeMillis() - start, lastUpdateId, currentUpdateId);
        lastUpdateId = currentUpdateId;
      } else {
        LOG.info("State node unchanged in {}ms (version: {})", System.currentTimeMillis() - start, lastUpdateId);
      }
    } catch (Exception e) {
      LOG.error("Caught exception during state node update", e);
    }
  }
}

@@ -783,28 +783,29 @@
 %7B%0A      LOG.
-info
+trace
 (%22Updating s
@@ -1069,20 +1069,21 @@
     LOG.
-info
+debug
 (%22Update
@@ -1270,12 +1270,13 @@
 LOG.
-info
+trace
 (%22St

tweak logging levels for background state node update