From c6df0384ca318e40ac9df1514614acad4c6bcf63 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 25 Sep 2015 09:20:54 +0000 Subject: [PATCH] refs #521: Support Unscheduling of repetitive task on SmartExecutor https://support.d4science.org/issues/521 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor@119021 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../executor/SmartExecutorImpl.java | 20 ++++-- .../executor/SmartExecutorInitalizator.java | 11 ++- .../configuration/LaunchConfiguration.java | 23 +++++++ .../FileConfiguredTasks.java} | 68 +++++++++++++------ .../{ => jsonbased}/JSONLaunchParameter.java | 2 +- .../{ => jsonbased}/JSONScheduling.java | 2 +- .../SchedulePersistenceException.java | 32 +++++++++ .../scheduler/SmartExecutorScheduler.java | 14 ++-- ...rserTest.java => ConfiguredTasksTest.java} | 28 ++++---- 9 files changed, 153 insertions(+), 47 deletions(-) create mode 100644 src/main/java/org/gcube/vremanagement/executor/configuration/LaunchConfiguration.java rename src/main/java/org/gcube/vremanagement/executor/configuration/{ConfiguredTasks.java => jsonbased/FileConfiguredTasks.java} (57%) rename src/main/java/org/gcube/vremanagement/executor/configuration/{ => jsonbased}/JSONLaunchParameter.java (97%) rename src/main/java/org/gcube/vremanagement/executor/configuration/{ => jsonbased}/JSONScheduling.java (97%) create mode 100644 src/main/java/org/gcube/vremanagement/executor/exception/SchedulePersistenceException.java rename src/test/java/org/gcube/vremanagement/executor/configuration/{ParserTest.java => ConfiguredTasksTest.java} (72%) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorImpl.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorImpl.java index 248a779..30ec5a0 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorImpl.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorImpl.java @@ -11,6 +11,7 @@ import org.gcube.vremanagement.executor.exception.InputsNullException; import org.gcube.vremanagement.executor.exception.LaunchException; import org.gcube.vremanagement.executor.exception.PluginInstanceNotFoundException; import org.gcube.vremanagement.executor.exception.PluginNotFoundException; +import org.gcube.vremanagement.executor.exception.SchedulerNotFoundException; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; import org.gcube.vremanagement.executor.plugin.PluginState; import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler; @@ -48,10 +49,21 @@ public class SmartExecutorImpl implements SmartExecutor { /**{@inheritDoc}*/ @Override - public void unSchedule(String executionIdentifier) throws SchedulerException { - SmartExecutorScheduler smartExecutorScheduler = SmartExecutorInitalizator.getSmartExecutorScheduler(); - UUID uuid = UUID.fromString(executionIdentifier); - smartExecutorScheduler.stop(uuid); + public boolean unSchedule(String executionIdentifier) { + boolean currentStopped = true; + try { + SmartExecutorScheduler smartExecutorScheduler = SmartExecutorInitalizator.getSmartExecutorScheduler(); + UUID uuid = UUID.fromString(executionIdentifier); + smartExecutorScheduler.stop(uuid); + } catch (SchedulerNotFoundException snfe) { + currentStopped = true; + } catch(SchedulerException e){ + currentStopped = false; + } + + // TODO Remove from configuration + + return currentStopped; } /**{@inheritDoc}*/ diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitalizator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitalizator.java index acb65cd..35490d6 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitalizator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitalizator.java @@ -35,8 +35,7 @@ import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent; import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler; import org.gcube.vremanagement.executor.api.types.LaunchParameter; -import org.gcube.vremanagement.executor.configuration.JSONLaunchParameter; -import org.gcube.vremanagement.executor.configuration.ConfiguredTasks; +import org.gcube.vremanagement.executor.configuration.jsonbased.FileConfiguredTasks; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; import org.gcube.vremanagement.executor.plugin.PluginDeclaration; import org.gcube.vremanagement.executor.pluginmanager.PluginManager; @@ -74,12 +73,12 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler { protected static SmartExecutorScheduler smartExecutorScheduler; - protected static ConfiguredTasks configuredTasks; + protected static FileConfiguredTasks configuredTasks; /** * @return the configuredTasks */ - public static ConfiguredTasks getConfiguredTasks() { + public static FileConfiguredTasks getConfiguredTasks() { return configuredTasks; } @@ -361,8 +360,8 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler { logger.trace("Going to Run Configured Tasks"); try { - configuredTasks = new ConfiguredTasks(ctx.persistence().location()); - List configuredTaskList = configuredTasks.getConfiguredTasks(); + configuredTasks = new FileConfiguredTasks(ctx.persistence().location()); + List configuredTaskList = configuredTasks.getConfiguredTasks(); SmartExecutorImpl smartExecutorImpl = new SmartExecutorImpl(); for(LaunchParameter parameter : configuredTaskList){ diff --git a/src/main/java/org/gcube/vremanagement/executor/configuration/LaunchConfiguration.java b/src/main/java/org/gcube/vremanagement/executor/configuration/LaunchConfiguration.java new file mode 100644 index 0000000..a734822 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/executor/configuration/LaunchConfiguration.java @@ -0,0 +1,23 @@ +/** + * + */ +package org.gcube.vremanagement.executor.configuration; + +import java.util.List; + +import org.gcube.vremanagement.executor.api.types.LaunchParameter; +import org.gcube.vremanagement.executor.exception.SchedulePersistenceException; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public interface LaunchConfiguration { + + public List getScheduledLaunch() throws SchedulePersistenceException; + + public void addLaunch(LaunchParameter parameter) throws SchedulePersistenceException; + + public void removeLaunch(LaunchParameter parameter)throws SchedulePersistenceException; + +} diff --git a/src/main/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasks.java b/src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/FileConfiguredTasks.java similarity index 57% rename from src/main/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasks.java rename to src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/FileConfiguredTasks.java index 3ad2937..40a06fc 100644 --- a/src/main/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasks.java +++ b/src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/FileConfiguredTasks.java @@ -1,7 +1,7 @@ /** * */ -package org.gcube.vremanagement.executor.configuration; +package org.gcube.vremanagement.executor.configuration.jsonbased; import java.io.File; import java.io.FileNotFoundException; @@ -12,7 +12,10 @@ import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; +import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.api.types.Scheduling; +import org.gcube.vremanagement.executor.configuration.LaunchConfiguration; +import org.gcube.vremanagement.executor.exception.SchedulePersistenceException; import org.gcube.vremanagement.executor.utils.IOUtility; import org.json.JSONArray; import org.json.JSONException; @@ -24,22 +27,23 @@ import org.slf4j.LoggerFactory; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public class ConfiguredTasks { +public class FileConfiguredTasks implements LaunchConfiguration { /** * Logger */ - private static Logger logger = LoggerFactory.getLogger(ConfiguredTasks.class); + private static Logger logger = LoggerFactory.getLogger(FileConfiguredTasks.class); protected String configurationFileLocation; - protected List configuredTasks; + protected List configuredTasks; + public static final String CONFIG_TASK_FILENAME = "definedTasks.json"; - public ConfiguredTasks(String location) throws IOException, JSONException { + public FileConfiguredTasks(String location) throws IOException, JSONException { this.configurationFileLocation = location; - this.configuredTasks = new ArrayList(); + this.configuredTasks = new ArrayList(); this.configuredTasks = retriveConfiguredTask(); } @@ -53,11 +57,11 @@ public class ConfiguredTasks { return configurationFileLocation + "/" + CONFIG_TASK_FILENAME; } - protected List retriveConfiguredTask() + public List retriveConfiguredTask() throws IOException, JSONException { String configuredTasksDefinition = IOUtility.readFile(configurationFileName(configurationFileLocation)); - List tasks = new ArrayList(); + List tasks = new ArrayList(); JSONArray jsonArray = new JSONArray(configuredTasksDefinition); for(int i=0; i getScheduledLaunch() + throws SchedulePersistenceException { + return null; + } + /** * @return the configuredTasks */ - public List getConfiguredTasks() { + public List getConfiguredTasks() { return configuredTasks; } /** * @param configuredTasks the configuredTasks to set */ - public void setConfiguredTasks(List configuredTasks) { + public void setConfiguredTasks(List configuredTasks) { this.configuredTasks = configuredTasks; } - - } diff --git a/src/main/java/org/gcube/vremanagement/executor/configuration/JSONLaunchParameter.java b/src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/JSONLaunchParameter.java similarity index 97% rename from src/main/java/org/gcube/vremanagement/executor/configuration/JSONLaunchParameter.java rename to src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/JSONLaunchParameter.java index 65b2d36..1725089 100644 --- a/src/main/java/org/gcube/vremanagement/executor/configuration/JSONLaunchParameter.java +++ b/src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/JSONLaunchParameter.java @@ -1,7 +1,7 @@ /** * */ -package org.gcube.vremanagement.executor.configuration; +package org.gcube.vremanagement.executor.configuration.jsonbased; import java.text.ParseException; import java.util.HashMap; diff --git a/src/main/java/org/gcube/vremanagement/executor/configuration/JSONScheduling.java b/src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/JSONScheduling.java similarity index 97% rename from src/main/java/org/gcube/vremanagement/executor/configuration/JSONScheduling.java rename to src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/JSONScheduling.java index 362e06f..f219138 100644 --- a/src/main/java/org/gcube/vremanagement/executor/configuration/JSONScheduling.java +++ b/src/main/java/org/gcube/vremanagement/executor/configuration/jsonbased/JSONScheduling.java @@ -1,7 +1,7 @@ /** * */ -package org.gcube.vremanagement.executor.configuration; +package org.gcube.vremanagement.executor.configuration.jsonbased; import java.text.ParseException; diff --git a/src/main/java/org/gcube/vremanagement/executor/exception/SchedulePersistenceException.java b/src/main/java/org/gcube/vremanagement/executor/exception/SchedulePersistenceException.java new file mode 100644 index 0000000..3e4bc34 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/executor/exception/SchedulePersistenceException.java @@ -0,0 +1,32 @@ +/** + * + */ +package org.gcube.vremanagement.executor.exception; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class SchedulePersistenceException extends Exception { + + /** + * Generated Serial Version UID + */ + private static final long serialVersionUID = -3261726979079756047L; + + public SchedulePersistenceException() { + super(); + } + + public SchedulePersistenceException(String message) { + super(message); + } + + public SchedulePersistenceException(Throwable throwable){ + super(throwable); + } + + public SchedulePersistenceException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java index 0f58790..7516233 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java @@ -10,11 +10,13 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import org.gcube.vremanagement.executor.SmartExecutorInitalizator; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.api.types.Scheduling; import org.gcube.vremanagement.executor.exception.InputsNullException; import org.gcube.vremanagement.executor.exception.LaunchException; import org.gcube.vremanagement.executor.exception.PluginNotFoundException; +import org.gcube.vremanagement.executor.exception.SchedulePersistenceException; import org.gcube.vremanagement.executor.exception.SchedulerNotFoundException; import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; @@ -97,8 +99,8 @@ public class SmartExecutorScheduler { } - protected Scheduler reallySchedule(final UUID uuid, Scheduler scheduler, LaunchParameter parameter) throws LaunchException, - InputsNullException, PluginNotFoundException { + protected Scheduler reallySchedule(final UUID uuid, Scheduler scheduler, + LaunchParameter parameter) throws LaunchException, InputsNullException, PluginNotFoundException { JobKey jobKey = new JobKey(uuid.toString()); JobDetail jobDetail = JobBuilder.newJob(SmartExecutorJob.class). @@ -130,11 +132,15 @@ public class SmartExecutorScheduler { triggerBuilder.endAt(triggerEndTime); } - + try { + SmartExecutorInitalizator.getConfiguredTasks().addLaunch(parameter); + } catch (SchedulePersistenceException e) { + logger.error("Unable to persist the scheduling", e.getCause()); + } + } else { triggerBuilder.startNow(); } - try { SmartExecutorJobListener sejl = new SmartExecutorJobListener(); diff --git a/src/test/java/org/gcube/vremanagement/executor/configuration/ParserTest.java b/src/test/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasksTest.java similarity index 72% rename from src/test/java/org/gcube/vremanagement/executor/configuration/ParserTest.java rename to src/test/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasksTest.java index 2f4a841..f134d7e 100644 --- a/src/test/java/org/gcube/vremanagement/executor/configuration/ParserTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasksTest.java @@ -12,7 +12,11 @@ import java.util.Map; import org.acme.HelloWorldPlugin; import org.acme.HelloWorldPluginDeclaration; +import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.api.types.Scheduling; +import org.gcube.vremanagement.executor.configuration.jsonbased.FileConfiguredTasks; +import org.gcube.vremanagement.executor.configuration.jsonbased.JSONLaunchParameter; +import org.gcube.vremanagement.executor.exception.SchedulePersistenceException; import org.json.JSONException; import org.junit.Assert; import org.junit.Test; @@ -21,15 +25,15 @@ import org.junit.Test; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public class ParserTest { +public class ConfiguredTasksTest { public static final String TEST = "test"; - public void checkOriginal(ConfiguredTasks parser, int size){ - List configuredTasks = parser.getConfiguredTasks(); + public void checkOriginal(FileConfiguredTasks parser, int size){ + List configuredTasks = parser.getConfiguredTasks(); Assert.assertEquals(size, configuredTasks.size()); - JSONLaunchParameter parameter = configuredTasks.get(0); + JSONLaunchParameter parameter = (JSONLaunchParameter) configuredTasks.get(0); Assert.assertEquals(HelloWorldPluginDeclaration.NAME, parameter.getPluginName()); Map inputs = parameter.getInputs(); Assert.assertEquals(1000, inputs.get(HelloWorldPlugin.SLEEP_TIME)); @@ -37,7 +41,7 @@ public class ParserTest { Assert.assertEquals(null, parameter.getScheduling()); Assert.assertEquals(true, parameter.isPersist()); - parameter = configuredTasks.get(1); + parameter = (JSONLaunchParameter) configuredTasks.get(1); Assert.assertEquals(parameter.getPluginName(), HelloWorldPluginDeclaration.NAME); inputs = parameter.getInputs(); Assert.assertEquals(1000, inputs.get(HelloWorldPlugin.SLEEP_TIME)); @@ -51,7 +55,7 @@ public class ParserTest { Assert.assertEquals(false, scheduling.mustPreviousExecutionsCompleted()); Assert.assertEquals(true, parameter.isPersist()); - parameter = configuredTasks.get(2); + parameter = (JSONLaunchParameter) configuredTasks.get(2); Assert.assertEquals(parameter.getPluginName(), HelloWorldPluginDeclaration.NAME); inputs = parameter.getInputs(); Assert.assertEquals(1000, inputs.get(HelloWorldPlugin.SLEEP_TIME)); @@ -63,10 +67,10 @@ public class ParserTest { @Test - public void testLaunchConfiguredTask() throws IOException, JSONException, ParseException { + public void testLaunchConfiguredTask() throws SchedulePersistenceException, IOException, JSONException, ParseException { String location = new File(".").getAbsolutePath(); location = location + "/src/test/resources"; - ConfiguredTasks parser = new ConfiguredTasks(location); + FileConfiguredTasks parser = new FileConfiguredTasks(location); checkOriginal(parser, 3); @@ -76,11 +80,11 @@ public class ParserTest { JSONLaunchParameter added = new JSONLaunchParameter(HelloWorldPluginDeclaration.NAME, inputs, true); parser.addLaunch(added); - parser = new ConfiguredTasks(location); + parser = new FileConfiguredTasks(location); checkOriginal(parser, 4); - List configuredTasks = parser.getConfiguredTasks(); - JSONLaunchParameter parameter = configuredTasks.get(3); + List configuredTasks = parser.getConfiguredTasks(); + JSONLaunchParameter parameter = (JSONLaunchParameter) configuredTasks.get(3); Assert.assertEquals(parameter.getPluginName(), HelloWorldPluginDeclaration.NAME); inputs = parameter.getInputs(); Assert.assertEquals(1000, inputs.get(HelloWorldPlugin.SLEEP_TIME)); @@ -90,7 +94,7 @@ public class ParserTest { parser.removeLaunch(parameter); - parser = new ConfiguredTasks(location); + parser = new FileConfiguredTasks(location); checkOriginal(parser, 3); }