Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Fixes and log file added
Browse files Browse the repository at this point in the history
  • Loading branch information
Trsak committed May 7, 2019
1 parent b185197 commit 7db7307
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
46 changes: 34 additions & 12 deletions FivemManager/FivemServer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ConfigFile;
Expand All @@ -11,18 +13,20 @@ internal class FivemServer
public bool IsRunning;
private bool _isFivemServerRunning;
private readonly ConfigReader _config;
private Process _process;

public FivemServer(ConfigReader config)
{
this._config = config;
this.IsRunning = false;
this._isFivemServerRunning = false;
this._process = null;
}

public void StartServer()
{
LogToConsole("Starting server...");
var process = new System.Diagnostics.Process();
this._process = new System.Diagnostics.Process();

var startInfo = new System.Diagnostics.ProcessStartInfo
{
Expand All @@ -33,8 +37,8 @@ public void StartServer()
WorkingDirectory = this._config.TryGetValue<string>("ServerLocation")
};

process.StartInfo = startInfo;
process.Start();
this._process.StartInfo = startInfo;
this._process.Start();

this.IsRunning = true;
this._isFivemServerRunning = true;
Expand Down Expand Up @@ -77,10 +81,7 @@ private void StopServer()
{
this._isFivemServerRunning = false;

foreach (var process in Process.GetProcessesByName("FXServer"))
{
process.Kill();
}
this._process.Kill();
}

private void OnRestart()
Expand All @@ -93,24 +94,45 @@ private void OnRestart()

public void CheckIfCrashed()
{
if (!this._isFivemServerRunning) return;

LogToConsole("CRASH CHECK: Checking, if server is running.");

if (!this._isFivemServerRunning) return;

var processesNumber = Process.GetProcessesByName("FXServer").GetLength(0);
if (processesNumber == 0)
if (!this.IsProcessRunning())
{
LogToConsole("CRASH CHECK: Server is not running!");
this.StartServer();
return;
}

LogToConsole("CRASH CHECK: Server is running.");
LogToConsole("CRASH CHECK: Server is running.");
}

private static void LogToConsole(string text)
{
Console.WriteLine(DateTime.Now.ToString("[HH:mm:ss] ") + text);
text = DateTime.Now.ToString("[HH:mm:ss] ") + text;
Console.WriteLine(text);
System.IO.File.AppendAllText(@"log.txt", text + "\n");
}

private bool IsProcessRunning()
{
if (this._process == null)
{
return false;
}

try
{
Process.GetProcessById(this._process.Id);
}
catch (ArgumentException)
{
return false;
}

return true;
}
}
}
6 changes: 3 additions & 3 deletions FivemManager/config/manager.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#FXServer DIR location
FXServerLocation = "D:/Projects/FXServer/server"

#Server data location, place where ConfigFile is located
#Server data location (where ConfigFile is located)
ServerLocation = "D:/Projects/FiveM"

#main server config file
Expand All @@ -10,5 +10,5 @@ ConfigFile = "server.cfg"
#Server restart times, split by ";"
RestartTimes = "02:00:05;12:00:05;18:00:05"

#Interval for checking if server is running or not
CrashCheckInterval = 1
#Interval for checking if server is running or not (in minutes)
CrashCheckInterval = 5

0 comments on commit 7db7307

Please sign in to comment.