Skip to content

Commit

Permalink
[simple-api] jni helper function for external monitoring
Browse files Browse the repository at this point in the history
- New startMonitorableJob method to start and retrieve a job object for
external monitoring.
This method get a map of java objects to ensure parameters types are
correctly converted to pipeline compatible string values.
- _startJob now returns the job it has created and launched
- Fix uri-encoded result file names.
  • Loading branch information
NPavie committed Nov 30, 2023
1 parent e8bb3bd commit 8c50d09
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/main/java/SimpleAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@
import org.osgi.service.component.annotations.ReferencePolicy;

/**
* A simplified Java API consisting of a {@link #startJob()} method that starts a job based on a
* script name and a list of options, and {@link #getNewMessages()} and {@link #getLastJobStatus()}
* methods. It is used to build a simple Java CLI (see the {@link #main()} method). The simplified
* API also makes it easier to bridge with other programming languages using JNI.
* A simplified Java API consisting of a
* {@link #startJob(String, Map<String,? extends Iterable<String>>)} method
* that starts a job based on a script name and a list of options,
* and {@link #getNewMessages()} and {@link #getLastJobStatus()} methods.
* It is used to build a simple Java CLI (see the {@link #main(String[])} method).
* For JNI usage, the API provides a
* {@link #startMonitorableJob(String, Map<String,Object>)} that returns a
* {@link CommandLineJob} object for external monitoring.
*/
@Component(
name = "SimpleAPI",
Expand Down Expand Up @@ -71,7 +75,7 @@ public void setJobFactory(JobFactory jobFactory) {
this.jobFactory = jobFactory;
}

private void _startJob(String scriptName, Map<String,? extends Iterable<String>> options) throws IllegalArgumentException, FileNotFoundException {
private CommandLineJob _startJob(String scriptName, Map<String,? extends Iterable<String>> options) throws IllegalArgumentException, FileNotFoundException {
ScriptService<?> scriptService = scriptRegistry.getScript(scriptName);
if (scriptService == null)
throw new IllegalArgumentException(scriptName + " script not found");
Expand All @@ -90,12 +94,43 @@ private void _startJob(String scriptName, Map<String,? extends Iterable<String>>
);
job.getMonitor().getStatusUpdates().listen(s -> updateJobStatus(s));
new Thread(job).start();
return job;
}

public static void startJob(String scriptName, Map<String,? extends Iterable<String>> options) throws IllegalArgumentException, FileNotFoundException {
getInstance()._startJob(scriptName, options);
}

/**
* Start a new job that can be externally monitored.
* @param scriptName the script to launch.
* @param options the Map of script parameters name and value to use.
* Values are passed as java object that will be converted to
* string.
* @return a CommandLineJob that can be monitored with
* {@link CommandLineJob#getStatus()} and
* {@link CommandLineJob#getMonitor()}
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
public static CommandLineJob startMonitorableJob(String scriptName, Map<String,Object> options) throws IllegalArgumentException, FileNotFoundException {
Map<String,ArrayList<String>> stringifiedOptions = new HashMap<String, ArrayList<String>>();
for (Map.Entry<String,Object> e : options.entrySet()){
Object value = e.getValue();
ArrayList<String> subStringValue = new ArrayList<>();
if(value instanceof List){
for (Object subValue : (List<Object>)value)
subStringValue.add(subValue.toString());
} else {
subStringValue.add(value.toString());
}
stringifiedOptions.put(e.getKey(), subStringValue);
}
return getInstance()._startJob(scriptName, stringifiedOptions);
}



/**
* Singleton thread safe instance of SimpleAPI.
*/
Expand Down Expand Up @@ -383,7 +418,7 @@ public void run() {
File f = new File(u);
if (u.toString().endsWith("/"))
for (JobResult r : job.getResults().getResults(port)) {
File dest = new File(f, r.strip().getIdx());
File dest = new File(u.resolve(r.strip().getIdx()));
if (dest.exists())
existingFiles.add(dest);
else
Expand Down

0 comments on commit 8c50d09

Please sign in to comment.