Example #1
0
  private static void stopServer(boolean silent, boolean removePid) {
    if (!silent) {
      LOGGER.info("Stopping RESTHeart...");
    }

    if (shutdownHandler != null) {
      if (!silent) {
        LOGGER.info("Waiting for pending request to complete (up to 1 minute)...");
      }
      try {
        shutdownHandler.shutdown();
        shutdownHandler.awaitShutdown(60 * 1000); // up to 1 minute
      } catch (InterruptedException ie) {
        LOGGER.error("Error while waiting for pending request to complete", ie);
      }
    }

    if (server != null) {
      if (!silent) {
        LOGGER.info("Stopping the Undertow server...");
      }
      try {
        server.stop();
      } catch (Throwable t) {
        LOGGER.error("Error stopping the Undertow server", t);
      }
    }

    try {
      if (!silent) {
        LOGGER.info("Flushing and closing the MongoDB client...");
      }
      if (MongoDBClientSingleton.isInitialized()) {
        MongoClient client = MongoDBClientSingleton.getInstance().getClient();
        client.fsync(false);
        client.close();
      }
    } catch (Throwable t) {
      LOGGER.error("Error flushing and closing the MongoDB client", t);
    }

    Path pidFilePath = FileUtils.getPidFilePath(FileUtils.getFileAbsoultePathHash(CONF_FILE_PATH));

    if (removePid && pidFilePath != null) {
      if (!silent) {
        LOGGER.info("Removing the pid file {}", pidFilePath.toString());
      }
      try {
        Files.deleteIfExists(pidFilePath);
      } catch (IOException ex) {
        LOGGER.error("Can't delete pid file {}", pidFilePath.toString(), ex);
      }
    }

    if (!silent) {
      LOGGER.info("Cleaning up temporary directories...");
    }
    TMP_EXTRACTED_FILES
        .keySet()
        .forEach(
            k -> {
              try {
                ResourcesExtractor.deleteTempDir(k, TMP_EXTRACTED_FILES.get(k));
              } catch (URISyntaxException | IOException ex) {
                LOGGER.error(
                    "Error cleaning up temporary directory {}",
                    TMP_EXTRACTED_FILES.get(k).toString(),
                    ex);
              }
            });

    if (!silent) {
      LOGGER.info(ansi().fg(GREEN).bold().a("RESTHeart stopped").reset().toString());
    }
  }
 public DocumentDAO() {
   client = MongoDBClientSingleton.getInstance().getClient();
 }
Example #3
0
  private static void startServer(boolean fork) {
    LOGGER.info("Starting " + ansi().fg(RED).bold().a("RESTHeart").reset().toString());

    if (RESTHEART_VERSION != null) {
      LOGGER.info("version {}", RESTHEART_VERSION);
    }

    Path pidFilePath = FileUtils.getPidFilePath(FileUtils.getFileAbsoultePathHash(CONF_FILE_PATH));

    boolean pidFileAlreadyExists = false;

    if (!OSChecker.isWindows() && pidFilePath != null) {
      pidFileAlreadyExists = checkPidFile(CONF_FILE_PATH);
    }

    logLoggingConfiguration(fork);

    LOGGER.debug(
        "Initializing MongoDB connection pool to {} with options {}",
        configuration.getMongoUri().getHosts(),
        configuration.getMongoUri().getOptions());

    try {
      MongoDBClientSingleton.init(configuration);
      // force setup
      MongoDBClientSingleton.getInstance();
      LOGGER.info("MongoDB connection pool initialized");
      LOGGER.info("MongoDB version {}", MongoDBClientSingleton.getServerVersion());
    } catch (Throwable t) {
      LOGGER.error("Error connecting to MongoDB. exiting..", t);
      stopServer(false, !pidFileAlreadyExists);
      System.exit(-1);
    }

    try {
      startCoreSystem();
    } catch (Throwable t) {
      LOGGER.error("Error starting RESTHeart. Exiting...", t);
      stopServer(false, !pidFileAlreadyExists);
      System.exit(-2);
    }

    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              @Override
              public void run() {
                stopServer(false);
              }
            });

    // create pid file on supported OSes
    if (!OSChecker.isWindows() && pidFilePath != null) {
      FileUtils.createPidFile(pidFilePath);
    }

    // log pid file path on supported OSes
    if (!OSChecker.isWindows() && pidFilePath != null) {
      LOGGER.info("Pid file {}", pidFilePath);
    }

    LOGGER.info(ansi().fg(GREEN).bold().a("RESTHeart started").reset().toString());
  }