Add linux schedule management functions
This commit is contained in:
parent
bf754c79d6
commit
1c7069c737
@ -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}
|
||||
* | | | | |
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user