Skip to content

Commit

Permalink
Fixes #367 - Add ability to add a job to desktop client
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem committed Oct 10, 2024
1 parent 6e73cfa commit 1dc1ff7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,19 @@ public void initialize() {
public void onAdd(ActionEvent event) {
Job automation = new Job();
automation.setId(System.currentTimeMillis());
automation.setName("Untitled - " + System.currentTimeMillis());
jobsTableView.getItems().add(automation);
String name = "untitled-" + System.currentTimeMillis();
automation.setName(name);
int status = JobUtils.createJob(baseDirectory, name);
switch(status) {
case 0 -> {
jobsTableView.getItems().add(automation);
}
default -> {
Alert alert = new Alert(WARNING);
alert.setContentText("Unable to add job");
alert.showAndWait();
}
}
}

/**
Expand Down
73 changes: 73 additions & 0 deletions shared/src/main/java/com/manorrock/sphynx/shared/JobUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@

import java.io.File;
import java.io.IOException;
import static java.lang.System.Logger.Level.ERROR;
import static java.lang.System.Logger.Level.WARNING;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.UUID;

/**
* A utility class dealing with jobs.
Expand Down Expand Up @@ -72,4 +74,75 @@ public static int deleteJob(File baseDirectory, String name) {
}
return 0;
}

public static int createJob(File baseDirectory, String name) {
/*
* Step 1 - determine name.
*/
if (name == null || name.trim().equals("")) {
name = UUID.randomUUID().toString();
}

String script = null;

/*
* Step 2 - determine script.
*/
if (script == null) {
script = "";
}

/*
* Step 3 - create directory structure.
*/
if (!baseDirectory.exists()) {
if (!baseDirectory.mkdirs()) {
return 1;
}
}
File jobDirectory = new File(baseDirectory, "jobs" + File.separator + name);
if (jobDirectory.exists()) {
return 2;
} else {
if (!jobDirectory.mkdirs()) {
return 3;
}
}
File jobWorkDirectory = new File(jobDirectory, "work");
if (jobWorkDirectory.exists()) {
return 4;
} else {
if (!jobWorkDirectory.mkdirs()) {
return 5;
}
}
File executionsDirectory = new File(jobDirectory, "executions");
if (executionsDirectory.exists()) {
return 6;
} else {
if (!executionsDirectory.mkdirs()) {
return 7;
}
}

/*
* Step 4 - copy script into directory structure.
*/
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");
}
if (!scriptFilename.getParentFile().exists()) {
if (!scriptFilename.getParentFile().mkdirs()) {
return 8;
}
}
try {
Files.write(scriptFilename.toPath(), script.getBytes());
} catch (IOException ioe) {
LOGGER.log(ERROR, "Unable to write script to file");
return 9;
}
return 0;
}
}

0 comments on commit 1dc1ff7

Please sign in to comment.