Search in sources :

Example 1 with ModuleVersionReporter

use of org.folio.okapi.common.ModuleVersionReporter in project okapi by folio-org.

the class MainVerticle method init.

@Override
public void init(Vertx vertx, Context context) {
    ModuleVersionReporter m = new ModuleVersionReporter("org.folio.okapi/okapi-core");
    okapiVersion = m.getVersion();
    m.logStart();
    boolean enableProxy = false;
    boolean enableDeployment = false;
    super.init(vertx, context);
    JsonObject config = context.config();
    port = Integer.parseInt(Config.getSysConf("port", "9130", config));
    int portStart = Integer.parseInt(Config.getSysConf("port_start", Integer.toString(port + 1), config));
    int portEnd = Integer.parseInt(Config.getSysConf("port_end", Integer.toString(portStart + 10), config));
    String okapiVersion2 = Config.getSysConf("okapiVersion", null, config);
    if (okapiVersion2 != null) {
        okapiVersion = okapiVersion2;
    }
    if (clusterManager != null) {
        logger.info("cluster NodeId " + clusterManager.getNodeID());
    } else {
        logger.info("clusterManager not in use");
    }
    final String host = Config.getSysConf("host", "localhost", config);
    String okapiUrl = Config.getSysConf("okapiurl", "http://localhost:" + port, config);
    // Remove trailing slash, if there
    okapiUrl = okapiUrl.replaceAll("/+$", "");
    final String nodeName = Config.getSysConf("nodename", null, config);
    String storageType = Config.getSysConf("storage", "inmemory", config);
    String loglevel = Config.getSysConf("loglevel", "", config);
    if (!loglevel.isEmpty()) {
        logHelper.setRootLogLevel(loglevel);
    } else {
        String lev = getenv("OKAPI_LOGLEVEL");
        if (lev != null && !lev.isEmpty()) {
            logHelper.setRootLogLevel(loglevel);
        }
    }
    String mode = config.getString("mode", "cluster");
    switch(mode) {
        case "deployment":
            enableDeployment = true;
            break;
        case "proxy":
            enableProxy = true;
            break;
        case "purgedatabase":
            initMode = PURGE;
            // so we get to initialize the database. We exit soon after anyway
            enableProxy = true;
            break;
        case "initdatabase":
            initMode = INIT;
            enableProxy = true;
            break;
        default:
            // cluster and dev
            enableDeployment = true;
            enableProxy = true;
            break;
    }
    storage = new Storage(vertx, storageType, config);
    envManager = new EnvManager(storage.getEnvStore());
    discoveryManager = new DiscoveryManager(storage.getDeploymentStore());
    if (clusterManager != null) {
        discoveryManager.setClusterManager(clusterManager);
    }
    if (enableDeployment) {
        Ports ports = new Ports(portStart, portEnd);
        deploymentManager = new DeploymentManager(vertx, discoveryManager, envManager, host, ports, port, nodeName);
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                CountDownLatch latch = new CountDownLatch(1);
                deploymentManager.shutdown(ar -> latch.countDown());
                try {
                    if (!latch.await(2, TimeUnit.MINUTES)) {
                        logger.error("Timed out waiting to undeploy all");
                    }
                } catch (InterruptedException e) {
                    logger.error("Exception while shutting down");
                    Thread.currentThread().interrupt();
                    throw new IllegalStateException(e);
                }
            }
        });
    }
    if (enableProxy) {
        ModuleStore moduleStore = storage.getModuleStore();
        moduleManager = new ModuleManager(moduleStore);
        TenantStore tenantStore = storage.getTenantStore();
        tenantManager = new TenantManager(moduleManager, tenantStore);
        moduleManager.setTenantManager(tenantManager);
        discoveryManager.setModuleManager(moduleManager);
        logger.info("Proxy using " + storageType + " storage");
        PullManager pullManager = new PullManager(vertx, moduleManager);
        InternalModule internalModule = new InternalModule(moduleManager, tenantManager, deploymentManager, discoveryManager, envManager, pullManager, okapiVersion);
        proxyService = new ProxyService(vertx, moduleManager, tenantManager, discoveryManager, internalModule, okapiUrl);
        tenantManager.setProxyService(proxyService);
    } else {
        // not really proxying, except to /_/deployment
        moduleManager = new ModuleManager(null);
        // make sure it is not shared
        moduleManager.forceLocalMap();
        tenantManager = new TenantManager(moduleManager, null);
        tenantManager.forceLocalMap();
        moduleManager.setTenantManager(tenantManager);
        discoveryManager.setModuleManager(moduleManager);
        InternalModule internalModule = new InternalModule(null, null, deploymentManager, null, envManager, null, okapiVersion);
        // no modules, tenants, or discovery. Only deployment and env.
        proxyService = new ProxyService(vertx, moduleManager, tenantManager, discoveryManager, internalModule, okapiUrl);
    }
}
Also used : EnvManager(org.folio.okapi.env.EnvManager) Json(io.vertx.core.json.Json) ModuleDescriptor(org.folio.okapi.bean.ModuleDescriptor) XOkapiHeaders(org.folio.okapi.common.XOkapiHeaders) NOT_FOUND(org.folio.okapi.common.ErrorType.NOT_FOUND) Storage(org.folio.okapi.service.impl.Storage) ModuleId(org.folio.okapi.common.ModuleId) Router(io.vertx.ext.web.Router) ModuleManager(org.folio.okapi.service.ModuleManager) Context(io.vertx.core.Context) Tenant(org.folio.okapi.bean.Tenant) ModuleStore(org.folio.okapi.service.ModuleStore) OkapiLogger(org.folio.okapi.common.OkapiLogger) DiscoveryManager(org.folio.okapi.discovery.DiscoveryManager) ModuleVersionReporter(org.folio.okapi.common.ModuleVersionReporter) JsonObject(io.vertx.core.json.JsonObject) InternalModule(org.folio.okapi.web.InternalModule) ManagementFactory(java.lang.management.ManagementFactory) Logger(io.vertx.core.logging.Logger) Ports(org.folio.okapi.bean.Ports) TenantManager(org.folio.okapi.service.TenantManager) InitMode(org.folio.okapi.service.impl.Storage.InitMode) Config(org.folio.okapi.common.Config) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) ProxyService(org.folio.okapi.service.ProxyService) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) Set(java.util.Set) PullManager(org.folio.okapi.pull.PullManager) Future(io.vertx.core.Future) TenantStore(org.folio.okapi.service.TenantStore) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) LogHelper(org.folio.okapi.util.LogHelper) DeploymentManager(org.folio.okapi.deployment.DeploymentManager) HttpMethod(io.vertx.core.http.HttpMethod) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpServerOptions(io.vertx.core.http.HttpServerOptions) System.getenv(java.lang.System.getenv) CorsHandler(io.vertx.ext.web.handler.CorsHandler) ModuleVersionReporter(org.folio.okapi.common.ModuleVersionReporter) DiscoveryManager(org.folio.okapi.discovery.DiscoveryManager) DeploymentManager(org.folio.okapi.deployment.DeploymentManager) JsonObject(io.vertx.core.json.JsonObject) Ports(org.folio.okapi.bean.Ports) CountDownLatch(java.util.concurrent.CountDownLatch) ModuleManager(org.folio.okapi.service.ModuleManager) InternalModule(org.folio.okapi.web.InternalModule) PullManager(org.folio.okapi.pull.PullManager) TenantStore(org.folio.okapi.service.TenantStore) Storage(org.folio.okapi.service.impl.Storage) ProxyService(org.folio.okapi.service.ProxyService) ModuleStore(org.folio.okapi.service.ModuleStore) TenantManager(org.folio.okapi.service.TenantManager) EnvManager(org.folio.okapi.env.EnvManager)

Example 2 with ModuleVersionReporter

use of org.folio.okapi.common.ModuleVersionReporter in project okapi by folio-org.

the class MainVerticle method start.

@Override
public void start(Future<Void> fut) throws IOException {
    Router router = Router.router(vertx);
    helloGreeting = System.getenv("helloGreeting");
    if (helloGreeting == null) {
        helloGreeting = "Hello";
    }
    final int port = Integer.parseInt(System.getProperty("port", "8080"));
    String bName = ManagementFactory.getRuntimeMXBean().getName();
    ModuleVersionReporter m = new ModuleVersionReporter("org.folio.okapi/okapi-test-module");
    m.logStart();
    logger.info("Starting okapi-test-module " + bName + " on port " + port);
    router.get("/testb").handler(this::myStreamHandle);
    router.post("/testb").handler(this::myStreamHandle);
    router.delete("/testb").handler(this::myStreamHandle);
    router.get("/testr").handler(this::myStreamHandle);
    router.post("/testr").handler(this::myStreamHandle);
    router.get("/_/tenant").handler(this::myTenantHandle);
    router.post("/_/tenant").handler(this::myTenantHandle);
    router.delete("/_/tenant").handler(this::myTenantHandle);
    router.get("/recurse").handler(this::recurseHandle);
    HttpServerOptions so = new HttpServerOptions().setHandle100ContinueAutomatically(true);
    vertx.createHttpServer(so).requestHandler(router::accept).listen(port, result -> {
        if (result.succeeded()) {
            final String pidFile = System.getProperty("pidFile");
            if (pidFile != null && !pidFile.isEmpty()) {
                final String pid = bName.split("@")[0];
                try (FileWriter fw = new FileWriter(pidFile)) {
                    fw.write(pid);
                    logger.info("Writing " + pid);
                } catch (IOException ex) {
                    logger.error(ex);
                }
            }
            fut.complete();
        } else {
            fut.fail(result.cause());
            logger.error("okapi-test-module failed: " + result.cause());
        }
    });
}
Also used : ModuleVersionReporter(org.folio.okapi.common.ModuleVersionReporter) FileWriter(java.io.FileWriter) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Router(io.vertx.ext.web.Router) IOException(java.io.IOException)

Aggregations

HttpServerOptions (io.vertx.core.http.HttpServerOptions)2 Router (io.vertx.ext.web.Router)2 ModuleVersionReporter (org.folio.okapi.common.ModuleVersionReporter)2 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Vertx (io.vertx.core.Vertx)1 HttpHeaders (io.vertx.core.http.HttpHeaders)1 HttpMethod (io.vertx.core.http.HttpMethod)1 Json (io.vertx.core.json.Json)1 JsonObject (io.vertx.core.json.JsonObject)1 Logger (io.vertx.core.logging.Logger)1 ClusterManager (io.vertx.core.spi.cluster.ClusterManager)1 CorsHandler (io.vertx.ext.web.handler.CorsHandler)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 System.getenv (java.lang.System.getenv)1 ManagementFactory (java.lang.management.ManagementFactory)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1