2015-02-06 10:35:18 +01:00
|
|
|
package org.gcube.vremanagement.executor;
|
|
|
|
|
2021-05-27 09:28:36 +02: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.smartgears.ApplicationManager;
|
|
|
|
import org.gcube.smartgears.ContextProvider;
|
2020-09-30 11:19:49 +02:00
|
|
|
import org.gcube.smartgears.context.application.ApplicationContext;
|
2017-02-10 16:57:10 +01:00
|
|
|
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
|
2020-10-01 12:51:57 +02:00
|
|
|
import org.gcube.vremanagement.executor.ispublisher.ISPublisher;
|
2020-10-14 14:57:33 +02:00
|
|
|
import org.gcube.vremanagement.executor.ispublisher.RestISPublisher;
|
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;
|
2020-09-30 11:19:49 +02:00
|
|
|
import org.gcube.vremanagement.executor.plugin.Plugin;
|
2015-02-06 10:35:18 +01:00
|
|
|
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
|
|
|
|
2015-10-12 18:33:47 +02:00
|
|
|
/**
|
|
|
|
* {@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
|
2020-10-14 11:25:52 +02:00
|
|
|
public void onInit() {
|
2020-12-02 11:22:48 +01:00
|
|
|
String context = ContextUtility.getCurrentContext();
|
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"
|
2020-12-02 11:22:48 +01:00
|
|
|
+ "Smart Executor is Starting on context {}\n"
|
2016-09-22 11:31:05 +02:00
|
|
|
+ "-------------------------------------------------------",
|
2020-12-02 11:22:48 +01:00
|
|
|
context);
|
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");
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2020-10-14 11:25:52 +02:00
|
|
|
PluginManager pluginManager = PluginManager.getInstance();
|
|
|
|
|
|
|
|
Map<String, Class<? extends Plugin>> availablePlugins = pluginManager.getAvailablePlugins();
|
2020-09-30 11:19:49 +02:00
|
|
|
ApplicationContext applicationContext = ContextProvider.get();
|
2015-02-12 14:40:41 +01:00
|
|
|
|
2020-10-13 15:33:26 +02:00
|
|
|
List<ISPublisher> isPublishers = ISPublisher.getISPublishers(applicationContext);
|
|
|
|
for(ISPublisher isPublisher : isPublishers) {
|
|
|
|
try {
|
|
|
|
isPublisher.unpublishPlugins(true);
|
|
|
|
}catch (Exception e) {
|
2020-12-09 11:59:37 +01:00
|
|
|
logger.error("Unable to unpublish plugins from IS using {}. Trying to continue.", isPublisher.getClass().getName());
|
2020-10-13 15:33:26 +02:00
|
|
|
}
|
|
|
|
try {
|
2020-10-01 12:51:57 +02:00
|
|
|
isPublisher.publishPlugins(availablePlugins);
|
2020-10-13 15:33:26 +02:00
|
|
|
}catch (Exception e) {
|
2020-10-15 14:03:24 +02:00
|
|
|
if(isPublisher instanceof RestISPublisher) {
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.warn("Unable to create RunningPlugin in context {}. {}", context, e.getMessage());
|
2020-10-15 14:03:24 +02:00
|
|
|
} else {
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.error("Unable to create ServiceEndpoint in context {}. The Service will be aborted", context, e);
|
2020-10-14 14:57:33 +02:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2020-10-01 12:51:57 +02:00
|
|
|
}
|
2020-10-13 15:33:26 +02:00
|
|
|
|
2017-02-10 16:57:10 +01:00
|
|
|
}
|
2020-10-13 15:33:26 +02:00
|
|
|
|
2017-02-10 16:57:10 +01:00
|
|
|
|
|
|
|
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",
|
2020-12-02 11:22:48 +01:00
|
|
|
SmartExecutorPersistenceConnector.class.getSimpleName(), context, e);
|
2016-09-22 11:31:05 +02:00
|
|
|
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 {
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.debug("Going to get Orphan Scheduled Tasks in scope {}", context);
|
2020-09-30 11:19:49 +02:00
|
|
|
|
2021-05-27 09:28:36 +02:00
|
|
|
List<ScheduledTask> gotScheduledTasks = smartExecutorPersistenceConnector.getScheduledTasks(pluginManager.getAvailablePlugins().keySet());
|
|
|
|
List<ScheduledTask> scheduledTasks = new ArrayList<>();
|
|
|
|
for(ScheduledTask scheduledTask : gotScheduledTasks) {
|
|
|
|
if(smartExecutorPersistenceConnector.isOrphan(scheduledTask, true)) {
|
|
|
|
scheduledTasks.add(scheduledTask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 14:38:46 +02:00
|
|
|
if(scheduledTasks.size()==0){
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.debug("No Orphan Scheduled Tasks this instance can take in charge in scope {}", context);
|
2017-05-25 14:38:46 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.debug("({}) Someone else is going to take in charge the scheduled task {}. Skipping.", context, 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 {
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.info("({}) Going to schedule an already scheduled task with the following parameters {}", context,
|
2019-09-24 14:59:25 +02:00
|
|
|
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 {
|
2020-09-30 11:19:49 +02:00
|
|
|
ContextUtility.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) {
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.error("({}) Error while trying to relaunch scheduled task.", context, e);
|
2017-02-10 16:57:10 +01:00
|
|
|
try {
|
|
|
|
smartExecutorPersistenceConnector.addScheduledTask(scheduledTask);
|
|
|
|
} catch (Exception ex) {
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.error("({}) Unable to add back scheduled task {}", context, 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) {
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.error("Unable to get Orphan Scheduled Tasksfor scope {}.", context, 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"
|
2020-12-02 11:22:48 +01:00
|
|
|
+ "Smart Executor Started Successfully on context {}\n"
|
|
|
|
+ "-------------------------------------------------------", context);
|
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
|
2020-10-14 11:25:52 +02:00
|
|
|
public void onShutdown(){
|
2016-09-22 11:31:05 +02:00
|
|
|
|
|
|
|
|
2015-08-31 17:02:11 +02:00
|
|
|
logger.trace(
|
2015-02-12 14:40:41 +01:00
|
|
|
"\n-------------------------------------------------------\n"
|
2020-12-02 11:22:48 +01:00
|
|
|
+ "Smart Executor is Stopping on context {}\n"
|
2016-09-22 11:31:05 +02:00
|
|
|
+ "-------------------------------------------------------",
|
2020-12-02 11:22:48 +01:00
|
|
|
ContextUtility.getCurrentContext());
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2017-05-25 14:38:46 +02:00
|
|
|
|
|
|
|
SmartExecutorScheduler scheduler;
|
|
|
|
try {
|
|
|
|
scheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
|
|
|
|
scheduler.stopAll();
|
2020-12-02 11:22:48 +01:00
|
|
|
SmartExecutorSchedulerFactory.removeCurrentSmartExecutorScheduler();
|
2017-05-25 14:38:46 +02:00
|
|
|
} catch (SchedulerException e) {
|
|
|
|
logger.error("", e);
|
|
|
|
}
|
2015-02-06 10:35:18 +01:00
|
|
|
|
2020-09-30 11:19:49 +02:00
|
|
|
ApplicationContext applicationContext = ContextProvider.get();
|
2020-10-01 12:51:57 +02:00
|
|
|
List<ISPublisher> isPublishers = ISPublisher.getISPublishers(applicationContext);
|
|
|
|
for(ISPublisher isPublisher : isPublishers) {
|
|
|
|
try {
|
|
|
|
isPublisher.unpublishPlugins(false);
|
|
|
|
}catch (Exception e) {
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.error("unable to unpublish plugins from IS using {}", isPublisher.getClass().getName());
|
2020-10-01 12:51:57 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 14:38:46 +02:00
|
|
|
|
2016-09-22 11:31:05 +02:00
|
|
|
try {
|
2020-12-02 11:22:48 +01:00
|
|
|
SmartExecutorPersistenceFactory.closeCurrentPersistenceConnector();
|
2017-05-25 14:38:46 +02:00
|
|
|
} catch (Throwable e) {
|
2020-12-02 11:22:48 +01:00
|
|
|
logger.error("Unable to correctly close {} for context {}",
|
2016-09-22 11:31:05 +02:00
|
|
|
SmartExecutorPersistenceConnector.class.getSimpleName(),
|
2020-12-02 11:22:48 +01:00
|
|
|
ContextUtility.getCurrentContext(), 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"
|
2020-12-02 11:22:48 +01:00
|
|
|
+ "Smart Executor Stopped Successfully on context {}\n"
|
2016-09-22 11:31:05 +02:00
|
|
|
+ "-------------------------------------------------------",
|
2020-12-02 11:22:48 +01:00
|
|
|
ContextUtility.getCurrentContext());
|
2015-02-06 10:35:18 +01:00
|
|
|
}
|
|
|
|
}
|