2015-02-06 10:35:18 +01:00
|
|
|
package org.gcube.vremanagement.executor;
|
|
|
|
|
|
|
|
import java.io.StringWriter;
|
2017-02-01 17:01:15 +01:00
|
|
|
import java.util.ArrayList;
|
2015-02-06 10:35:18 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
import org.gcube.common.authorization.client.Constants;
|
2017-05-18 17:33:07 +02:00
|
|
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
2016-09-22 11:31:05 +02:00
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
2017-02-01 17:01:15 +01:00
|
|
|
import org.gcube.common.authorization.library.ClientType;
|
|
|
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
2016-09-22 11:31:05 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2015-02-06 10:35:18 +01:00
|
|
|
import org.gcube.common.resources.gcore.Resource;
|
|
|
|
import org.gcube.common.resources.gcore.Resources;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Profile;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
2015-02-06 17:53:33 +01:00
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime;
|
2015-02-06 17:25:29 +01:00
|
|
|
import org.gcube.common.resources.gcore.common.Platform;
|
2015-02-06 10:35:18 +01:00
|
|
|
import org.gcube.common.resources.gcore.utils.Group;
|
2016-02-12 18:57:59 +01:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2016-09-22 11:31:05 +02:00
|
|
|
import org.gcube.informationsystem.publisher.RegistryPublisher;
|
2015-02-06 10:35:18 +01:00
|
|
|
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
|
|
|
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
|
2015-02-12 14:40:41 +01:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.gcube.resources.discovery.icclient.ICFactory;
|
2016-09-22 11:31:05 +02:00
|
|
|
import org.gcube.smartgears.ApplicationManager;
|
|
|
|
import org.gcube.smartgears.ContextProvider;
|
2015-02-12 14:40:41 +01:00
|
|
|
import org.gcube.smartgears.configuration.container.ContainerConfiguration;
|
2017-02-10 16:57:10 +01:00
|
|
|
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
|
2019-09-24 14:59:25 +02:00
|
|
|
import org.gcube.vremanagement.executor.json.ExtendedSEMapper;
|
2015-08-26 14:29:00 +02:00
|
|
|
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector;
|
2015-10-12 18:33:47 +02:00
|
|
|
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory;
|
2015-02-06 10:35:18 +01:00
|
|
|
import org.gcube.vremanagement.executor.plugin.PluginDeclaration;
|
|
|
|
import org.gcube.vremanagement.executor.pluginmanager.PluginManager;
|
2017-02-10 16:57:10 +01:00
|
|
|
import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask;
|
2015-07-28 15:41:35 +02:00
|
|
|
import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler;
|
2017-05-25 14:38:46 +02:00
|
|
|
import org.gcube.vremanagement.executor.scheduler.SmartExecutorSchedulerFactory;
|
|
|
|
import org.quartz.SchedulerException;
|
2015-02-06 10:35:18 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
2017-02-01 17:01:15 +01:00
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
2015-02-06 10:35:18 +01:00
|
|
|
*/
|
2016-09-22 11:31:05 +02:00
|
|
|
public class SmartExecutorInitializator implements ApplicationManager {
|
2015-02-06 10:35:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logger
|
|
|
|
*/
|
2015-10-12 11:53:46 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(SmartExecutorInitializator.class);
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2015-07-28 15:41:35 +02:00
|
|
|
public static final long JOIN_TIMEOUT = 1000;
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2017-02-01 17:01:15 +01:00
|
|
|
public static String getCurrentScope(){
|
2016-09-22 11:31:05 +02:00
|
|
|
String token = SecurityTokenProvider.instance.get();
|
|
|
|
AuthorizationEntry authorizationEntry;
|
|
|
|
try {
|
|
|
|
authorizationEntry = Constants.authorizationService().get(token);
|
|
|
|
} catch (Exception e) {
|
2017-07-07 15:22:21 +02:00
|
|
|
logger.trace("Context was not retrieved from token. Going to get it from {}", ScopeProvider.class.getSimpleName());
|
2016-09-22 11:31:05 +02:00
|
|
|
return ScopeProvider.instance.get();
|
|
|
|
}
|
2017-07-07 15:22:21 +02:00
|
|
|
String context = authorizationEntry.getContext();
|
|
|
|
logger.trace("Context retrieved from token is {}. Context in {} is {}",
|
|
|
|
context, ScopeProvider.class.getSimpleName(), ScopeProvider.instance.get());
|
|
|
|
return context;
|
2016-02-09 15:20:26 +01:00
|
|
|
}
|
|
|
|
|
2017-05-18 17:33:07 +02:00
|
|
|
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{
|
|
|
|
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
|
|
|
String context = authorizationEntry.getContext();
|
|
|
|
logger.info("Context of token {} is {}", token, context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void setContext(String token) throws ObjectNotFound, Exception{
|
|
|
|
SecurityTokenProvider.instance.set(token);
|
|
|
|
ScopeProvider.instance.set(getCurrentScope(token));
|
|
|
|
}
|
|
|
|
|
2017-02-01 17:01:15 +01:00
|
|
|
public static ClientInfo getClientInfo() {
|
|
|
|
String token = SecurityTokenProvider.instance.get();
|
|
|
|
AuthorizationEntry authorizationEntry;
|
|
|
|
try {
|
|
|
|
authorizationEntry = Constants.authorizationService().get(token);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return new ClientInfo() {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generated Serial Version UID
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 8311873203596762883L;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ClientType getType() {
|
|
|
|
return ClientType.USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> getRoles() {
|
|
|
|
return new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getId() {
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return authorizationEntry.getClientInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 10:35:18 +01:00
|
|
|
/**
|
|
|
|
* Publish the provided resource on all Service Scopes retrieved from
|
|
|
|
* Context
|
|
|
|
* @param resource to be published
|
|
|
|
* @throws RegistryNotFoundException if the Registry is not found so the
|
|
|
|
* resource has not be published
|
|
|
|
*/
|
2016-09-22 11:31:05 +02:00
|
|
|
private static void publishResource(Resource resource) throws Exception {
|
2015-02-06 10:35:18 +01:00
|
|
|
StringWriter stringWriter = new StringWriter();
|
|
|
|
Resources.marshal(resource, stringWriter);
|
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
|
|
|
|
2015-02-06 10:35:18 +01:00
|
|
|
try {
|
2017-02-01 17:01:15 +01:00
|
|
|
logger.debug("Trying to publish to {}:\n{}", getCurrentScope(), stringWriter);
|
2016-09-22 11:31:05 +02:00
|
|
|
registryPublisher.create(resource);
|
|
|
|
} catch (Exception e) {
|
2015-02-06 10:35:18 +01:00
|
|
|
logger.error("The resource was not published", e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the resource from IS
|
|
|
|
* @param resource to be unpublished
|
|
|
|
* @throws RegistryNotFoundException if the Registry is not found so the
|
|
|
|
* resource has not be published
|
|
|
|
*/
|
2016-09-22 11:31:05 +02:00
|
|
|
private static void unPublishResource(Resource resource) throws Exception {
|
2015-05-06 16:52:15 +02:00
|
|
|
//StringWriter stringWriter = new StringWriter();
|
|
|
|
//Resources.marshal(resource, stringWriter);
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
2015-05-06 16:52:15 +02:00
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
String id = resource.id();
|
2017-02-01 17:01:15 +01:00
|
|
|
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getCurrentScope());
|
2015-05-06 16:52:15 +02:00
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
registryPublisher.remove(resource);
|
2015-05-06 16:52:15 +02:00
|
|
|
|
|
|
|
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
|
2015-02-06 10:35:18 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 14:40:41 +01:00
|
|
|
/**
|
|
|
|
* Return the parsed version string as array of short.
|
|
|
|
* @param version the version as String
|
|
|
|
* @param wantedLenght if the length is equals to dot (.) separated
|
|
|
|
* number in the string. Otherwise the version is padded or truncated to
|
|
|
|
* the required version
|
|
|
|
* @return the parsed version as array of short. If on slicing some of the
|
|
|
|
* version cannot be parsed as short 1 is used for the first number, 0 is
|
|
|
|
* used instead or for padding
|
|
|
|
*/
|
|
|
|
private static short[] getVersionSlice(String version, int wantedLenght){
|
|
|
|
logger.trace("Trying to parse {}", version);
|
|
|
|
|
|
|
|
short[] versionSlices = new short[wantedLenght];
|
|
|
|
for(int j=0; j<wantedLenght; j++){
|
|
|
|
versionSlices[j] = (short) (j==0 ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
String[] stringSlices = version.split("[.-]");
|
|
|
|
for(int i=0; i<stringSlices.length; i++){
|
|
|
|
logger.trace("Parsing version slice n. {} wich is '{}'", i, stringSlices[i]);
|
|
|
|
if(i>=wantedLenght){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
short n = Short.parseShort(stringSlices[i]);
|
|
|
|
versionSlices[i] = n;
|
|
|
|
logger.trace("Version slice n. {} wich is '{}' parsed as short {}", i, stringSlices[i], n);
|
|
|
|
} catch(NumberFormatException nfe){
|
|
|
|
logger.trace("Version slice n. {} wich is '{}' failed to parse. The default value {} will be used", i, stringSlices[i], versionSlices[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(Exception e){
|
|
|
|
logger.trace("Error parsing the supplied version the default will be used", versionSlices);
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.trace("Version {} parsed as {}", version, versionSlices);
|
|
|
|
return versionSlices;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getRunningOn(ContainerConfiguration containerConfiguration){
|
|
|
|
return String.format("%s:%s", containerConfiguration.hostname(), containerConfiguration.port());
|
|
|
|
}
|
|
|
|
|
2015-02-06 10:35:18 +01:00
|
|
|
/**
|
|
|
|
* Create the Service Endpoint using information related to discovered
|
2015-02-12 14:40:41 +01:00
|
|
|
* available plugins and their own discovered capabilities
|
2015-02-06 10:35:18 +01:00
|
|
|
* @return the created {@link ServiceEndpoint}
|
|
|
|
*/
|
2017-02-10 16:57:10 +01:00
|
|
|
protected static ServiceEndpoint createServiceEndpoint(Map<String, PluginDeclaration> availablePlugins){
|
2015-02-06 10:35:18 +01:00
|
|
|
logger.debug("Creating ServiceEndpoint to publish on IS available plugins and their own supported capabilities");
|
|
|
|
ServiceEndpoint serviceEndpoint = new ServiceEndpoint();
|
|
|
|
Profile profile = serviceEndpoint.newProfile();
|
2016-09-22 11:31:05 +02:00
|
|
|
profile.category(ContextProvider.get().configuration().serviceClass());
|
|
|
|
profile.name(ContextProvider.get().configuration().name());
|
|
|
|
String version = ContextProvider.get().configuration().version();
|
2015-02-12 14:40:41 +01:00
|
|
|
profile.version(version);
|
2016-09-22 11:31:05 +02:00
|
|
|
profile.description(ContextProvider.get().configuration().description());
|
2015-02-06 17:53:33 +01:00
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
String runningOn = getRunningOn(ContextProvider.get().container().configuration());
|
2015-02-06 17:25:29 +01:00
|
|
|
Platform platform = profile.newPlatform();
|
2015-02-12 14:40:41 +01:00
|
|
|
platform.name(runningOn);
|
2015-02-11 16:48:04 +01:00
|
|
|
|
2015-02-12 14:40:41 +01:00
|
|
|
short[] versionSlices = getVersionSlice(version, 4);
|
|
|
|
platform.version(versionSlices[0]);
|
|
|
|
platform.minorVersion(versionSlices[1]);
|
|
|
|
platform.buildVersion(versionSlices[2]);
|
|
|
|
platform.revisionVersion(versionSlices[3]);
|
2015-02-06 17:53:33 +01:00
|
|
|
|
|
|
|
Runtime runtime = profile.newRuntime();
|
2015-02-12 14:40:41 +01:00
|
|
|
runtime.hostedOn(runningOn);
|
2016-09-22 11:31:05 +02:00
|
|
|
runtime.status(ContextProvider.get().configuration().mode().toString());
|
2015-02-06 10:35:18 +01:00
|
|
|
|
|
|
|
Group<AccessPoint> accessPoints = profile.accessPoints();
|
|
|
|
|
|
|
|
for(String pluginName : availablePlugins.keySet()){
|
|
|
|
AccessPoint accessPointElement = new AccessPoint();
|
|
|
|
accessPointElement.name(pluginName);
|
2015-02-06 17:16:22 +01:00
|
|
|
|
|
|
|
PluginDeclaration pluginDeclaration = availablePlugins.get(pluginName);
|
|
|
|
|
|
|
|
accessPointElement.description(pluginDeclaration.getDescription());
|
|
|
|
|
2015-02-06 10:35:18 +01:00
|
|
|
Group<Property> properties = accessPointElement.properties();
|
2015-02-06 17:53:33 +01:00
|
|
|
Property propertyVersionElement = new Property();
|
|
|
|
propertyVersionElement.nameAndValue("Version", pluginDeclaration.getVersion());
|
|
|
|
properties.add(propertyVersionElement);
|
|
|
|
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2015-09-28 17:44:12 +02:00
|
|
|
Map<String, String> pluginCapabilities = pluginDeclaration.getSupportedCapabilities();
|
|
|
|
for(String capabilityName : pluginCapabilities.keySet()){
|
2015-02-06 10:35:18 +01:00
|
|
|
Property propertyElement = new Property();
|
2015-09-28 17:44:12 +02:00
|
|
|
propertyElement.nameAndValue(capabilityName, pluginCapabilities.get(capabilityName));
|
2015-02-06 10:35:18 +01:00
|
|
|
properties.add(propertyElement);
|
|
|
|
}
|
|
|
|
accessPoints.add(accessPointElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringWriter stringWriter = new StringWriter();
|
|
|
|
Resources.marshal(serviceEndpoint, stringWriter);
|
2015-02-12 14:40:41 +01:00
|
|
|
logger.debug("The created ServiceEndpoint profile is\n{}", stringWriter.toString());
|
2015-02-06 10:35:18 +01:00
|
|
|
|
|
|
|
return serviceEndpoint;
|
|
|
|
}
|
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
private void cleanServiceEndpoints(){
|
2015-10-12 18:33:47 +02:00
|
|
|
try {
|
|
|
|
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class)
|
2016-09-22 11:31:05 +02:00
|
|
|
.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", ContextProvider.get().configuration().serviceClass()))
|
|
|
|
.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", ContextProvider.get().configuration().name()))
|
|
|
|
.addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'", getRunningOn(ContextProvider.get().container().configuration())))
|
2015-10-12 18:33:47 +02:00
|
|
|
.setResult("$resource");
|
|
|
|
|
|
|
|
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
|
|
|
|
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
|
|
|
|
|
|
|
|
for (ServiceEndpoint serviceEndpoint : serviceEndpoints) {
|
|
|
|
try {
|
|
|
|
logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}",
|
2017-02-01 17:01:15 +01:00
|
|
|
serviceEndpoint.id(), getCurrentScope());
|
2016-09-22 11:31:05 +02:00
|
|
|
unPublishResource(serviceEndpoint);
|
2015-10-12 18:33:47 +02:00
|
|
|
} catch(Exception e){
|
|
|
|
logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} from scope {}",
|
2017-02-01 17:01:15 +01:00
|
|
|
serviceEndpoint.id(), getCurrentScope(), e);
|
2015-10-12 18:33:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.debug("An Exception occur while checking and/or unpublishing old ServiceEndpoint", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
2015-02-06 10:35:18 +01:00
|
|
|
* The method discover the plugins available on classpath and their own
|
|
|
|
* supported capabilities and publish a ServiceEndpoint with the
|
|
|
|
* discovered information.
|
|
|
|
* Furthermore create/connect to DB
|
|
|
|
*/
|
|
|
|
@Override
|
2016-09-22 11:31:05 +02:00
|
|
|
public void onInit() {
|
2017-02-01 17:01:15 +01:00
|
|
|
String scope = getCurrentScope();
|
2017-05-25 14:38:46 +02:00
|
|
|
|
2015-08-31 17:02:11 +02:00
|
|
|
logger.trace(
|
2015-02-12 14:40:41 +01:00
|
|
|
"\n-------------------------------------------------------\n"
|
2016-09-22 11:31:05 +02:00
|
|
|
+ "Smart Executor is Starting on scope {}\n"
|
|
|
|
+ "-------------------------------------------------------",
|
|
|
|
scope);
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2017-02-10 16:57:10 +01:00
|
|
|
logger.debug("Getting Available Plugins and their own supported capabilities");
|
|
|
|
PluginManager pluginManager = PluginManager.getInstance();
|
|
|
|
Map<String, PluginDeclaration> availablePlugins = pluginManager.getAvailablePlugins();
|
|
|
|
ServiceEndpoint serviceEndpoint = createServiceEndpoint(availablePlugins);
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
cleanServiceEndpoints();
|
2015-02-12 14:40:41 +01:00
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
try {
|
2017-02-10 16:57:10 +01:00
|
|
|
publishResource(serviceEndpoint);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Unable to Create ServiceEndpoint for scope {}. The Service will be aborted", scope, e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
final SmartExecutorPersistenceConnector smartExecutorPersistenceConnector;
|
|
|
|
try {
|
|
|
|
smartExecutorPersistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector();
|
2016-09-22 11:31:05 +02:00
|
|
|
} catch (Exception e) {
|
2017-02-10 16:57:10 +01:00
|
|
|
logger.error("Unable to instantiate {} for scope {}. The Service will be aborted",
|
2016-09-22 11:31:05 +02:00
|
|
|
SmartExecutorPersistenceConnector.class.getSimpleName(), scope, e);
|
|
|
|
throw new RuntimeException(e);
|
2015-02-12 14:40:41 +01:00
|
|
|
}
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
// TODO set task that are still on running state to FAILED state on
|
|
|
|
// Persistence to clean previous situation of a failure of HostingNode
|
2015-02-06 10:35:18 +01:00
|
|
|
|
|
|
|
try {
|
2017-05-25 14:38:46 +02:00
|
|
|
logger.debug("Going to get Orphan Scheduled Tasks in scope {}", scope);
|
2017-02-10 16:57:10 +01:00
|
|
|
|
|
|
|
List<ScheduledTask> scheduledTasks = smartExecutorPersistenceConnector.getOrphanScheduledTasks(availablePlugins.values());
|
2017-05-25 14:38:46 +02:00
|
|
|
if(scheduledTasks.size()==0){
|
|
|
|
logger.debug("No Orphan Scheduled Tasks this instance can take in charge in scope {}", scope);
|
|
|
|
}
|
|
|
|
|
2017-02-10 16:57:10 +01:00
|
|
|
for(final ScheduledTask scheduledTask : scheduledTasks){
|
2017-05-25 14:38:46 +02:00
|
|
|
|
2019-09-24 14:59:25 +02:00
|
|
|
String taskAsString = ExtendedSEMapper.getInstance().marshal(scheduledTask);
|
2017-05-25 14:38:46 +02:00
|
|
|
|
2017-02-10 16:57:10 +01:00
|
|
|
try {
|
|
|
|
// Reserving the task.
|
|
|
|
smartExecutorPersistenceConnector.reserveScheduledTask(scheduledTask);
|
|
|
|
}catch (Exception e) {
|
2017-05-25 14:38:46 +02:00
|
|
|
logger.debug("({}) Someone else is going to take in charge the scheduled task {}. Skipping.", scope, taskAsString);
|
2017-02-10 16:57:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread thread = new Thread(){
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run(){
|
|
|
|
LaunchParameter launchParameter = scheduledTask.getLaunchParameter();
|
2017-05-25 14:38:46 +02:00
|
|
|
|
2017-04-13 16:41:16 +02:00
|
|
|
try {
|
2019-09-24 14:59:25 +02:00
|
|
|
logger.info("({}) Going to schedule an already scheduled task with the following parameters {}", scope,
|
|
|
|
ExtendedSEMapper.getInstance().marshal(launchParameter));
|
2017-04-13 16:41:16 +02:00
|
|
|
} catch (Exception e1) {
|
|
|
|
|
|
|
|
}
|
2017-02-10 16:57:10 +01:00
|
|
|
|
|
|
|
String scheduledTasktoken = scheduledTask.getToken();
|
|
|
|
try {
|
2017-05-18 17:33:07 +02:00
|
|
|
setContext(scheduledTasktoken);
|
2017-05-25 14:38:46 +02:00
|
|
|
|
|
|
|
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
|
2017-02-10 16:57:10 +01:00
|
|
|
// A new Scheduled Task will be persisted due to launch. Removing it
|
|
|
|
smartExecutorPersistenceConnector.removeScheduledTask(scheduledTask);
|
|
|
|
smartExecutorScheduler.schedule(launchParameter, scheduledTask.getUUID());
|
|
|
|
} catch (Exception e) {
|
2017-05-25 14:38:46 +02:00
|
|
|
logger.error("({}) Error while trying to relaunch scheduled task.", scope, e);
|
2017-02-10 16:57:10 +01:00
|
|
|
try {
|
|
|
|
smartExecutorPersistenceConnector.addScheduledTask(scheduledTask);
|
|
|
|
} catch (Exception ex) {
|
2017-05-25 14:38:46 +02:00
|
|
|
logger.error("({}) Unable to add back scheduled task {}", scope, taskAsString);
|
2017-02-10 16:57:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2017-05-25 14:38:46 +02:00
|
|
|
|
2017-02-10 16:57:10 +01:00
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
|
2015-02-06 10:35:18 +01:00
|
|
|
} catch (Exception e) {
|
2017-02-10 16:57:10 +01:00
|
|
|
logger.error("Unable to get Orphan Scheduled Tasksfor scope {}.", scope, e);
|
2015-02-06 10:35:18 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-10-12 18:33:47 +02:00
|
|
|
|
2015-08-31 17:02:11 +02:00
|
|
|
logger.trace(
|
2015-02-12 14:40:41 +01:00
|
|
|
"\n-------------------------------------------------------\n"
|
2016-09-22 11:31:05 +02:00
|
|
|
+ "Smart Executor Started Successfully on scope {}\n"
|
|
|
|
+ "-------------------------------------------------------", scope);
|
2015-08-31 11:59:00 +02:00
|
|
|
|
2017-02-10 16:57:10 +01:00
|
|
|
|
2015-02-06 10:35:18 +01:00
|
|
|
}
|
|
|
|
|
2015-10-12 18:33:47 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
2015-02-06 10:35:18 +01:00
|
|
|
* This function is invoked before the service will stop and unpublish the
|
|
|
|
* resource from the IS to maintain the infrastructure integrity.
|
|
|
|
* Furthermore close the connection to DB.
|
|
|
|
*/
|
|
|
|
@Override
|
2016-09-22 11:31:05 +02:00
|
|
|
public void onShutdown(){
|
|
|
|
|
|
|
|
|
2015-08-31 17:02:11 +02:00
|
|
|
logger.trace(
|
2015-02-12 14:40:41 +01:00
|
|
|
"\n-------------------------------------------------------\n"
|
2016-09-22 11:31:05 +02:00
|
|
|
+ "Smart Executor is Stopping on scope {}\n"
|
|
|
|
+ "-------------------------------------------------------",
|
2017-02-01 17:01:15 +01:00
|
|
|
getCurrentScope());
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2017-05-25 14:38:46 +02:00
|
|
|
|
|
|
|
SmartExecutorScheduler scheduler;
|
|
|
|
try {
|
|
|
|
scheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
|
|
|
|
scheduler.stopAll();
|
|
|
|
SmartExecutorSchedulerFactory.remove();
|
|
|
|
} catch (SchedulerException e) {
|
|
|
|
logger.error("", e);
|
|
|
|
}
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
cleanServiceEndpoints();
|
2017-05-25 14:38:46 +02:00
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
try {
|
2017-05-25 14:38:46 +02:00
|
|
|
SmartExecutorPersistenceFactory.closePersistenceConnector();
|
|
|
|
} catch (Throwable e) {
|
2016-09-22 11:31:05 +02:00
|
|
|
logger.error("Unable to correctly close {} for scope {}",
|
|
|
|
SmartExecutorPersistenceConnector.class.getSimpleName(),
|
2017-02-01 17:01:15 +01:00
|
|
|
getCurrentScope(), e);
|
2015-02-06 10:35:18 +01:00
|
|
|
}
|
|
|
|
|
2015-08-31 17:02:11 +02:00
|
|
|
logger.trace(
|
2015-02-12 14:40:41 +01:00
|
|
|
"\n-------------------------------------------------------\n"
|
2016-09-22 11:31:05 +02:00
|
|
|
+ "Smart Executor Stopped Successfully on scope {}\n"
|
|
|
|
+ "-------------------------------------------------------",
|
2017-02-01 17:01:15 +01:00
|
|
|
getCurrentScope());
|
2015-02-06 10:35:18 +01:00
|
|
|
}
|
|
|
|
}
|