Skip to content

Commit

Permalink
Move startup of base services to 'initialize' phase. #508
Browse files Browse the repository at this point in the history
  • Loading branch information
derekadams committed Aug 11, 2017
1 parent 6a0eb4d commit c9b0ce0
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public void onTenantDeleted(ITenant tenant) throws SiteWhereException {
ISiteWhereTenantEngine engine = getTenantEnginesById().get(tenant.getId());
if (engine != null) {
if (engine.getLifecycleStatus() == LifecycleStatus.Started) {
stopTenantEngine(engine, new LifecycleProgressMonitor(
stopAndTerminateTenantEngine(engine, new LifecycleProgressMonitor(
new LifecycleProgressContext(1, "Shut down deleted tenant engine.")));
}
getTenantEnginesById().remove(tenant.getId());
Expand Down Expand Up @@ -1395,7 +1395,7 @@ public void run() {
LifecycleProgressMonitor threadMonitor = new LifecycleProgressMonitor(
new LifecycleProgressContext(1,
"Stopping tenant engine '" + engine.getTenant().getName() + "'"));
stopTenantEngine(engine, threadMonitor);
stopAndTerminateTenantEngine(engine, threadMonitor);
} catch (SiteWhereException e) {
LOGGER.error("Tenant engine shutdown failed.", e);
}
Expand All @@ -1413,17 +1413,19 @@ public void run() {
}

/**
* Stop a tenant engine.
* Stop and terminate a tenant engine.
*
* @param engine
* @param monitor
* @throws SiteWhereException
*/
protected void stopTenantEngine(ISiteWhereTenantEngine engine, ILifecycleProgressMonitor monitor)
protected void stopAndTerminateTenantEngine(ISiteWhereTenantEngine engine, ILifecycleProgressMonitor monitor)
throws SiteWhereException {
// TODO: Revisit tenant shutdown logic.
if (engine.getLifecycleStatus() == LifecycleStatus.Started) {
engine.lifecycleStop(monitor);
}
engine.lifecycleTerminate(monitor);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ public void initialize(ILifecycleProgressMonitor monitor) {
// Initialize search provider management.
setSearchProviderManager(initializeSearchProviderManagement());

// Start core functions that must run regardless of whether the
// tenant is considered 'started'.
startCoreFunctions(monitor);

setLifecycleStatus(LifecycleStatus.Stopped);
} catch (SiteWhereException e) {
setLifecycleError(e);
Expand All @@ -242,6 +246,27 @@ public void initialize(ILifecycleProgressMonitor monitor) {
}
}

/**
* Start core functionality that must run regardless of whether the tenant
* is truly 'started'.
*
* @param monitor
* @throws SiteWhereException
*/
private void startCoreFunctions(ILifecycleProgressMonitor monitor) throws SiteWhereException {
// Organizes steps for starting server.
ICompositeLifecycleStep start = new CompositeLifecycleStep("Started tenant '" + getTenant().getName() + "'");

// Start base tenant services.
startBaseServices(start);

// Start tenant management API implementations.
startManagementImplementations(start);

// Execute all operations.
start.execute(monitor);
}

/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -594,6 +619,52 @@ protected IScheduleManagement initializeScheduleManagement() throws SiteWhereExc
}
}

/**
* Start base tenant services.
*
* @param start
* @throws SiteWhereException
*/
protected void startBaseServices(ICompositeLifecycleStep start) throws SiteWhereException {
// Start Groovy configuration.
start.addStep(new StartComponentLifecycleStep(this, getGroovyConfiguration(),
"Started tenant Groovy script engine", "Groovy configuration startup failed.", true));

// Start lifecycle components.
for (ILifecycleComponent component : getRegisteredLifecycleComponents()) {
start.addStep(new StartComponentLifecycleStep(this, component, "Started " + component.getComponentName(),
component.getComponentName() + " startup failed.", true));
}
}

/**
* Start tenant management API implementations.
*
* @param start
* @throws SiteWhereException
*/
protected void startManagementImplementations(ICompositeLifecycleStep start) throws SiteWhereException {
// Start asset management.
start.addStep(new StartComponentLifecycleStep(this, getAssetManagement(), "Started asset management",
"Asset management startup failed.", true));

// Start device management cache provider.
start.addStep(new StartComponentLifecycleStep(this, getDeviceManagementCacheProvider(),
"Started device management cache provider", "Device management cache provider startup failed.", true));

// Start device management.
start.addStep(new StartComponentLifecycleStep(this, getDeviceManagement(), "Started device management",
"Device management startup failed.", true));

// Start device management.
start.addStep(new StartComponentLifecycleStep(this, getDeviceEventManagement(),
"Started device event management", "Device event management startup failed.", true));

// Start device management.
start.addStep(new StartComponentLifecycleStep(this, getScheduleManagement(), "Started schedule management",
"Schedule management startup failed.", true));
}

/**
* Verify and initialize search provider manager.
*
Expand Down Expand Up @@ -659,12 +730,6 @@ public void execute(ILifecycleProgressMonitor monitor) throws SiteWhereException
}
});

// Start base tenant services.
startBaseServices(start);

// Start tenant management API implementations.
startManagementImplementations(start);

// Start tenant services.
startTenantServices(start);

Expand All @@ -686,52 +751,6 @@ public void execute(ILifecycleProgressMonitor monitor) throws SiteWhereException
start.execute(monitor);
}

/**
* Start base tenant services.
*
* @param start
* @throws SiteWhereException
*/
protected void startBaseServices(ICompositeLifecycleStep start) throws SiteWhereException {
// Start Groovy configuration.
start.addStep(new StartComponentLifecycleStep(this, getGroovyConfiguration(),
"Started tenant Groovy script engine", "Groovy configuration startup failed.", true));

// Start lifecycle components.
for (ILifecycleComponent component : getRegisteredLifecycleComponents()) {
start.addStep(new StartComponentLifecycleStep(this, component, "Started " + component.getComponentName(),
component.getComponentName() + " startup failed.", true));
}
}

/**
* Start tenant management API implementations.
*
* @param start
* @throws SiteWhereException
*/
protected void startManagementImplementations(ICompositeLifecycleStep start) throws SiteWhereException {
// Start asset management.
start.addStep(new StartComponentLifecycleStep(this, getAssetManagement(), "Started asset management",
"Asset management startup failed.", true));

// Start device management cache provider.
start.addStep(new StartComponentLifecycleStep(this, getDeviceManagementCacheProvider(),
"Started device management cache provider", "Device management cache provider startup failed.", true));

// Start device management.
start.addStep(new StartComponentLifecycleStep(this, getDeviceManagement(), "Started device management",
"Device management startup failed.", true));

// Start device management.
start.addStep(new StartComponentLifecycleStep(this, getDeviceEventManagement(),
"Started device event management", "Device event management startup failed.", true));

// Start device management.
start.addStep(new StartComponentLifecycleStep(this, getScheduleManagement(), "Started schedule management",
"Schedule management startup failed.", true));
}

/**
* Start tenant services.
*
Expand Down Expand Up @@ -835,20 +854,6 @@ protected void stop(ILifecycleProgressMonitor monitor, boolean persist) throws S
// Stop tenant services.
stopTenantServices(stop);

// Stop lifecycle components.
stop.addStep(new SimpleLifecycleStep("Stopped registered components") {

@Override
public void execute(ILifecycleProgressMonitor monitor) throws SiteWhereException {
for (ILifecycleComponent component : getRegisteredLifecycleComponents()) {
component.lifecycleStop(monitor);
}
}
});

// Stop core management implementations.
stopManagementServices(stop);

// Execute operation with progress monitoring.
stop.execute(monitor);
}
Expand Down Expand Up @@ -879,6 +884,47 @@ protected void stopTenantServices(ICompositeLifecycleStep stop) throws SiteWhere
stop.addStep(new StopComponentLifecycleStep(this, getGroovyConfiguration(), "Stopped Groovy engine"));
}

/*
* (non-Javadoc)
*
* @see
* com.sitewhere.server.lifecycle.LifecycleComponent#terminate(com.sitewhere
* .spi.server.lifecycle.ILifecycleProgressMonitor)
*/
@Override
public void terminate(ILifecycleProgressMonitor monitor) throws SiteWhereException {
stopCoreFunctions(monitor);
}

/**
* Stop core functionality. This should only happen if the tenant is
* completely terminated and not in the standard lifecycle loop.
*
* @param monitor
* @throws SiteWhereException
*/
protected void stopCoreFunctions(ILifecycleProgressMonitor monitor) throws SiteWhereException {
// Organizes steps for stopping tenant.
ICompositeLifecycleStep stop = new CompositeLifecycleStep("Stopped tenant '" + getTenant().getName() + "'");

// Stop lifecycle components.
stop.addStep(new SimpleLifecycleStep("Stopped registered components") {

@Override
public void execute(ILifecycleProgressMonitor monitor) throws SiteWhereException {
for (ILifecycleComponent component : getRegisteredLifecycleComponents()) {
component.lifecycleStop(monitor);
}
}
});

// Stop core management implementations.
stopManagementServices(stop);

// Execute operation with progress monitoring.
stop.execute(monitor);
}

/**
* Stop tenant management services.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ public static class StartCommand extends TenantEngineCommand {
@Override
public ICommandResponse call() throws Exception {
try {
getEngine().lifecycleInitialize(
new LifecycleProgressMonitor(new LifecycleProgressContext(1, "Start tenant engine")));
if (getEngine().getLifecycleStatus() == LifecycleStatus.InitializationError) {
return new CommandResponse(CommandResult.Failed, getEngine().getLifecycleError().getMessage());
}
getEngine().lifecycleStart(getProgressMonitor());
if (getEngine().getLifecycleStatus() == LifecycleStatus.LifecycleError) {
return new CommandResponse(CommandResult.Failed, getEngine().getLifecycleError().getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ protected ITenant getTenant(HttpServletRequest request, boolean checkAuthUser) t
LOGGER.error("No tenant engine for tenant: " + match.getName());
throw new TenantNotAvailableException();
}
if (engine.getEngineState().getLifecycleStatus() != LifecycleStatus.Started) {
LOGGER.error("Engine not started for tenant: " + match.getName());
if (engine.getEngineState().getLifecycleStatus() == LifecycleStatus.InitializationError) {
LOGGER.error("Engine not initialized for tenant: " + match.getName());
throw new TenantNotAvailableException();
}

Expand Down

0 comments on commit c9b0ce0

Please sign in to comment.