Пример #1
0
  public void run() {
    EventLog.log("Starting nanny for \"" + getServerDesc() + "\"");
    setStartedAndRunning(true);
    NDC.push(getServerDesc());
    try {
      // Nanny villages
      while (isKeepRunning()) {
        util.getConsole().checkPause();
        TimeWhenRunnable firstTimeWhenRunnable = null;
        sharpTimeWhenRunnable = null;
        if ((messageMode != null) && !messageMode.equals("false")) {
          // check messages before for commands including resume
          int msgRead = messageReader.readMessages(util, loginUrl, messageMode);
          log.info("Messages Done next " + msgRead);
        }
        // check if still enabled
        if (!suspended) {
          // Loop all enabled villages
          EventLog.log("Checking Villages");
          firstTimeWhenRunnable = processVillages();
          log.info("Villages done");
          // gac - move servers below villages
          // Loop all enabled server strategies
          TimeWhenRunnable serverStrategiesTimeWhenRunnable = processServerStrategies();
          if (serverStrategiesTimeWhenRunnable.before(firstTimeWhenRunnable)) {
            log.debug("ServerStrategy Earlier than villages");
            firstTimeWhenRunnable = serverStrategiesTimeWhenRunnable;
          }
          // gac add report processing - exists as server strategy to run periodically
          // but this will run at the end of every active period
          // count if outstanding after this read
          int moreRead = 0;
          if ((reportMode != null) && !reportMode.equals("false")) {
            // set the reporting mode and check reports
            // ReportMessageReader.getInstance().setReportsMode(reportMode);
            // EventLog.log(loginUrl+" about to read reportMode="+reportMode);
            moreRead = reportReader.readReports(util, loginUrl, reportMode);
            log.info("Server Reports done");
          }
          // read messages again
          if ((messageMode != null) && !messageMode.equals("false")) {
            // check messages
            int msgRead = messageReader.readMessages(util, loginUrl, messageMode);
            log.info("Messages at end done next " + msgRead);
            // read more if consuming all message not just scanning titles for commands
            // if (!messageMode.equalsIgnoreCase("titles")) {
            if (!messageMode.equalsIgnoreCase(messageReader.getScanMode())) {
              moreRead += msgRead;
            }
          }
          if (moreRead != 0) {
            // still more to read
            firstTimeWhenRunnable = TimeWhenRunnable.NOW; // Re-run immediately
            log.debug("Villages will be run again because more reports to read (" + moreRead + ")");
          } else {
            // no new reports left
          }
          if (isConfigurationChanged()) {
            // Update configuration
            updateConfig(getNextConfig());
            EventLog.log("evt.configReloadDone", this.getClass());
            this.nextConfig = null; // No need to synchronize here
            firstTimeWhenRunnable = TimeWhenRunnable.NOW; // Re-run immediately
            log.debug("Villages will be run again because of configuration update");
          }
        } else {
          // suspended
          log.debug("All Strategies Suspended");
          firstTimeWhenRunnable =
              new TimeWhenRunnable(
                  System.currentTimeMillis() + SUSPENDCHECK * Util.MILLI_MINUTE); // try again later
        }
        boolean sharp = false;
        long milliPause = 0;
        long milliSharp = -1L;
        if (firstTimeWhenRunnable != null) {
          log.debug("Earliest Strategy @ " + firstTimeWhenRunnable);
          sharp = firstTimeWhenRunnable.isSharp();
          milliPause = firstTimeWhenRunnable.getTime() - System.currentTimeMillis();
          if (milliPause < 0) {
            milliPause = 0;
          }
          int pauseLimit = config.getInt("/@pauseLimit", 86400) * 1000; //  Max 24h
          if (milliPause > pauseLimit) {
            log.info("Limiting pause from " + milliPause + " to " + pauseLimit);
            milliPause = pauseLimit;
          }
          if (sharpTimeWhenRunnable != null) {
            log.debug("Earliest sharp Strategy @ " + sharpTimeWhenRunnable);
            milliSharp = sharpTimeWhenRunnable.getTime() - System.currentTimeMillis();
            if (milliSharp < 0) {
              milliSharp = 0;
            }
          }
          log.debug(
              "Next available Action in "
                  + Util.milliToTimeString(milliPause)
                  + (sharp ? " sharp: " + Util.milliToTimeString(milliSharp) : ""));
        }

        if (isKeepRunning()) {
          util.userPause(milliPause, sharp, milliSharp);
          util.shortestPause(sharp); // Just to be safe
        }
      }
    } catch (Exception e) {
      EventLog.log(e.getMessage());
      log.error("", e);
      setEnabledAndStartStop(false);
    } finally {
      NDC.remove(); // not pop()
    }
  }