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
This commit is contained in:
Luca Frosini 2015-09-02 08:16:38 +00:00
parent ef3d949eb7
commit f2a8767cda
2 changed files with 55 additions and 11 deletions

View File

@ -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<JSONLaunchParameter> retriveConfiguredTask() throws IOException, JSONException {
protected List<JSONLaunchParameter> retriveConfiguredTask()
throws IOException, JSONException {
String configuredTasksDefinition = IOUtility.readFile(configurationFileName(configurationFileLocation));
List<JSONLaunchParameter> tasks = new ArrayList<JSONLaunchParameter>();
@ -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
*/

View File

@ -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<JSONLaunchParameter> 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<String, Object> inputs = new HashMap<String, Object>();
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<JSONLaunchParameter> 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);
}
}