From ef3d949eb75698c3de5a928e7b4fac90e77e4ad9 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 1 Sep 2015 15:57:44 +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@117760 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../executor/SmartExecutorInitalizator.java | 18 +++++++-- .../{Parser.java => ConfiguredTasks.java} | 37 +++++++++++++---- .../configuration/JSONLaunchParameter.java | 40 +++++++++++++++++++ .../executor/configuration/ParserTest.java | 8 +++- 4 files changed, 90 insertions(+), 13 deletions(-) rename src/main/java/org/gcube/vremanagement/executor/configuration/{Parser.java => ConfiguredTasks.java} (61%) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitalizator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitalizator.java index c42f17c..acb65cd 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitalizator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitalizator.java @@ -36,7 +36,7 @@ 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.Parser; +import org.gcube.vremanagement.executor.configuration.ConfiguredTasks; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; import org.gcube.vremanagement.executor.plugin.PluginDeclaration; import org.gcube.vremanagement.executor.pluginmanager.PluginManager; @@ -73,6 +73,16 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler { */ protected static SmartExecutorScheduler smartExecutorScheduler; + + protected static ConfiguredTasks configuredTasks; + + /** + * @return the configuredTasks + */ + public static ConfiguredTasks getConfiguredTasks() { + return configuredTasks; + } + /** * @return the ctx */ @@ -351,11 +361,11 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler { logger.trace("Going to Run Configured Tasks"); try { - Parser taskConfiguration = new Parser(ctx.persistence().location()); - List configuredTasks = taskConfiguration.getConfiguredTasks(); + configuredTasks = new ConfiguredTasks(ctx.persistence().location()); + List configuredTaskList = configuredTasks.getConfiguredTasks(); SmartExecutorImpl smartExecutorImpl = new SmartExecutorImpl(); - for(LaunchParameter parameter : configuredTasks){ + for(LaunchParameter parameter : configuredTaskList){ try { smartExecutorImpl.launch(parameter); } catch (Exception e) { diff --git a/src/main/java/org/gcube/vremanagement/executor/configuration/Parser.java b/src/main/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasks.java similarity index 61% rename from src/main/java/org/gcube/vremanagement/executor/configuration/Parser.java rename to src/main/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasks.java index 01ef3dd..83dd819 100644 --- a/src/main/java/org/gcube/vremanagement/executor/configuration/Parser.java +++ b/src/main/java/org/gcube/vremanagement/executor/configuration/ConfiguredTasks.java @@ -3,12 +3,15 @@ */ package org.gcube.vremanagement.executor.configuration; +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.PrintWriter; import java.text.ParseException; import java.util.ArrayList; import java.util.List; -import org.gcube.vremanagement.executor.api.types.LaunchParameter; +import org.apache.commons.io.FileUtils; import org.gcube.vremanagement.executor.api.types.Scheduling; import org.gcube.vremanagement.executor.utils.IOUtility; import org.json.JSONArray; @@ -21,12 +24,12 @@ import org.slf4j.LoggerFactory; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -public class Parser { +public class ConfiguredTasks { /** * Logger */ - private static Logger logger = LoggerFactory.getLogger(Parser.class); + private static Logger logger = LoggerFactory.getLogger(ConfiguredTasks.class); protected String configurationFileLocation; protected List configuredTasks; @@ -34,7 +37,7 @@ public class Parser { public static final String CONFIG_TASK_FILENAME = "definedTasks.json"; - public Parser(String location) throws IOException, JSONException { + public ConfiguredTasks(String location) throws IOException, JSONException { this.configurationFileLocation = location; this.configuredTasks = new ArrayList(); this.configuredTasks = retriveConfiguredTask(); @@ -44,9 +47,14 @@ public class Parser { return new JSONScheduling(jsonObject); } + + protected static String configurationFileName(String configurationFileLocation){ + return configurationFileLocation + "/" + CONFIG_TASK_FILENAME; + } + protected List retriveConfiguredTask() throws IOException, JSONException { - String configuredTasksDefinition = IOUtility.readFile(configurationFileLocation + "/" + CONFIG_TASK_FILENAME); + String configuredTasksDefinition = IOUtility.readFile(configurationFileName(configurationFileLocation)); List tasks = new ArrayList(); JSONArray jsonArray = new JSONArray(configuredTasksDefinition); @@ -63,8 +71,23 @@ public class Parser { return tasks; } - public void addLaunch(LaunchParameter parameter) throws ParseException { - configuredTasks.add(new JSONLaunchParameter(parameter)); + 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); + String fileName = configurationFileName(configurationFileLocation); + + JSONArray jsonArray = new JSONArray(); + for(JSONLaunchParameter jsonLaunchParameter : configuredTasks){ + jsonArray.put(jsonLaunchParameter.toJSON()); + } + String jsonArrayString = jsonArray.toString(); + emptyConfigurationFile(fileName); + FileUtils.writeStringToFile(new File(fileName), jsonArrayString); } /** diff --git a/src/main/java/org/gcube/vremanagement/executor/configuration/JSONLaunchParameter.java b/src/main/java/org/gcube/vremanagement/executor/configuration/JSONLaunchParameter.java index 85f2dac..65b2d36 100644 --- a/src/main/java/org/gcube/vremanagement/executor/configuration/JSONLaunchParameter.java +++ b/src/main/java/org/gcube/vremanagement/executor/configuration/JSONLaunchParameter.java @@ -5,8 +5,10 @@ package org.gcube.vremanagement.executor.configuration; import java.text.ParseException; import java.util.HashMap; +import java.util.Map; import org.gcube.vremanagement.executor.api.types.LaunchParameter; +import org.gcube.vremanagement.executor.api.types.Scheduling; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -28,6 +30,24 @@ public class JSONLaunchParameter extends LaunchParameter { @SuppressWarnings("unused") private JSONLaunchParameter(){} + public JSONLaunchParameter(String pluginName, Map inputs) { + super(pluginName, inputs); + } + + public JSONLaunchParameter(String pluginName, Map inputs, boolean persist) { + super(pluginName, inputs, persist); + } + + public JSONLaunchParameter(String pluginName, Map inputs, Scheduling scheduling) throws ParseException { + super(pluginName, inputs, scheduling); + this.scheduling = new JSONScheduling(scheduling); + } + + public JSONLaunchParameter(String pluginName, Map inputs, boolean persist, Scheduling scheduling) throws ParseException { + super(pluginName, inputs, persist, scheduling); + this.scheduling = new JSONScheduling(scheduling); + } + public JSONLaunchParameter(LaunchParameter parameter) throws ParseException { super(parameter.getPluginName(), parameter.getInputs(), parameter.isPersist()); this.scheduling = new JSONScheduling(parameter.getScheduling()); @@ -71,4 +91,24 @@ public class JSONLaunchParameter extends LaunchParameter { public void setScheduling(JSONScheduling scheduling) { this.scheduling = scheduling; } + + public JSONObject toJSON() throws JSONException { + JSONObject obj = new JSONObject(); + obj.put(PLUGIN_NAME, pluginName); + + JSONObject inputJsonObject = new JSONObject(); + for(String id : inputs.keySet()){ + inputJsonObject.put(id, inputs.get(id)); + } + obj.put(INPUTS, inputJsonObject); + + if(scheduling!=null){ + obj.put(SCHEDULING, scheduling.toJSON()); + } + + obj.put(PERSIST, true); + + return obj; + } + } 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 fd7297e..b62def9 100644 --- a/src/test/java/org/gcube/vremanagement/executor/configuration/ParserTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/configuration/ParserTest.java @@ -5,6 +5,7 @@ package org.gcube.vremanagement.executor.configuration; import java.io.File; import java.io.IOException; +import java.text.ParseException; import java.util.List; import java.util.Map; @@ -24,10 +25,10 @@ public class ParserTest { public static final String TEST = "test"; @Test - public void testLaunchConfiguredTask() throws IOException, JSONException { + public void testLaunchConfiguredTask() throws IOException, JSONException, ParseException { String location = new File(".").getAbsolutePath(); location = location + "/src/test/resources"; - Parser parser = new Parser(location); + ConfiguredTasks parser = new ConfiguredTasks(location); List configuredTasks = parser.getConfiguredTasks(); Assert.assertEquals(3, configuredTasks.size()); @@ -60,5 +61,8 @@ public class ParserTest { Assert.assertEquals(3, inputs.get(TEST)); Assert.assertEquals(null, parameter.getScheduling()); Assert.assertEquals(true, parameter.isPersist()); + + JSONLaunchParameter added = new JSONLaunchParameter(HelloWorldPluginDeclaration.NAME, inputs, true); + parser.addLaunch(added); } }