@Override public ILifecycleProgressMonitor createStartupProgressMonitor() { return new LifecycleProgressMonitor(new LifecycleProgressContext(1, "Starting SiteWhere Server")); }
@Override public void initialize(ILifecycleProgressMonitor monitor) throws SiteWhereException { initializeDatastoreModules( new LifecycleProgressMonitor(new LifecycleProgressContext(1, "Initializing datastore asset modules"))); }
@Override public T removeApiChannel(String host) throws SiteWhereException { T toRemove = getApiChannels().remove(host); if (toRemove != null) { stopNestedComponent(toRemove, new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Shut down API channel."), getMicroservice())); } return toRemove; }
@Override public void onEntryAdded(IAssetCategory category) { try { SiteWhere.getServer().getAssetModuleManager(getTenant()).onAssetCategoryAdded(category, new LifecycleProgressMonitor(new LifecycleProgressContext(1, "Handle new asset category added"))); } catch (SiteWhereException e) { LOGGER.error("Error creating asset module for new asset category.", e); } }
@Override public void onEntryRemoved(IAssetCategory category) { try { SiteWhere.getServer().getAssetModuleManager(getTenant()).onAssetCategoryRemoved(category, new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Handle new asset category removed"))); } catch (SiteWhereException e) { LOGGER.error("Error removing asset module for asset category.", e); } }
@Override public void onEntryUpdated(IAssetCategory category) { try { SiteWhere.getServer().getAssetModuleManager(getTenant()).onAssetCategoryAdded(category, new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Handle new asset category updated"))); } catch (SiteWhereException e) { LOGGER.error("Error reloading asset module for updated asset category.", e); } }
@Override public void contextDestroyed(ServletContextEvent event) { // TODO: What monitors shutdown? LifecycleProgressMonitor monitor = new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Stopping SiteWhere Server")); SiteWhere.getServer().lifecycleStop(monitor); // Verify shutdown was successful. if (SiteWhere.getServer().getLifecycleStatus() == LifecycleStatus.Stopped) { LOGGER.info("Server shut down successfully."); } // Shut down Log4J manually. LoggerContext context = (LoggerContext) LogManager.getContext(); Configurator.shutdown(context); }
@Override public void onTenantDeleted(ITenant tenant) throws SiteWhereException { // Remove from auth token cache. removeTenantForAuthToken(tenant); // Shutdown tenant engine and remove it. ISiteWhereTenantEngine engine = getTenantEnginesById().get(tenant.getId()); if (engine != null) { if (engine.getLifecycleStatus() == LifecycleStatus.Started) { stopTenantEngine(engine, new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Shut down deleted tenant engine."))); } getTenantEnginesById().remove(tenant.getId()); } }
@Override public void execute(ILifecycleProgressMonitor monitor) throws SiteWhereException { monitor.pushContext(new LifecycleProgressContext(steps.size(), getName())); StringBuffer buffer = new StringBuffer(); buffer.append("About to process composite lifecycle with " + steps.size() + "steps:\n"); for (ILifecycleStep step : steps) { buffer.append(" " + step.getName() + "\n"); } LOGGER.debug(buffer.toString()); try { for (ILifecycleStep step : steps) { LOGGER.debug("Starting " + step.getName()); monitor.startProgress(step.getName()); step.execute(monitor); monitor.finishProgress(); } } finally { monitor.popContext(); } }
/** * Bootstrap a tenant engine with data as specified in the tenant template. * * @param curator * @throws SiteWhereException */ protected void bootstrapTenantEngine(CuratorFramework curator) throws SiteWhereException { ILifecycleProgressMonitor monitor = new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Bootstrap tenant engine."), getTenantEngine().getMicroservice()); String tenantName = getTenantEngine().getTenant().getName(); long start = System.currentTimeMillis(); // Execute tenant bootstrap. IDatasetTemplate template = getTenantEngine().getDatasetTemplate(); getTenantEngine().tenantBootstrap(template, monitor); try { curator.create().forPath(getTenantEngine().getModuleBootstrappedPath()); LOGGER.info("Tenant engine for '" + tenantName + "' bootstrapped in " + (System.currentTimeMillis() - start) + "ms."); } catch (Exception e) { LOGGER.info("Error marking tenant engine '" + tenantName + "' as bootstrapped."); } }
/** * Initialize a tenant engine. * * @param tenant * @return * @throws SiteWhereException */ protected ISiteWhereTenantEngine initializeTenantEngine(ITenant tenant) throws SiteWhereException { ISiteWhereTenantEngine engine = createTenantEngine(tenant, SERVER_SPRING_CONTEXT, getConfigurationResolver()); initializeNestedComponent(engine, new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Initializing tenant engine '" + tenant.getName() + "'"))); if (engine.getLifecycleStatus() != LifecycleStatus.Error) { registerTenant(tenant, engine); return engine; } else { LOGGER.warn("Tenant engine initialization failed for '" + tenant.getId() + "'", engine.getLifecycleError()); return null; } }
/** * Refresh all asset modules. * * @return * @throws SiteWhereException */ @RequestMapping(value = "/modules/refresh", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "Refresh the list of asset modules") @Secured({ SiteWhereRoles.REST }) @Documented(examples = { @Example(stage = Stage.Response, json = Assets.RefreshAssetModules.class, description = "refreshModules.md") }) public void refreshModules(HttpServletRequest servletRequest) throws SiteWhereException { Tracer.start(TracerCategory.RestApiCall, "refreshModules", LOGGER); try { LifecycleProgressMonitor monitor = new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Refreshing asset modules")); SiteWhere.getServer().getAssetModuleManager(getTenant(servletRequest)).refreshModules(monitor); } finally { Tracer.stop(LOGGER); } }
/** * Start a tenant engine. * * @param engine * @throws SiteWhereException */ protected void startTenantEngine(ISiteWhereTenantEngine engine) throws SiteWhereException { if (engine.getLifecycleStatus() != LifecycleStatus.Error) { LifecycleProgressMonitor monitor = new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Starting tenant engine '" + engine.getTenant().getName() + "'")); startNestedComponent(engine, monitor, "Tenant engine '" + engine.getTenant().getName() + "' startup failed.", false); engine.logState(); } else { getLifecycleComponents().put(engine.getComponentId(), engine); LOGGER.info("Skipping startup for tenant engine '" + engine.getTenant().getName() + "' due to initialization errors."); } }
@Override public T call() throws Exception { try { // Start tenant engine. LOGGER.info("Starting tenant engine for '" + getTenantEngine().getTenant().getName() + "'."); ILifecycleProgressMonitor monitor = new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Start tenant engine."), getTenantEngine().getMicroservice()); long start = System.currentTimeMillis(); getTenantEngine().lifecycleStart(monitor); if (getTenantEngine().getLifecycleStatus() == LifecycleStatus.LifecycleError) { throw getTenantEngine().getLifecycleError(); } LOGGER.info("Tenant engine for '" + getTenantEngine().getTenant().getName() + "' started in " + (System.currentTimeMillis() - start) + "ms."); getCompletableFuture().complete(getTenantEngine()); return getTenantEngine(); } catch (Throwable t) { LOGGER.error("Unable to start tenant engine for '" + getTenantEngine().getTenant().getName() + "'.", t); getCompletableFuture().completeExceptionally(t); throw t; } }
@Override public T call() throws Exception { try { // Start microservice. ILifecycleProgressMonitor monitor = new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Start microservice configuration."), getMicroservice()); long start = System.currentTimeMillis(); getMicroservice().configurationStart(getMicroservice().getGlobalApplicationContext(), getMicroservice().getLocalApplicationContext(), monitor); if (getMicroservice().getLifecycleStatus() == LifecycleStatus.LifecycleError) { throw getMicroservice().getLifecycleError(); } LOGGER.debug("Microservice configuration '" + getMicroservice().getName() + "' started in " + (System.currentTimeMillis() - start) + "ms."); getMicroservice().setConfigurationState(ConfigurationState.Started); getCompletableFuture().complete(getMicroservice()); return getMicroservice(); } catch (Throwable t) { LOGGER.error("Unable to start microservice configuration '" + getMicroservice().getName() + "'.", t); getMicroservice().setConfigurationState(ConfigurationState.Failed); getCompletableFuture().completeExceptionally(t); throw t; } }
@Override public T call() throws Exception { try { // Stop microservice. if (getMicroservice().getLifecycleStatus() != LifecycleStatus.Stopped) { ILifecycleProgressMonitor monitor = new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Stop microservice configuration."), getMicroservice()); long start = System.currentTimeMillis(); getMicroservice().configurationStop(getMicroservice().getGlobalApplicationContext(), getMicroservice().getLocalApplicationContext(), monitor); if (getMicroservice().getLifecycleStatus() == LifecycleStatus.LifecycleError) { throw getMicroservice().getLifecycleError(); } LOGGER.debug("Microservice configuration '" + getMicroservice().getName() + "' stopped in " + (System.currentTimeMillis() - start) + "ms."); } getMicroservice().setConfigurationState(ConfigurationState.Stopped); getCompletableFuture().complete(getMicroservice()); return getMicroservice(); } catch (Throwable t) { LOGGER.error("Unable to stop microservice configuration '" + getMicroservice().getName() + "'.", t); getMicroservice().setConfigurationState(ConfigurationState.Failed); getCompletableFuture().completeExceptionally(t); throw t; } }
@Override public T call() throws Exception { try { // Terminate microservice. if (getMicroservice().getLifecycleStatus() != LifecycleStatus.Terminated) { ILifecycleProgressMonitor monitor = new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Terminate microservice configuration."), getMicroservice()); long start = System.currentTimeMillis(); getMicroservice().configurationTerminate(getMicroservice().getGlobalApplicationContext(), getMicroservice().getLocalApplicationContext(), monitor); if (getMicroservice().getLifecycleStatus() == LifecycleStatus.LifecycleError) { throw getMicroservice().getLifecycleError(); } LOGGER.debug("Microservice configuration '" + getMicroservice().getName() + "' terminated in " + (System.currentTimeMillis() - start) + "ms."); } getMicroservice().setConfigurationState(ConfigurationState.Unloaded); getCompletableFuture().complete(getMicroservice()); return getMicroservice(); } catch (Throwable t) { LOGGER.error("Unable to terminate microservice configuration '" + getMicroservice().getName() + "'.", t); getMicroservice().setConfigurationState(ConfigurationState.Failed); getCompletableFuture().completeExceptionally(t); throw t; } }
@Override public ICommandResponse call() throws Exception { try { getEngine().lifecycleInitialize( new LifecycleProgressMonitor(new LifecycleProgressContext(1, "Start tenant engine"))); if (getEngine().getLifecycleStatus() == LifecycleStatus.Error) { return new CommandResponse(CommandResult.Failed, getEngine().getLifecycleError().getMessage()); } getEngine().lifecycleStart(getProgressMonitor()); if (getEngine().getLifecycleStatus() == LifecycleStatus.Error) { return new CommandResponse(CommandResult.Failed, getEngine().getLifecycleError().getMessage()); } } catch (Exception e) { return new CommandResponse(CommandResult.Failed, e.getMessage()); } return new CommandResponse(CommandResult.Successful, "Tenant engine started."); } }
/** * Initialize user model from scripts included in instance template scripts. * * @param templatePath * @param scripts * @throws SiteWhereException */ protected void initializeUserModelFromInstanceTemplateScripts(String templatePath, List<String> scripts) throws SiteWhereException { for (String script : scripts) { getInstanceScriptSynchronizer().add(script); } // Wait for user management APIs to become available. getLogger().info("About to initialize user model from instance template."); getUserManagementApiDemux().waitForMicroserviceAvailable(); getLogger().debug("User management API detected as available."); GroovyConfiguration groovy = new GroovyConfiguration(getInstanceScriptSynchronizer()); groovy.start(new LifecycleProgressMonitor(new LifecycleProgressContext(1, "Initialize user model."), this)); for (String script : scripts) { GroovyUserModelInitializer initializer = new GroovyUserModelInitializer(groovy, script); initializer.initialize(getUserManagementApiDemux().getApiChannel()); } }
/** * Initialize tenant model from scripts included in instance template scripts. * * @param templatePath * @param scripts * @throws SiteWhereException */ protected void initializeTenantModelFromInstanceTemplateScripts(String templatePath, List<String> scripts) throws SiteWhereException { for (String script : scripts) { getInstanceScriptSynchronizer().add(script); } // Wait for tenant management APIs to become available. getLogger().info("About to initialize tenant model from instance template."); getTenantManagementApiDemux().waitForMicroserviceAvailable(); getLogger().debug("Tenant management microservice detected as available."); GroovyConfiguration groovy = new GroovyConfiguration(getInstanceScriptSynchronizer()); groovy.start(new LifecycleProgressMonitor(new LifecycleProgressContext(1, "Initialize tenant model."), this)); for (String script : scripts) { GroovyTenantModelInitializer initializer = new GroovyTenantModelInitializer(groovy, script); initializer.initialize(getTenantManagementApiDemux().getApiChannel()); } }