Compare commits
No commits in common. "d7852e281df3ab5f078bc84c95ae7db9d4480a0e" and "e74ddc99cad79a398b3d99d9cb82deac9d1e62ae" have entirely different histories.
d7852e281d
...
e74ddc99ca
@ -1,6 +1,5 @@
|
|||||||
package name.nathanmcrae.numbersstation;
|
package name.nathanmcrae.numbersstation;
|
||||||
|
|
||||||
import com.leakyabstractions.result.api.Result;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
@ -14,7 +13,6 @@ import java.time.LocalDate;
|
|||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class StationSettings {
|
public class StationSettings {
|
||||||
@ -67,44 +65,6 @@ public class StationSettings {
|
|||||||
scheduleStartTime = LocalTime.now();
|
scheduleStartTime = LocalTime.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void deleteStationData(String stationName) throws IOException {
|
|
||||||
StationSettings temp = new StationSettings(stationName);
|
|
||||||
|
|
||||||
temp.deleteDir();
|
|
||||||
|
|
||||||
String osName = System.getProperty("os.name").toLowerCase();
|
|
||||||
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);
|
|
||||||
} else {
|
|
||||||
logger.log(Level.SEVERE, "Unsupported OS " + osName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean stationDataExists(String stationName) {
|
|
||||||
StationSettings temp = new StationSettings(stationName);
|
|
||||||
if (Files.exists(temp.stationPath())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
String osName = System.getProperty("os.name").toLowerCase();
|
|
||||||
if (osName.contains("win")) {
|
|
||||||
Result<Boolean, String> result = WindowsScheduler.scheduleExists(temp);
|
|
||||||
if (result.hasSuccess() && result.getSuccess().get()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
|
|
||||||
// TODO: linux
|
|
||||||
logger.log(Level.SEVERE, "Unsupported OS " + osName);
|
|
||||||
} else {
|
|
||||||
logger.log(Level.SEVERE, "Unsupported OS " + osName);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteDir() throws IOException {
|
public void deleteDir() throws IOException {
|
||||||
Files.walkFileTree(stationPath(), new SimpleFileVisitor<Path>() {
|
Files.walkFileTree(stationPath(), new SimpleFileVisitor<Path>() {
|
||||||
@Override
|
@Override
|
||||||
@ -121,7 +81,6 @@ public class StationSettings {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Path nextMessagePath() {
|
public Path nextMessagePath() {
|
||||||
return stationPath().resolve("next-message.txt");
|
return stationPath().resolve("next-message.txt");
|
||||||
}
|
}
|
||||||
|
@ -221,7 +221,7 @@ public class WindowsScheduler {
|
|||||||
exec.appendChild(command);
|
exec.appendChild(command);
|
||||||
|
|
||||||
Element arguments = doc.createElement("Arguments");
|
Element arguments = doc.createElement("Arguments");
|
||||||
arguments.appendChild(doc.createTextNode("-Version 5 -NoProfile -File P:/personal_root/projects/numbers-station/src/main/java/run.ps1 --station \"" + station.getName() + "\""));
|
arguments.appendChild(doc.createTextNode("-Version 5 -NoProfile -File P:/personal_root/projects/number-station/src/main/java/run.ps1 --station \"" + station.getName() + "\""));
|
||||||
exec.appendChild(arguments);
|
exec.appendChild(arguments);
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
@ -286,10 +286,6 @@ public class WindowsScheduler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settings.getManageScheduleExternally()) {
|
|
||||||
return Results.success(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
Result<String, String> userIdResult = getUserId();
|
Result<String, String> userIdResult = getUserId();
|
||||||
if (!userIdResult.hasSuccess()) {
|
if (!userIdResult.hasSuccess()) {
|
||||||
return Results.failure(userIdResult.getFailure().get());
|
return Results.failure(userIdResult.getFailure().get());
|
||||||
@ -345,52 +341,6 @@ public class WindowsScheduler {
|
|||||||
return Results.success(true);
|
return Results.success(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<Boolean, String> removeSchedule(StationSettings settings) {
|
|
||||||
Path tempFile = null;
|
|
||||||
try {
|
|
||||||
String taskName = "numbers-station-main_" + settings.getName();
|
|
||||||
|
|
||||||
Process queryTaskProcess = new ProcessBuilder("schtasks.exe", "/query", "/tn", taskName).start();
|
|
||||||
if (!queryTaskProcess.waitFor(5, TimeUnit.SECONDS)) {
|
|
||||||
String message = "Failed to query " + taskName + " task: process timed out";
|
|
||||||
logger.log(Level.SEVERE, message);
|
|
||||||
return Results.failure(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove previous instance of task if it exists
|
|
||||||
if (queryTaskProcess.exitValue() == 0) {
|
|
||||||
Process removeTaskProcess = new ProcessBuilder("schtasks.exe", "/delete", "/f", "/tn", taskName).start();
|
|
||||||
if (!removeTaskProcess.waitFor(5, TimeUnit.SECONDS)) {
|
|
||||||
String message = "Failed to remove " + taskName + " task: process timed out";
|
|
||||||
logger.log(Level.SEVERE, message);
|
|
||||||
return Results.failure(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (removeTaskProcess.exitValue() == 0) {
|
|
||||||
logger.info(taskName + " task removed successfully.");
|
|
||||||
} else {
|
|
||||||
Pair<String, String> output = captureProcessOutput(removeTaskProcess);
|
|
||||||
String message = "Failed to remove " + taskName + " task. Exit code: " + removeTaskProcess.exitValue() + ". stdout: " + output.getKey() + "\n\tstderr: " + output.getValue();
|
|
||||||
logger.log(Level.SEVERE, message);
|
|
||||||
return Results.failure(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException | InterruptedException e) {
|
|
||||||
String message = "Exception while registering schedule";
|
|
||||||
logger.log(Level.SEVERE, message, e);
|
|
||||||
return Results.failure(message);
|
|
||||||
} finally {
|
|
||||||
if (tempFile != null) {
|
|
||||||
try {
|
|
||||||
Files.delete(tempFile);
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.log(Level.SEVERE, "Failed to delete temporary file", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Results.success(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Result<Boolean, String> runSchedule(StationSettings settings) {
|
public static Result<Boolean, String> runSchedule(StationSettings settings) {
|
||||||
String taskName = "numbers-station-main_" + settings.getName();
|
String taskName = "numbers-station-main_" + settings.getName();
|
||||||
|
|
||||||
@ -405,28 +355,6 @@ public class WindowsScheduler {
|
|||||||
return Results.success(true);
|
return Results.success(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<Boolean, String> scheduleExists(StationSettings settings) {
|
|
||||||
try {
|
|
||||||
String taskName = "numbers-station-main_" + settings.getName();
|
|
||||||
|
|
||||||
Process queryTaskProcess = new ProcessBuilder("schtasks.exe", "/query", "/tn", taskName).start();
|
|
||||||
if (!queryTaskProcess.waitFor(5, TimeUnit.SECONDS)) {
|
|
||||||
String message = "Failed to query " + taskName + " task: process timed out";
|
|
||||||
logger.log(Level.SEVERE, message);
|
|
||||||
return Results.failure(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Results.success(queryTaskProcess.exitValue() == 0);
|
|
||||||
} catch (InterruptedException | IOException e) {
|
|
||||||
String message = "Exception while querying schedule";
|
|
||||||
logger.log(Level.SEVERE, message, e);
|
|
||||||
return Results.failure(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return (stdout contents, stderr contents)
|
|
||||||
*/
|
|
||||||
public static Pair<String, String> captureProcessOutput(Process process) throws IOException {
|
public static Pair<String, String> captureProcessOutput(Process process) throws IOException {
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
StringBuilder error = new StringBuilder();
|
StringBuilder error = new StringBuilder();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user