ConanServerManager/lib/statemachine.dart

415 lines
14 KiB
Dart
Raw Permalink Normal View History

2023-11-05 00:03:15 -07:00
import 'dart:async';
import 'dart:io';
import 'package:libac_dart/packets/packets.dart';
import 'package:libac_dart/utils/IOTools.dart';
2024-06-04 21:51:53 -07:00
import 'package:libac_dart/utils/TimeUtils.dart';
2023-11-05 00:03:15 -07:00
import 'package:servermanager/game.dart';
2024-05-23 22:18:59 -07:00
import 'package:servermanager/structs/SessionData.dart';
2024-06-16 16:42:50 -07:00
import 'package:servermanager/structs/discordHookHelper.dart';
2024-05-25 04:50:28 -07:00
import 'package:servermanager/structs/mod.dart';
import 'package:servermanager/structs/settings.dart';
2024-06-03 18:33:13 -07:00
import 'package:servermanager/wine.dart';
2023-11-05 00:03:15 -07:00
enum States {
Idle, // For when the state machine is waiting for a state change
2024-05-23 22:18:59 -07:00
PreStart, // Backup task
2023-11-05 00:03:15 -07:00
Starting, // For when the server is starting up
ModUpdateCheck, // For when the mods are being checked in the jail folder against the live mods
WarnPlayersNonIntrusive, // Gives a non-instrusive warning about the upcoming restart
WarnPlayersIntrusive, // Sends a message intrusively about the upcoming restart
FullStop, // Intends to set the inactive state, and immediately stops the server
Inactive // The inactive state will be used for when the state machine is not supposed to be doing anything.
}
2024-05-23 22:18:59 -07:00
enum WarnType { Intrusive, NonIntrusive }
enum WarnIntervals {
2024-06-15 22:48:48 -07:00
FIVE_SECONDS(seconds: 5, type: WarnType.Intrusive, warning: "Tiiiiiimber!"),
2024-05-23 22:18:59 -07:00
TEN_SECONDS(
seconds: 10,
type: WarnType.Intrusive,
warning: "The server has 10 seconds until restart"),
TWENTY_SECONDS(
seconds: 20,
type: WarnType.NonIntrusive,
2024-05-23 22:18:59 -07:00
warning: "The server is about to go down in 20 seconds"),
THIRTY_SECONDS(
seconds: 30,
type: WarnType.NonIntrusive,
warning: "The server will restart in 30 seconds"),
ONE_MINUTE(
seconds: 60,
type: WarnType.Intrusive,
2024-05-23 22:18:59 -07:00
warning: "The server will restart in 1 minute"),
FIVE_MIN(
seconds: (5 * 60),
type: WarnType.NonIntrusive,
warning: "The server will restart in 5 minutes"),
TEN_MIN(
seconds: (10 * 60),
2024-06-15 22:48:48 -07:00
type: WarnType.Intrusive,
2024-05-23 22:18:59 -07:00
warning: "The server will restart in 10 minutes"),
ONE_HOUR(
seconds: (1 * 60 * 60),
2024-06-15 22:48:48 -07:00
type: WarnType.Intrusive,
2024-05-23 22:18:59 -07:00
warning: "The server will restart in 1 hour"),
TWO_HOURS(
seconds: (2 * 60 * 60),
type: WarnType.NonIntrusive,
warning: "The server will restart in 2 hours"),
ELEVEN_HOURS(
seconds: (11 * 60 * 60),
type: WarnType.NonIntrusive,
warning: "The server will restart in 11 hours"),
2024-05-23 22:18:59 -07:00
NONE(
2024-05-25 04:50:28 -07:00
seconds: 2147483647,
2024-05-23 22:18:59 -07:00
type: WarnType.NonIntrusive,
warning:
2024-05-25 04:50:28 -07:00
""); // int32 max value should never be possible, is more seconds than in a single day.
2024-05-23 22:18:59 -07:00
final int seconds;
final WarnType type;
final String warning;
const WarnIntervals(
{required this.seconds, required this.type, required this.warning});
}
2023-11-05 00:03:15 -07:00
class StateMachine {
2024-05-23 22:18:59 -07:00
static Process? PROC;
2024-05-24 13:48:21 -07:00
static Completer<void> DeadProcKillswitch = Completer();
void resetKillswitch() {
DeadProcKillswitch = Completer();
}
static Future<void> monitorProcess() async {
2024-06-04 21:51:53 -07:00
Settings settings = Settings();
// Ping RCON. If we can connect, the server is alive.
// Only start pinging once a minute, after the first 10 minutes.
if (SessionData.operating_time.getTotalSeconds() >
Time(minutes: 10, hours: 0, seconds: 0).getTotalSeconds() &&
!SessionData.canPingServer) {
SessionData.canPingServer = true;
}
if (SessionData.canPingServer) {
SessionData.timeSinceLastPing.tickUp();
if (SessionData.timeSinceLastPing.getTotalSeconds() > 60) {
SessionData.timeSinceLastPing.apply(0);
}
2024-05-24 13:48:21 -07:00
}
}
2023-11-05 00:03:15 -07:00
var _currentState = States.Inactive;
StreamController<States> _stateController = StreamController.broadcast();
Stream<States> get stateChanges => _stateController.stream;
States get currentState => _currentState;
void changeState(States n) {
_currentState = n;
_stateController.add(n);
}
Future<void> runTask() async {
if (currentState == States.Idle) {
return; // Nothing to do here
} else if (currentState == States.FullStop) {
Settings settings = Settings();
print("Sending shutdown command to server");
2024-05-23 22:18:59 -07:00
await settings.sendRconCommand("shutdown");
2024-05-24 18:11:00 -07:00
Timer.periodic(Duration(seconds: 30), (timer) {
timer.cancel();
2024-05-24 18:36:34 -07:00
print("Sending killswitch to server");
PROC!.kill(ProcessSignal.sigkill);
2024-06-04 21:51:53 -07:00
PacketServer.socket!.close();
2024-05-24 18:11:00 -07:00
});
2024-06-16 16:42:50 -07:00
DiscordHookHelper.sendWebHook(
settings.inst!.discord,
DiscordHookProps.OFFLINE_ALERT,
"Server is now offline",
"The server is shutting down");
changeState(States.Inactive);
2023-11-05 00:03:15 -07:00
} else if (currentState == States.Starting) {
// Server startup in progress
Settings settings = Settings();
2024-05-25 16:51:08 -07:00
//await settings.RunUpdate(valid: false);
//await doDownloadMods(false);
2024-05-23 22:18:59 -07:00
2024-05-25 16:51:08 -07:00
//settings.inst!.mods = await doScanMods(false);
2024-05-24 13:48:21 -07:00
settings.Write();
2023-11-05 00:03:15 -07:00
await settings.writeOutModListFile();
var conanArgs = [
"-RconEnabled=1",
"-RconPassword=${settings.inst!.serverSettings.RconPassword}",
"-RconPort=${settings.inst!.serverSettings.RconPort}",
"-Port=${settings.inst!.serverSettings.GamePort}",
2023-11-05 02:40:55 -07:00
"-QueryPort=${settings.inst!.serverSettings.QueryPort}",
2024-05-24 14:09:13 -07:00
"-log",
"-console"
2023-11-05 00:03:15 -07:00
];
// Start the server now
2024-06-04 21:51:53 -07:00
String executable = PathHelper.builder(settings.getServerPath())
.resolve("ConanSandbox")
.resolve("Binaries")
.resolve("Win64")
.resolve("ConanSandboxServer-Win64-Shipping.exe")
.build();
2023-11-05 00:03:15 -07:00
if (Platform.isWindows) {
2024-06-04 21:51:53 -07:00
PROC = await Process.start(executable, conanArgs,
2023-11-05 00:03:15 -07:00
workingDirectory: settings.getServerPath());
} else {
2024-06-04 21:51:53 -07:00
runDetachedWine(executable, conanArgs, settings.getServerPath());
2023-11-05 00:03:15 -07:00
}
2024-06-04 21:51:53 -07:00
Timer.periodic(Duration(seconds: 20), (timer) async {
File logFile = File(PathHelper.builder(settings.getServerPath())
.resolve("ConanSandbox")
.resolve("Saved")
.resolve("Logs")
.resolve("ConanSandbox.log")
.build());
await logFile.create(recursive: true);
tailAndPrint(logFile);
timer.cancel();
});
2023-11-05 00:03:15 -07:00
changeState(States.Idle);
2024-05-23 22:18:59 -07:00
} else if (currentState == States.PreStart) {
// Perform Backup Task
// Finally, allow the server to start up
changeState(States.Starting);
2023-11-05 00:03:15 -07:00
}
}
StateMachine() {
stateChanges.listen((event) async {
await runTask();
});
}
2024-05-23 22:18:59 -07:00
Timer? task;
2024-06-03 15:07:55 -07:00
Future<void> pollModUpdates() async {
await doDownloadMods(false);
await doScanMods(false);
}
2024-05-23 22:18:59 -07:00
/// You should only start this task once the server is ready to be started.
/// This task will start the server on first-start
///
/// It will monitor the states for shutdown, it will also monitor the process handle
///
/// This is what runs the automatic restart timers as well. All state change logic that should happen automatically is contained in here.
Future<void> startScheduler() async {
Settings settings = Settings();
SessionData.timer = settings.inst!.timer.time.copy();
changeState(States.PreStart);
2024-06-16 16:42:50 -07:00
DiscordHookHelper.sendWebHook(
settings.inst!.discord,
DiscordHookProps.ONLINE_ALERT,
"Server is now starting up",
"The server is starting up now, it should appear on the server list in a few minutes");
2024-05-24 13:48:21 -07:00
resetKillswitch();
SessionData.enableRestartTimer = settings.inst!.timer.enabled;
2024-05-24 13:48:21 -07:00
2024-05-23 22:18:59 -07:00
// Schedule the server task
2024-06-03 15:07:55 -07:00
task = Timer.periodic(Duration(seconds: 1), (timer) async {
2024-05-23 22:18:59 -07:00
switch (currentState) {
case States.Inactive:
{
timer.cancel();
// Check if we should perform a world restore
if (SessionData.isWorldRestore) {
await DiscordHookHelper.sendWebHook(
settings.inst!.discord,
DiscordHookProps.OFFLINE_ALERT,
"RESTORE",
"Restoring backup file: ${SessionData.snapshotToRestore}");
// Now restore the backup file
File backup = File(SessionData.snapshotToRestore);
await backup.copy(settings.getWorldGameDB());
}
2024-05-23 22:18:59 -07:00
//Settings settings = Settings();
if (settings.inst!.pterodactylMode) {
// Shut down the server processes now
PacketServer.socket!.close();
exit(0);
2024-05-24 14:09:13 -07:00
} else {
resetKillswitch();
SessionData.timer = settings.inst!.timer.time.copy();
2024-06-03 15:07:55 -07:00
await pollModUpdates();
2024-05-24 14:09:13 -07:00
changeState(States.PreStart);
2024-05-25 04:50:28 -07:00
SessionData.enableRestartTimer = settings.inst!.timer.enabled;
2024-05-23 22:18:59 -07:00
}
break;
}
case States.Idle:
{
// Restart timers and such
SessionData.timer.tickDown();
SessionData.operating_time.tickUp();
SessionData.timeSinceLastPing.tickUp();
2024-05-25 04:50:28 -07:00
SessionData.bumpModUpdateChecker();
2024-05-23 22:18:59 -07:00
// Check if we should send an alert
int sec = SessionData.timer.getTotalSeconds();
WarnIntervals current = SessionData.CURRENT_INTERVAL;
bool send = false;
for (WarnIntervals WI in WarnIntervals.values) {
if (WI == WarnIntervals.NONE) continue;
2024-06-09 13:52:56 -07:00
if (WI.seconds >= sec &&
2024-06-09 19:04:06 -07:00
(current == WarnIntervals.NONE ||
WI.seconds <= current.seconds) &&
2024-06-09 13:52:56 -07:00
WI != current) {
2024-05-23 22:18:59 -07:00
current = WI;
send = true;
break;
}
}
// Ping the server to check if it is alive
2024-06-15 14:43:36 -07:00
/*
if (SessionData.timeSinceLastPing.getTotalSeconds() > 60 &&
SessionData.operating_time.getTotalSeconds() > (10 * 60)) {
SessionData.timeSinceLastPing.apply(0);
2024-06-15 14:43:36 -07:00
int pid = PROC!.pid;
try {
Socket sock = await Socket.connect(
"127.0.0.1", settings.inst!.serverSettings.QueryPort);
await sock.close();
2024-06-05 21:22:00 -07:00
} catch (E, stack) {
print("Dead process checker caught: ${E}\n\n${stack}\n\n");
DeadProcKillswitch.complete();
}
2024-06-15 14:43:36 -07:00
}*/
if (send && SessionData.enableRestartTimer) {
2024-05-23 22:18:59 -07:00
// Send the alert message
2024-05-24 14:09:13 -07:00
SessionData.CURRENT_INTERVAL = current;
2024-06-16 16:42:50 -07:00
int alertColor = 0;
2024-05-24 14:09:13 -07:00
if (current.type == WarnType.Intrusive) {
2024-05-25 13:42:30 -07:00
print("Sending alert '${current.warning}'");
2024-05-24 14:09:13 -07:00
settings.sendRconCommand("broadcast ${current.warning}");
2024-06-16 16:42:50 -07:00
// Set discord alert color
alertColor = DiscordHookProps.ALERT_INTRUSIVE;
} else if (current.type == WarnType.NonIntrusive) {
print("Sending chat message '${current.warning}'");
2024-06-16 16:42:50 -07:00
//settings.sendRconCommand(
// "ast chat \"global\" \"${current.warning}\"");
// Set discord alert color
alertColor = DiscordHookProps.ALERT;
2024-05-24 14:09:13 -07:00
}
2024-06-16 16:42:50 -07:00
DiscordHookHelper.sendWebHook(settings.inst!.discord, alertColor,
2024-07-01 23:15:08 -07:00
"Server Restart Alert", current.warning);
2024-05-23 22:18:59 -07:00
}
// Check Shutdown Pending
2024-06-03 15:07:55 -07:00
if (SessionData.shutdownMessage.isNotEmpty) {
settings
.sendRconCommand("broadcast ${SessionData.shutdownMessage}");
SessionData.shutdownMessage = "";
}
2024-05-23 22:18:59 -07:00
if (SessionData.shutdownPending) {
// Shut down the server
changeState(States.FullStop);
SessionData.shutdownPending = false;
}
2024-05-25 04:50:28 -07:00
// Check mod updates
if (SessionData.shouldCheckModUpdates()) {
2024-05-25 13:42:30 -07:00
print("Scheduling mod update checker...");
SessionData.resetModUpdateChecker();
2024-05-25 04:50:28 -07:00
Timer.periodic(Duration(seconds: 10), (timer) async {
timer.cancel();
await doDownloadMods(true);
List<Mod> currentMods = await doScanMods(true);
List<String> updatedMods = [];
for (int i = 0; i < currentMods.length; i++) {
Mod currentMod = settings.inst!.mods[i];
Mod scannedMod = currentMods[i];
if (currentMod.mod_hash == scannedMod.mod_hash) {
// Mod is ok
} else {
// Mod is not ok
2024-07-01 23:15:08 -07:00
updatedMods.add(scannedMod.mod_name);
2024-05-25 04:50:28 -07:00
}
}
if (updatedMods.isNotEmpty) {
settings.sendRconCommand(
"broadcast The server will be going down for a restart in 5 minutes. The following mods have been updated: ${updatedMods.join(', ')}");
SessionData.timer.apply((5 * 60));
print(
"Scheduling restart for mod updates: ${updatedMods.join(', ')}");
SessionData.enableRestartTimer = true;
2024-06-16 20:13:35 -07:00
// Send discord alert!
DiscordHookHelper.sendWebHook(
settings.inst!.discord,
DiscordHookProps.ALERT_INTRUSIVE,
"Mods have been updated",
"The server is going to restart because the following mods have been updated: \n${updatedMods.join(', ')}");
DiscordHookHelper.sendWebHook(
settings.inst!.discord,
DiscordHookProps.ALERT,
"Server Restart Alert",
"The server will restart in 5 minutes");
}
2024-05-25 04:50:28 -07:00
});
}
2024-05-23 22:18:59 -07:00
// Check Total Seconds
if (SessionData.timer.getTotalSeconds() == 0 &&
SessionData.enableRestartTimer) {
2024-05-25 13:42:30 -07:00
print("Shutdown is pending, restart timer has hit zero");
2024-05-23 22:18:59 -07:00
SessionData.shutdownPending = true;
}
2024-05-24 13:48:21 -07:00
// Check Dead Process
if (DeadProcKillswitch.isCompleted) {
// Switch state
2024-05-25 13:42:30 -07:00
print("Dead process detected - Entering restart loop");
2024-05-24 13:48:21 -07:00
changeState(States.FullStop); // This has the stop logic
}
2024-05-23 22:18:59 -07:00
break;
}
default:
{
break;
}
}
});
}
2023-11-05 00:03:15 -07:00
}