/**
 * Copyright 2017 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
/**
 *
 */
package com.netflix.conductor.bootstrap;

import com.google.inject.Guice;
import com.google.inject.Injector;

import com.netflix.conductor.elasticsearch.EmbeddedElasticSearchProvider;
import com.netflix.conductor.grpc.server.GRPCServerProvider;
import com.netflix.conductor.jetty.server.JettyServerProvider;

import org.apache.log4j.PropertyConfigurator;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author Viren Entry point for the server
 */
public class Main {

    public static void main(String[] args) throws Exception {

        loadConfigFile(args.length > 0 ? args[0] : System.getenv("CONDUCTOR_CONFIG_FILE"));

        if (args.length == 2) {
            System.out.println("Using log4j config " + args[1]);
            PropertyConfigurator.configure(new FileInputStream(new File(args[1])));
        }

        Injector bootstrapInjector = Guice.createInjector(new BootstrapModule());
        ModulesProvider modulesProvider = bootstrapInjector.getInstance(ModulesProvider.class);
        Injector serverInjector = Guice.createInjector(modulesProvider.get());


        serverInjector.getInstance(EmbeddedElasticSearchProvider.class).get().ifPresent(search -> {
            try {
                search.start();
            } catch (Exception ioe) {
                System.exit(3);
            }
        });

        System.out.println("\n\n\n");
        System.out.println("                     _            _             ");
        System.out.println("  ___ ___  _ __   __| |_   _  ___| |_ ___  _ __ ");
        System.out.println(" / __/ _ \\| '_ \\ / _` | | | |/ __| __/ _ \\| '__|");
        System.out.println("| (_| (_) | | | | (_| | |_| | (__| || (_) | |   ");
        System.out.println(" \\___\\___/|_| |_|\\__,_|\\__,_|\\___|\\__\\___/|_|   ");
        System.out.println("\n\n\n");

        serverInjector.getInstance(GRPCServerProvider.class).get().ifPresent(server -> {
            try {
                server.start();
            } catch (IOException ioe) {
                System.exit(3);
            }
        });

        serverInjector.getInstance(JettyServerProvider.class).get().ifPresent(server -> {
            try {
                server.start();
            } catch (Exception ioe) {
                System.exit(3);
            }
        });

    }

    private static void loadConfigFile(String propertyFile) throws IOException {
        if (propertyFile == null) return;
        System.out.println("Using config file" + propertyFile);
        Properties props = new Properties(System.getProperties());
        props.load(new FileInputStream(propertyFile));
        System.setProperties(props);
    }
}

@@ -695,24 +695,133 @@
 .Injector;%0A%0A
+import com.netflix.conductor.dao.IndexDAO;%0Aimport com.netflix.conductor.elasticsearch.EmbeddedElasticSearch;%0A
 import com.n
@@ -1135,16 +1135,43 @@
 eption;%0A
+import java.util.Optional;%0A
 import j
@@ -1261,24 +1261,84 @@
 ass Main %7B%0A%0A
+    private static final int EMBEDDED_ES_INIT_TIME = 5000;%0A%0A
     public s
@@ -1931,24 +1931,80 @@
 et());%0A%0A
-%0A
        
+ Optional%3CEmbeddedElasticSearch%3E embeddedSearchInstance =
  serverI
@@ -2069,78 +2069,457 @@
 et()
-.ifPresent(search -%3E %7B%0A            try %7B%0A                search.start(
+;%0A        if (embeddedSearchInstance.isPresent()) %7B%0A            try %7B%0A                embeddedSearchInstance.get().start();%0A                /*%0A                 * Elasticsearch embedded instance does not notify when it is up and ready to accept incoming requests.%0A                 * A possible solution for reading and writing into the index is to wait a specific amount of time.%0A                 */%0A                Thread.sleep(EMBEDDED_ES_INIT_TIME
 );%0A 
@@ -2602,36 +2602,95 @@
      %7D%0A        %7D
+%0A        serverInjector.getInstance(IndexDAO.class).setup(
 );%0A%0A
+%0A
         System.o

Update Main class in order to support Embedded ES wait time - If Embedded ES option is chosen, the main class will wait for a EMBEDDED_ES_INIT_TIME amount of time