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
This commit is contained in:
parent
4637d76de7
commit
ef3d949eb7
|
@ -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<JSONLaunchParameter> configuredTasks = taskConfiguration.getConfiguredTasks();
|
||||
configuredTasks = new ConfiguredTasks(ctx.persistence().location());
|
||||
List<JSONLaunchParameter> configuredTaskList = configuredTasks.getConfiguredTasks();
|
||||
|
||||
SmartExecutorImpl smartExecutorImpl = new SmartExecutorImpl();
|
||||
for(LaunchParameter parameter : configuredTasks){
|
||||
for(LaunchParameter parameter : configuredTaskList){
|
||||
try {
|
||||
smartExecutorImpl.launch(parameter);
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -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<JSONLaunchParameter> 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<JSONLaunchParameter>();
|
||||
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<JSONLaunchParameter> retriveConfiguredTask() throws IOException, JSONException {
|
||||
|
||||
String configuredTasksDefinition = IOUtility.readFile(configurationFileLocation + "/" + CONFIG_TASK_FILENAME);
|
||||
String configuredTasksDefinition = IOUtility.readFile(configurationFileName(configurationFileLocation));
|
||||
List<JSONLaunchParameter> tasks = new ArrayList<JSONLaunchParameter>();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
|
@ -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<String, Object> inputs) {
|
||||
super(pluginName, inputs);
|
||||
}
|
||||
|
||||
public JSONLaunchParameter(String pluginName, Map<String, Object> inputs, boolean persist) {
|
||||
super(pluginName, inputs, persist);
|
||||
}
|
||||
|
||||
public JSONLaunchParameter(String pluginName, Map<String, Object> inputs, Scheduling scheduling) throws ParseException {
|
||||
super(pluginName, inputs, scheduling);
|
||||
this.scheduling = new JSONScheduling(scheduling);
|
||||
}
|
||||
|
||||
public JSONLaunchParameter(String pluginName, Map<String, Object> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<JSONLaunchParameter> 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue