From f2a8767cdae65832a17f5a231f976d76ffdf9015 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 2 Sep 2015 08:16:38 +0000 Subject: [PATCH] refs #508: Support Config File to run task at service startup https://support.d4science.org/issues/508 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor@117768 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../configuration/ConfiguredTasks.java | 25 ++++++++--- .../executor/configuration/ParserTest.java | 41 ++++++++++++++++--- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasks.java b/src/main/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasks.java index 83dd819..3ad2937 100644 --- a/src/main/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasks.java +++ b/src/main/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasks.java @@ -43,7 +43,8 @@ public class ConfiguredTasks { this.configuredTasks = retriveConfiguredTask(); } - protected Scheduling getScheduling(JSONObject jsonObject) throws JSONException, ParseException{ + protected Scheduling getScheduling(JSONObject jsonObject) + throws JSONException, ParseException{ return new JSONScheduling(jsonObject); } @@ -52,7 +53,8 @@ public class ConfiguredTasks { return configurationFileLocation + "/" + CONFIG_TASK_FILENAME; } - protected List retriveConfiguredTask() throws IOException, JSONException { + protected List retriveConfiguredTask() + throws IOException, JSONException { String configuredTasksDefinition = IOUtility.readFile(configurationFileName(configurationFileLocation)); List tasks = new ArrayList(); @@ -71,14 +73,14 @@ public class ConfiguredTasks { return tasks; } - protected void emptyConfigurationFile(String fileName) throws FileNotFoundException { + protected void emptyConfigurationFile(String fileName) + throws FileNotFoundException { PrintWriter writer = new PrintWriter(fileName); writer.print(""); writer.close(); } - public synchronized void addLaunch(JSONLaunchParameter parameter) throws ParseException, JSONException, IOException { - configuredTasks.add(parameter); + protected void writeOnConfigurationFile() throws JSONException, IOException{ String fileName = configurationFileName(configurationFileLocation); JSONArray jsonArray = new JSONArray(); @@ -90,6 +92,19 @@ public class ConfiguredTasks { FileUtils.writeStringToFile(new File(fileName), jsonArrayString); } + public synchronized void addLaunch(JSONLaunchParameter parameter) + throws ParseException, JSONException, IOException { + configuredTasks.add(parameter); + writeOnConfigurationFile(); + } + + + public synchronized void removeLaunch(JSONLaunchParameter parameter) + throws ParseException, JSONException, IOException { + configuredTasks.remove(parameter); + writeOnConfigurationFile(); + } + /** * @return the configuredTasks */ diff --git a/src/test/java/org/gcube/vremanagement/executor/configuration/ParserTest.java b/src/test/java/org/gcube/vremanagement/executor/configuration/ParserTest.java index b62def9..2f4a841 100644 --- a/src/test/java/org/gcube/vremanagement/executor/configuration/ParserTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/configuration/ParserTest.java @@ -6,6 +6,7 @@ package org.gcube.vremanagement.executor.configuration; import java.io.File; import java.io.IOException; import java.text.ParseException; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -24,13 +25,9 @@ public class ParserTest { public static final String TEST = "test"; - @Test - public void testLaunchConfiguredTask() throws IOException, JSONException, ParseException { - String location = new File(".").getAbsolutePath(); - location = location + "/src/test/resources"; - ConfiguredTasks parser = new ConfiguredTasks(location); + public void checkOriginal(ConfiguredTasks parser, int size){ List configuredTasks = parser.getConfiguredTasks(); - Assert.assertEquals(3, configuredTasks.size()); + Assert.assertEquals(size, configuredTasks.size()); JSONLaunchParameter parameter = configuredTasks.get(0); Assert.assertEquals(HelloWorldPluginDeclaration.NAME, parameter.getPluginName()); @@ -61,8 +58,40 @@ public class ParserTest { Assert.assertEquals(3, inputs.get(TEST)); Assert.assertEquals(null, parameter.getScheduling()); Assert.assertEquals(true, parameter.isPersist()); + } + + + + @Test + public void testLaunchConfiguredTask() throws IOException, JSONException, ParseException { + String location = new File(".").getAbsolutePath(); + location = location + "/src/test/resources"; + ConfiguredTasks parser = new ConfiguredTasks(location); + checkOriginal(parser, 3); + + Map inputs = new HashMap(); + inputs.put(HelloWorldPlugin.SLEEP_TIME, 1000); + inputs.put(TEST, 4); JSONLaunchParameter added = new JSONLaunchParameter(HelloWorldPluginDeclaration.NAME, inputs, true); parser.addLaunch(added); + + parser = new ConfiguredTasks(location); + checkOriginal(parser, 4); + + List configuredTasks = parser.getConfiguredTasks(); + JSONLaunchParameter parameter = configuredTasks.get(3); + Assert.assertEquals(parameter.getPluginName(), HelloWorldPluginDeclaration.NAME); + inputs = parameter.getInputs(); + Assert.assertEquals(1000, inputs.get(HelloWorldPlugin.SLEEP_TIME)); + Assert.assertEquals(4, inputs.get(TEST)); + Assert.assertEquals(null, parameter.getScheduling()); + Assert.assertEquals(true, parameter.isPersist()); + + parser.removeLaunch(parameter); + + parser = new ConfiguredTasks(location); + checkOriginal(parser, 3); + } }