Skip to content

Commit

Permalink
Fixes #302 - Add ability to execute job using REST API (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Jul 16, 2024
1 parent b638fd7 commit 9051899
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
68 changes: 66 additions & 2 deletions rest/src/main/java/com/manorrock/sphynx/rest/JobResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import jakarta.ws.rs.Path;
import jakarta.enterprise.context.RequestScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
Expand All @@ -45,6 +47,7 @@
import static java.lang.System.Logger.Level.TRACE;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -178,12 +181,73 @@ public List<String> list() {
if (jobsDirectory.exists()) {
ArrayList<String> result = new ArrayList<>();
String[] names = jobsDirectory.list();
for (String name : names) {
result.add(name);
if (names.length > 0) {
result.addAll(Arrays.asList(names));
}
return result;
} else {
throw new WebApplicationException(INTERNAL_SERVER_ERROR);
}
}

/**
* Execute a job.
*
* @param name the name of the job to execute.
* @return the log name.
*/
@Path("{name}/execute")
@POST
public String execute(@PathParam("name") String name) {
/*
* Step 1 - Determine job directory.
*/
File jobDirectory = new File(baseDirectory, "jobs" + File.separator + name);

/*
* Step 2 - Determine script filename.
*/
File scriptFilename = new File(jobDirectory, "script" + File.separator + "run.sh");
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
scriptFilename = new File(jobDirectory, "script" + File.separator + "run.cmd");
}

/**
* Step 4 - Create command array.
*/
ArrayList<String> commands = new ArrayList<>();
commands.add("sh");
commands.add(scriptFilename.getAbsolutePath());

/**
* Step 5 - Determine work directory.
*/
File workDirectory = new File(jobDirectory, "work");

/*
* Step 6 - Determine the log file.
*/
File logFile = new File(jobDirectory, "logs" + File.separator + System.currentTimeMillis() + ".log");
if (!logFile.getParentFile().exists()) {
if (!logFile.getParentFile().mkdirs()) {
LOGGER.log(ERROR, "Unable to create logs directory");
throw new WebApplicationException(INTERNAL_SERVER_ERROR);
}
}

try {
/*
* Step 7 - Create process.
*/
new ProcessBuilder()
.command(commands)
.directory(workDirectory)
.start();
} catch (IOException ex) {
LOGGER.log(ERROR, "I/O error occurred: ", ex);
throw new WebApplicationException(INTERNAL_SERVER_ERROR);
}

return logFile.getName().substring(0, logFile.getName().indexOf(".log"));
}
}
20 changes: 15 additions & 5 deletions rest/src/test/java/it/JobIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/


import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -42,11 +40,11 @@

/**
* The integration tests for Jobs.
*
*
* @author Manfred Riem ([email protected])
*/
class JobIT {

@Test
void testCreate() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand All @@ -59,7 +57,7 @@ void testCreate() throws Exception {
System.out.println(response.body());
assertTrue(response.body().trim().length() > 0);
}

@Test
void testList() throws Exception {
HttpClient client = HttpClient.newHttpClient();
Expand All @@ -70,4 +68,16 @@ void testList() throws Exception {
System.out.println(response.body());
assertTrue(response.body().trim().length() > 0);
}

@Test
void testExecute() throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest
.newBuilder(new URI("http://localhost:8080/rest/job/test/execute"))
.POST(BodyPublishers.noBody())
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println(response.body());
assertTrue(response.body().trim().length() > 0);
}
}

0 comments on commit 9051899

Please sign in to comment.