Skip to content

Commit

Permalink
fix code complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Elepover committed Apr 2, 2020
1 parent 3967224 commit 4de73d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 47 deletions.
73 changes: 26 additions & 47 deletions pmcenter/Methods/Methods.ExitApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static void ExitApp(int code)
Log("Waiting for background workers to exit (timeout: 10s)...");
var sw = new Stopwatch();
sw.Start();

Vars.IsShuttingDown = true;
if (Vars.IsPerformanceTestExecuting)
{
Expand All @@ -25,64 +26,42 @@ public static void ExitApp(int code)
}
Log("[OK] Shut down performance tester.");

if (Vars.ConfValidator != null && Vars.ConfValidator.IsAlive)
Thread[] threads =
{
Vars.ConfValidator.Interrupt();
while (Vars.ConfValidator.IsAlive)
{
if (sw.ElapsedMilliseconds >= 10000) Environment.Exit(16);
Thread.Sleep(50);
}
}
Log("[OK] Shut down reset timer.");
Vars.ConfValidator,
Vars.UpdateChecker,
Vars.RateLimiter,
Vars.BannedSweepper,
Vars.SyncConf
};

if (Vars.UpdateChecker != null && Vars.UpdateChecker.IsAlive)
string[] threadNames =
{
Vars.UpdateChecker.Interrupt();
while (Vars.UpdateChecker.IsAlive)
{
if (sw.ElapsedMilliseconds >= 10000) Environment.Exit(16);
Thread.Sleep(50);
}
}
Log("[OK] Shut down update checker.");
"reset timer",
"update checker",
"rate limiter",
"banned sweeper",
"configurations autosaver"
};


if (Vars.RateLimiter != null && Vars.RateLimiter.IsAlive)
for (int i = 0; i < threads.Length; i++)
{
Vars.RateLimiter.Interrupt();
while (Vars.RateLimiter.IsAlive)
var thread = threads[i];
if (thread != null && thread.IsAlive)
{
if (sw.ElapsedMilliseconds >= 10000) Environment.Exit(16);
Thread.Sleep(50);
thread.Interrupt();
while (thread.IsAlive)
{
if (sw.ElapsedMilliseconds >= 10000) Environment.Exit(16);
Thread.Sleep(50);
}
}
Log($"[OK] Shut down {threadNames[i]}.");
}
Log("[OK] Shut down rate limiter.");

if (Vars.BannedSweepper != null && Vars.BannedSweepper.IsAlive)
{
Vars.BannedSweepper.Interrupt();
while (Vars.BannedSweepper.IsAlive)
{
if (sw.ElapsedMilliseconds >= 10000) Environment.Exit(16);
Thread.Sleep(50);
}
}
Log("[OK] Shut down banned sweeper.");

if (Vars.SyncConf != null && Vars.SyncConf.IsAlive)
{
Vars.SyncConf.Interrupt();
while (Vars.SyncConf.IsAlive)
{
if (sw.ElapsedMilliseconds >= 10000) Environment.Exit(16);
Thread.Sleep(50);
}
}
Log("[OK] Shut down configurations autosaver.");
sw.Stop();
Log($"pmcenter has stopped in {Math.Round(sw.Elapsed.TotalSeconds, 2)}s.");
Environment.Exit(code);
}
}
}
}
1 change: 1 addition & 0 deletions pmcenter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public static async Task MainAsync(string[] args)
{
Vars.Bot = new TelegramBotClient(Vars.CurrentConf.APIKey);
}
Log("Validating API Key...");
_ = await Vars.Bot.TestApiAsync().ConfigureAwait(false);
Log("Hooking event processors...");
Vars.Bot.OnUpdate += BotProcess.OnUpdate;
Expand Down

0 comments on commit 4de73d4

Please sign in to comment.