Voeg afsluitlogica toe

This commit is contained in:
Chris Josten 2021-03-02 02:28:48 +01:00
parent ae97f07a41
commit 8fec607e4f
7 changed files with 122 additions and 55 deletions

View file

@ -1,19 +1,15 @@
{
"name": "mvoau ober",
"authors": "Meson Team",
"copyright": "Copyright © 2018, Meson Team",
"license": [
"MIT"
],
"sourceFiles": [
"source/msgpackrpc/client.d",
"source/msgpackrpc/package.d",
"source/msgpackrpc/protocol.d",
"source/msgpackrpc/server.d",
"source/app.d"
],
"targetType": "executable",
"dependencies": {
"ddbus": "3.0.0"
}
}
"authors": [
"Chris Josten"
],
"copyright": "Copyright © 2021, Chris Josten",
"dependencies": {
"ddbus": {"path": "/home/chris/Programmeren/D/ddbus"},
"msgpack-d": "~>1.0.3",
"vibe-core": "~>1.13.0"
},
"description": "Automatisch In-Uitschakelen Ober",
"license": "MIT",
"name": "mvoau",
"versions": []
}

14
ober/dub.selections.json Normal file
View file

@ -0,0 +1,14 @@
{
"fileVersion": 1,
"versions": {
"ddbus": {"path":"../../../D/ddbus"},
"dunit": "1.0.16",
"eventcore": "0.9.13",
"libasync": "0.8.6",
"memutils": "1.0.4",
"msgpack-d": "1.0.3",
"stdx-allocator": "2.77.5",
"taggedalgebraic": "0.11.19",
"vibe-core": "1.13.0"
}
}

View file

@ -4,8 +4,9 @@ Wants=network-online.target
After=network-online.target
[Service]
Environment="WACHTTIJD=15"
Type=idle
ExecStart=aiuo-shutdown [wachttijd] [klant-ip]
ExecStart=mvoau $WACHTTIJD
Restart=on-failure
RestartSec=3
User=root

View file

@ -21,22 +21,31 @@ extern (C) void close(int fileNo);
*/
struct Toep {
public:
immutable ushort PORT = 18_002;
immutable int m_waitingTime;
immutable string m_clientAddress;
immutable int CHECK_COUNT = 2;
immutable float m_waitingTime;
immutable int CHECK_COUNT = 4;
Connection m_dbusCon;
bool m_keepRunning = true;
version(WithRPC) Client m_client;
this(int waitingTime, string clientAddress) {
this.m_waitingTime = waitingTime;
this.m_clientAddress = clientAddress;
this(float waitingTime) {
this.m_waitingTime = waitingTime / CHECK_COUNT;
this.m_dbusCon = connectToBus(DBusBusType.DBUS_BUS_SYSTEM);
}
version(WithRPC) {
Client m_client;
immutable string m_clientAddress;
immutable ushort PORT = 18_002;
this(float waitingTime, string clientAddress) {
this.m_waitingTime = waitingTime / CHECK_COUNT;
this.m_clientAddress = clientAddress;
this.m_dbusCon = connectToBus(DBusBusType.DBUS_BUS_SYSTEM);
}
}
int exec() {
info ("Can shut down: ", canShutdown());
info ("Moet blokkeren:", moetBlokkeren());
info ("Kan afsluiten:", kanAfsluiten());
version(WithRPC) {
TCPConnection conn = connectTCP(m_clientAddress, PORT);
Duration sleepTime = 500.msecs;
@ -57,7 +66,7 @@ public:
}
});
runTask({
auto taak = runTask({
FileDescriptor inhibitLock = FileDescriptor.none;
version(WithRPC) {
FileDescriptor sleepLock = FileDescriptor.none;
@ -136,62 +145,105 @@ public:
}
int checkCount = CHECK_COUNT;
int afsluitNakijkTeller = CHECK_COUNT;
void werkAfsluitNakijkTellerBij() {
if (kanAfsluiten()) {
afsluitNakijkTeller--;
} else {
afsluitNakijkTeller = CHECK_COUNT;
}
}
while(m_keepRunning) {
sleep(seconds(cast(long) (m_waitingTime * 60)));
// Check if we are already preventing shutdown
if (inhibitLock != FileDescriptor.none) {
// We are. Check if we can shutdown (as in, no players are on the server)
if (canShutdown()) {
// We zijn op dit moment het afsluiten niet aan het blokkeren. Kijken of dat wel moet.
if (moetBlokkeren()) {
if (checkCount == 1) {
// Release the lock
// Laat het slot los
releaseLock(inhibitLock);
info("Stop preventing shutdown");
info("Afsluitslot losgelaten.");
} else {
// Check 1? more time
// Kijk nog 1 of meer maal
checkCount--;
tracef("Checking %d more time(s)", checkCount);
tracef("Nog %d maal kijken of we het afsluitslot los kunnen laten", checkCount);
}
} else {
// We cannot shut down. Reset the check counter.
// We kunnen niet afsluiten. De nakijkteller herstellen.
checkCount = CHECK_COUNT;
tracef("Still players out there. Keeping lock");
afsluitNakijkTeller = CHECK_COUNT;
tracef("Spelers zijn nog steeds actief. NIet afsluiten.");
}
} else if (!canShutdown()) {
werkAfsluitNakijkTellerBij();
} else if (moetBlokkeren()) {
try {
block();
info("Afsluitslot verkregen.");
} catch(DBusException e) {
warning("Could not take lock and prevent sleep/shutdown: ", e);
warning("Kon afsluitslot niet verkrijgen: ", e);
}
werkAfsluitNakijkTellerBij();
} else if (kanAfsluiten()){
if (afsluitNakijkTeller <= 1) {
trace("We kunnen afsluiten!");
execute(["shutdown", "+1", "Systeem gaat afsluiten vanwege inactiviteit"]);
m_keepRunning = false;
exitEventLoop();
} else {
afsluitNakijkTeller--;
tracef("Nog %s keer, dan kunnen we afsluiten!", afsluitNakijkTeller);
}
info("Start preventing shutdown");
} else {
trace("Nothing to do");
afsluitNakijkTeller = CHECK_COUNT;
trace("Niets om te doen.");
}
sleep(seconds(m_waitingTime));
}
});
int exitCode = runEventLoop();
// cleanup
info("Cleanup");
info("Rotzooi opruimen.");
return exitCode;
}
/**
* Checks if the system can shut down.
* Kijkt of we het afsluiten moeten blokkeren
*/
bool canShutdown() {
auto result = execute(["ss", "-H", "--query=tcp", "state", "established", "sport", "=", ":minecraft"]);
return result.output.splitLines().length == 0;
bool moetBlokkeren() {
auto resultaat = execute(["ss", "-H", "--query=tcp", "state", "established", "sport", "=", ":minecraft"]);
return resultaat.output.splitLines().length != 0;
}
/**
* Kijkt of we direct af kunnen sluiten.
*/
bool kanAfsluiten() {
auto resultaat = execute(["who"]);
return resultaat.output.splitLines().length != 0;
}
}
int main(string[] args) {
scope(failure) stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]);
if (args.length < 3) {
stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]);
return -1;
version(WithRPC) {
scope(failure) stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]);
if (args.length < 3) {
stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]);
return -1;
}
} else {
scope(failure) stderr.writefln("GEBRUIK: %s WACHTTIJD", args[0]);
if (args.length < 2) {
stderr.writefln("GEBRUIK: %s WACHTTIJD", args[0]);
return -1;
}
}
float waitingTime = to!float(args[1]);
version(WithRPC) {
return Toep(waitingTime, args[2]).exec();
} else {
return Toep(waitingTime).exec();
}
int waitingTime = to!int(args[1]);
return Toep(waitingTime, args[2]).exec();
}

View file

@ -1,3 +1,4 @@
module msgpackrpc.client;
import core.atomic;
import std.array;

View file

@ -1,3 +1,5 @@
module msgpackrpc.protocol;
import std.exception;
import msgpack;

View file

@ -0,0 +1 @@
module msgpackrpc.server;