improving code

This commit is contained in:
Luca Frosini 2020-10-22 15:42:22 +02:00
parent 0fc5d9886e
commit b12b72e43d
2 changed files with 53 additions and 36 deletions

View File

@ -32,6 +32,7 @@ import org.gcube.smartgears.handler.resourceregistry.resourcemanager.HostingNode
import org.gcube.smartgears.handlers.container.ContainerHandler;
import org.gcube.smartgears.handlers.container.ContainerLifecycleEvent.Start;
import org.gcube.smartgears.lifecycle.container.ContainerLifecycle;
import org.gcube.smartgears.lifecycle.container.ContainerState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -116,9 +117,9 @@ public class HostingNodeHandler extends ContainerHandler {
containerContext.events().subscribe(new Object() {
// @Observes({ activation, part_activation, shutdown, stop, failure })
@Observes({ activation, part_activation })
void onChanged(ContainerLifecycle lc) {
//@Observes({ activation, part_activation, shutdown, stop, failure })
@Observes({ activation, part_activation})
void onChanged(ContainerLifecycle cl) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String currentToken = SecurityTokenProvider.instance.get();
if (currentToken == null) {
@ -127,34 +128,42 @@ public class HostingNodeHandler extends ContainerHandler {
ContextUtility.setContextFromToken(currentToken);
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
ContainerState containerState = cl.state();
switch (containerState) {
case active:
hostingNodeManager.updateFacets();
break;
default:
//hostingNodeManager.updateStatus();
break;
}
} catch (Exception e) {
logger.error("Failed to update {} State", HostingNode.NAME, e);
} finally {
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@Observes({ shutdown, stop, failure })
void onShutdown(ContainerLifecycle cl) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String currentToken = SecurityTokenProvider.instance.get();
if (currentToken == null)
currentToken = containerContext.configuration().startTokens().iterator().next();
ContextUtility.setContextFromToken(currentToken);
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
hostingNodeManager.updateFacets();
} catch (Exception e) {
logger.error("Failed to update {} State", HostingNode.NAME, e);
logger.error("cannot complete periodic update of {}", HostingNode.NAME, e);
} finally {
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@Observes({ shutdown, stop, failure })
void onStop(ContainerLifecycle lc) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String currentToken = SecurityTokenProvider.instance.get();
if (currentToken == null) {
currentToken = containerContext.configuration().startTokens().iterator().next();
}
ContextUtility.setContextFromToken(currentToken);
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
hostingNodeManager.setDown();
} catch (Exception e) {
logger.error("Failed to update {} State", HostingNode.NAME, e);
} finally {
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@Observes(value = addToContext)
void addTo(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
@ -165,7 +174,7 @@ public class HostingNodeHandler extends ContainerHandler {
ContextUtility.setContextFromToken(currentToken);
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
hostingNodeManager.addToContext();
} catch (Exception e) {
logger.error("Failed to update Service State", e);
@ -184,7 +193,7 @@ public class HostingNodeHandler extends ContainerHandler {
ContextUtility.setContextFromToken(currentToken);
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
hostingNodeManager.removeFromContext();
} catch (Exception e) {
logger.error("Failed to update Service State", e);
@ -207,14 +216,14 @@ public class HostingNodeHandler extends ContainerHandler {
// we register it in response to lifecycle events so that we can
// stop and resume along with application
@Observes(value = { activation, part_activation }, kind = resilient)
synchronized void restartPeriodicUpdates(ContainerLifecycle lc) {
synchronized void restartPeriodicUpdates(ContainerLifecycle cl) {
// already running
if (periodicUpdates != null) {
return;
}
if (lc.state() == active) {
if (cl.state() == active) {
logger.info("scheduling periodic updates of {}", HostingNode.NAME);
} else {
logger.info("resuming periodic updates of {}", HostingNode.NAME);
@ -241,7 +250,7 @@ public class HostingNodeHandler extends ContainerHandler {
}
@Observes(value = { stop, failure, shutdown }, kind = resilient)
synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) {
synchronized void cancelPeriodicUpdates(ContainerLifecycle cl) {
if (periodicUpdates != null) {
logger.trace("stopping periodic updates of {}", HostingNode.NAME);
@ -253,6 +262,17 @@ public class HostingNodeHandler extends ContainerHandler {
logger.warn("could not stop periodic updates of {}", HostingNode.NAME, e);
}
}
String currentToken = SecurityTokenProvider.instance.get();
if (currentToken == null)
currentToken = containerContext.configuration().startTokens().iterator().next();
ContextUtility.setContextFromToken(currentToken);
try {
hostingNodeManager.updateFacets();
} catch (Exception e) {
logger.error("cannot complete periodic update of {}", HostingNode.NAME, e);
}
}
});

View File

@ -70,6 +70,7 @@ import org.gcube.smartgears.configuration.container.Site;
import org.gcube.smartgears.configuration.library.SmartGearsConfiguration;
import org.gcube.smartgears.context.container.ContainerContext;
import org.gcube.smartgears.handler.resourceregistry.ContextUtility;
import org.gcube.smartgears.lifecycle.container.ContainerLifecycle;
import org.gcube.smartgears.provider.ProviderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -187,7 +188,7 @@ public class HostingNodeManager {
return hostingNode;
}
public HostingNode setDown() throws ResourceRegistryException {
public HostingNode updateStatus(ContainerLifecycle cl) throws ResourceRegistryException {
logger.debug("Setting {} down", HostingNode.NAME);
ContainerStateFacet containerStateFacet = null;
@ -224,10 +225,6 @@ public class HostingNodeManager {
return hostingNode;
}
private String getState() {
return containerContext.lifecycle().state().remoteForm().toLowerCase();
}
private HostingNode instantiateHostingNode() {
logger.info("Creating {}", HostingNode.NAME);
@ -353,7 +350,7 @@ public class HostingNodeManager {
if (containerStateFacet == null) {
containerStateFacet = new ContainerStateFacetImpl();
}
String state = getState();
String state = containerContext.lifecycle().state().remoteForm().toLowerCase();
containerStateFacet.setValue(state);
return containerStateFacet;
}