Improve ArchiService

- Correct DisplayName
- Implement proper Shutdown sequence instead of 20 seconds timeout-kill
- Misc
This commit is contained in:
JustArchi
2016-06-28 07:14:51 +02:00
parent 5a8701444a
commit 7351d07518
3 changed files with 36 additions and 23 deletions

View File

@@ -12,7 +12,8 @@ namespace ArchiSteamFarm {
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller(); ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
serviceInstaller.ServiceName = SharedInfo.ServiceName; serviceInstaller.ServiceName = SharedInfo.ServiceName;
serviceInstaller.DisplayName = SharedInfo.ServiceDescription; serviceInstaller.DisplayName = SharedInfo.ServiceName;
serviceInstaller.Description = SharedInfo.ServiceDescription;
// Defaulting to only starting when a user starts it, can be easily changed after install // Defaulting to only starting when a user starts it, can be easily changed after install
serviceInstaller.StartType = ServiceStartMode.Manual; serviceInstaller.StartType = ServiceStartMode.Manual;

View File

@@ -332,6 +332,17 @@ namespace ArchiSteamFarm {
return false; return false;
} }
internal void Stop() {
Logging.LogGenericInfo("Stopping...", BotName);
KeepRunning = false;
if (SteamClient.IsConnected) {
SteamClient.Disconnect();
}
Program.OnBotShutdown();
}
internal void OnFarmingStopped() => ResetGamesPlayed(); internal void OnFarmingStopped() => ResetGamesPlayed();
internal async Task OnFarmingFinished(bool farmedSomething) { internal async Task OnFarmingFinished(bool farmedSomething) {
@@ -478,17 +489,6 @@ namespace ArchiSteamFarm {
SteamClient.Connect(); SteamClient.Connect();
} }
private void Stop() {
Logging.LogGenericInfo("Stopping...", BotName);
KeepRunning = false;
if (SteamClient.IsConnected) {
SteamClient.Disconnect();
}
Program.OnBotShutdown();
}
private bool IsMaster(ulong steamID) { private bool IsMaster(ulong steamID) {
if (steamID != 0) { if (steamID != 0) {
return (steamID == BotConfig.SteamMasterID) || IsOwner(steamID); return (steamID == BotConfig.SteamMasterID) || IsOwner(steamID);

View File

@@ -258,7 +258,7 @@ namespace ArchiSteamFarm {
} }
internal static void Exit(int exitCode = 0) { internal static void Exit(int exitCode = 0) {
WCF.StopServer(); Shutdown();
Environment.Exit(exitCode); Environment.Exit(exitCode);
} }
@@ -335,6 +335,10 @@ namespace ArchiSteamFarm {
} }
internal static void OnBotShutdown() { internal static void OnBotShutdown() {
if (ShutdownResetEvent.IsSet) {
return;
}
if (Bot.Bots.Values.Any(bot => bot.KeepRunning)) { if (Bot.Bots.Values.Any(bot => bot.KeepRunning)) {
return; return;
} }
@@ -348,6 +352,15 @@ namespace ArchiSteamFarm {
ShutdownResetEvent.Set(); ShutdownResetEvent.Set();
} }
private static void Shutdown() {
WCF.StopServer();
ShutdownResetEvent.Set();
foreach (Bot bot in Bot.Bots.Values) {
bot.Stop();
}
}
private static void InitServices() { private static void InitServices() {
GlobalConfig = GlobalConfig.Load(Path.Combine(ConfigDirectory, GlobalConfigFile)); GlobalConfig = GlobalConfig.Load(Path.Combine(ConfigDirectory, GlobalConfigFile));
if (GlobalConfig == null) { if (GlobalConfig == null) {
@@ -518,12 +531,7 @@ namespace ArchiSteamFarm {
} }
private static void Main(string[] args) { private static void Main(string[] args) {
if (!Environment.UserInteractive) { if (Environment.UserInteractive) {
// Service
using (Service service = new Service()) {
ServiceBase.Run(service);
}
} else {
// App // App
Init(args); Init(args);
@@ -532,22 +540,26 @@ namespace ArchiSteamFarm {
// We got a signal to shutdown // We got a signal to shutdown
Exit(); Exit();
} else {
// Service
using (Service service = new Service()) {
ServiceBase.Run(service);
}
} }
} }
private sealed class Service : ServiceBase { private sealed class Service : ServiceBase {
internal Service() { internal Service() {
ServiceName = SharedInfo.ServiceName; ServiceName = SharedInfo.ServiceName;
} }
protected override void OnStart(string[] args) => new Thread(() => { protected override void OnStart(string[] args) => Task.Run(() => {
Init(args); Init(args);
ShutdownResetEvent.Wait(); ShutdownResetEvent.Wait();
Stop(); Stop();
}).Start(); });
protected override void OnStop() => ShutdownResetEvent.Set(); protected override void OnStop() => Shutdown();
} }
} }