mc-server-manager/api/source/startup_requests.d

55 lines
1.5 KiB
D
Raw Normal View History

module startup_requests;
import std.stdio;
import std.datetime;
import std.file;
import std.path;
import std.string;
void createServerStartupRequest(string serverIdentifier) {
File f = File(getRequestFile(serverIdentifier), "w");
f.writeln(Clock.currTime(UTC()).toISOExtString());
f.close();
}
bool isStartupRequested(string serverIdentifier) {
return exists(getRequestFile(serverIdentifier));
}
void removeStartupRequest(string serverIdentifier) {
string requestFile = getRequestFile(serverIdentifier);
if (exists(requestFile)) std.file.remove(requestFile);
}
void removeOldStartupRequests() {
foreach (string requestFile; findAllStartupRequests()) {
Duration age = getRequestAge(requestFile);
if (age > hours(1)) {
std.file.remove(requestFile);
}
}
}
private Duration getRequestAge(string requestFile) {
string text = readText(requestFile).strip();
SysTime timestamp = SysTime.fromISOExtString(text, UTC());
return Clock.currTime(UTC()) - timestamp;
}
private string[] findAllStartupRequests() {
string[] requestFiles;
foreach (DirEntry entry; dirEntries(getcwd(), SpanMode.shallow, false)) {
string filename = baseName(entry.name);
if (startsWith(filename, "request_") && endsWith(filename, ".txt")) {
requestFiles ~= entry.name;
}
}
return requestFiles;
}
private string getRequestFile(string identifier) {
return "request_" ~ identifier ~ ".txt";
}