Add linux schedule management functions

This commit is contained in:
Nathan McRae 2025-05-29 00:45:41 -07:00
parent bf754c79d6
commit 1c7069c737
2 changed files with 108 additions and 4 deletions

View File

@ -60,6 +60,10 @@ public class LinuxScheduler {
matcher.appendReplacement(sb, "");
}
if (foundMultiple) {
logger.log(Level.WARNING, "Found multiple instances of '" + taskName + "' prior to replacement");
}
matcher.appendTail(sb);
} else {
sb.append(currentCrontab);
@ -95,6 +99,105 @@ public class LinuxScheduler {
return Results.success(true);
}
public static Result<Boolean, String> scheduleExists(StationSettings settings) {
try {
String taskName = "numbers-station-main_" + settings.getName();
Process listProcess = new ProcessBuilder("crontab", "-l").start();
if (!listProcess.waitFor(5, TimeUnit.SECONDS)) {
String message = "Failed to query " + taskName + " task: process timed out";
logger.log(Level.SEVERE, message);
return Results.failure(message);
}
Pair<String, String> output = WindowsScheduler.captureProcessOutput(listProcess);
if(listProcess.exitValue() != 0) {
String message = "Failed to get user id. Exit code: " + listProcess.exitValue() + ". stdout: " + output.getKey() + "\n\tstderr: " + output.getValue();
logger.log(Level.SEVERE, message);
return Results.failure(message);
}
String currentCrontab = output.getKey();
String cronEntry = "# " + taskName + "\n" + cronExpression(settings) + "\n\n";
Pattern pattern = Pattern.compile("# " + taskName + "\\n[^\\n]+\\n\\n");
Matcher matcher = pattern.matcher(currentCrontab);
if (matcher.find()) {
return Results.success(true);
} else {
return Results.success(true);
}
} catch (CronExpressionException | IOException | InterruptedException e) {
String message = "Exception while checking for schedule";
logger.log(Level.SEVERE, message, e);
return Results.failure(message);
}
}
public static Result<Boolean, String> removeSchedule(StationSettings settings) {
try {
String taskName = "numbers-station-main_" + settings.getName();
Process listProcess = new ProcessBuilder("crontab", "-l").start();
if (!listProcess.waitFor(5, TimeUnit.SECONDS)) {
String message = "Failed to query " + taskName + " task: process timed out";
logger.log(Level.SEVERE, message);
return Results.failure(message);
}
Pair<String, String> output = WindowsScheduler.captureProcessOutput(listProcess);
if(listProcess.exitValue() != 0) {
String message = "Failed to get user id. Exit code: " + listProcess.exitValue() + ". stdout: " + output.getKey() + "\n\tstderr: " + output.getValue();
logger.log(Level.SEVERE, message);
return Results.failure(message);
}
String currentCrontab = output.getKey();
String cronEntry = "# " + taskName + "\n" + cronExpression(settings) + "\n\n";
Pattern pattern = Pattern.compile("# " + taskName + "\\n[^\\n]+\\n\\n");
Matcher matcher = pattern.matcher(currentCrontab);
StringBuilder sb = new StringBuilder();
while (matcher.find()) {
matcher.appendReplacement(sb, "");
}
String newCrontab = sb.toString();
Process addProcess = new ProcessBuilder("crontab", "-").start();
Writer w = new OutputStreamWriter(addProcess.getOutputStream(), "UTF-8");
System.out.println(newCrontab);
w.write(newCrontab);
w.flush();
w.close();
if (!addProcess.waitFor(5, TimeUnit.SECONDS)) {
String message = "Failed to register " + taskName + " task: process timed out";
logger.log(Level.SEVERE, message);
return Results.failure(message);
}
if(addProcess.exitValue() != 0) {
Pair<String, String> addOutput = WindowsScheduler.captureProcessOutput(addProcess);
String message = "Failed to get user id. Exit code: " + addProcess.exitValue() + ". stdout: " + addOutput.getKey() + ". stderr: " + addOutput.getValue();
logger.log(Level.SEVERE, message);
return Results.failure(message);
}
} catch (CronExpressionException | IOException | InterruptedException e) {
String message = "Exception while removing schedule";
logger.log(Level.SEVERE, message, e);
return Results.failure(message);
}
return Results.success(true);
}
/**
* * * * * * {command to execute}
* | | | | |

View File

@ -81,8 +81,7 @@ public class StationSettings {
if (osName.contains("win")) {
WindowsScheduler.removeSchedule(temp);
} else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
// TODO: linux
logger.log(Level.SEVERE, "Unsupported OS " + osName);
LinuxScheduler.removeSchedule(temp);
} else {
logger.log(Level.SEVERE, "Unsupported OS " + osName);
}
@ -101,8 +100,10 @@ public class StationSettings {
return true;
}
} else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
// TODO: linux
logger.log(Level.SEVERE, "Unsupported OS " + osName);
Result<Boolean, String> result = LinuxScheduler.scheduleExists(temp);
if (result.hasSuccess() && result.getSuccess().get()) {
return true;
}
} else {
logger.log(Level.SEVERE, "Unsupported OS " + osName);
}