@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); } }
@Override public void tenantBootstrap(IDatasetTemplate template, ILifecycleProgressMonitor monitor) throws SiteWhereException { List<String> scripts = Collections.emptyList(); if (template.getInitializers() != null) { scripts = template.getInitializers().getScheduleManagement(); for (String script : scripts) { getTenantScriptSynchronizer().add(script); } } // Execute calls as superuser. Authentication previous = SecurityContextHolder.getContext().getAuthentication(); try { SecurityContextHolder.getContext() .setAuthentication(getMicroservice().getSystemUser().getAuthenticationForTenant(getTenant())); GroovyConfiguration groovy = new GroovyConfiguration(getTenantScriptSynchronizer()); groovy.start(new LifecycleProgressMonitor(new LifecycleProgressContext(1, "Initialize schedule model."), getMicroservice())); for (String script : scripts) { GroovyScheduleModelInitializer initializer = new GroovyScheduleModelInitializer(groovy, script); initializer.initialize(getScheduleManagement()); } } finally { SecurityContextHolder.getContext().setAuthentication(previous); } }
/** * 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 void tenantBootstrap(IDatasetTemplate template, ILifecycleProgressMonitor monitor) throws SiteWhereException { List<String> scripts = Collections.emptyList(); if (template.getInitializers() != null) { scripts = template.getInitializers().getAssetManagement(); for (String script : scripts) { getTenantScriptSynchronizer().add(script); } } // Execute remote calls as superuser. Authentication previous = SecurityContextHolder.getContext().getAuthentication(); try { SecurityContextHolder.getContext() .setAuthentication(getMicroservice().getSystemUser().getAuthenticationForTenant(getTenant())); GroovyConfiguration groovy = new GroovyConfiguration(getTenantScriptSynchronizer()); groovy.start(new LifecycleProgressMonitor(new LifecycleProgressContext(1, "Initialize asset model."), getMicroservice())); for (String script : scripts) { GroovyAssetModelInitializer initializer = new GroovyAssetModelInitializer(groovy, script); initializer.initialize(getAssetManagement()); } } finally { SecurityContextHolder.getContext().setAuthentication(previous); } }
new LifecycleProgressMonitor(new LifecycleProgressContext(1, "Issue tenant engine command")) {
@Override public void removeTenantEngine(UUID tenantId) throws SiteWhereException { IMicroserviceTenantEngine engine = getInitializedTenantEngines().get(tenantId); if (engine != null) { // Remove initialized engine if one exists. getInitializedTenantEngines().remove(tenantId); ILifecycleProgressMonitor monitor = new LifecycleProgressMonitor( new LifecycleProgressContext(1, "Shut down tenant engine."), this); // Stop tenant engine. engine.lifecycleStop(monitor); if (engine.getLifecycleStatus() == LifecycleStatus.LifecycleError) { getLogger().error("Error while stopping tenant engine.", engine.getLifecycleError()); } // Terminate tenant engine. engine.lifecycleTerminate(monitor); if (engine.getLifecycleStatus() == LifecycleStatus.LifecycleError) { getLogger().error("Error while terminating tenant engine.", engine.getLifecycleError()); } } else { // Remove failed engine if one exists. engine = getFailedTenantEngines().get(tenantId); if (engine != null) { getFailedTenantEngines().remove(tenantId); } } }
@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; } }