<commit_before>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace SoraBot.Services.Utils
{
    public class HealthChecker
    {
        private readonly IHostApplicationLifetime _applicationLifetime;
        private readonly DiscordSocketClient _client;
        private readonly ILogger<HealthChecker> _log;
        private readonly Timer _timer;

        private int _disconnectedCounter = 0;

        public HealthChecker(
            IHostApplicationLifetime applicationLifetime,
            DiscordSocketClient client,
            ILogger<HealthChecker> log)
        {
            _applicationLifetime = applicationLifetime;
            _client = client;
            _log = log;
            _timer = new Timer(HealthCheck, null, TimeSpan.FromMinutes(30),
                TimeSpan.FromMinutes(30));
            
            _client.Disconnected += ClientOnDisconnected;
            _log.LogInformation("Initialized HealthChecker");
        }

        private Task ClientOnDisconnected(Exception ex)
        {
            _log.LogWarning(ex, "Client disconnected unexpectedly, increasing health monitoring...");
            _timer.Change(TimeSpan.FromMinutes(2), TimeSpan.FromMinutes(2));
            return Task.CompletedTask;
        }

        private void HealthCheck(object state)
        {
            if (_client.ConnectionState == ConnectionState.Connected)
            {
                _log.LogTrace("Connection state came out okey. Resetting timer to 30 minutes check");
                // This means the last check was also successful so let's decrease the amount of operations and memory r/w :)
                if (_disconnectedCounter == 0) return;
                
                _disconnectedCounter = 0;
                _timer.Change(TimeSpan.FromMinutes(30), TimeSpan.FromMinutes(30));
                return;
            }
            // We're not connected
            _disconnectedCounter++;
            if (_disconnectedCounter < 3) return;
            
            // Shut it down and let the systemd service restart it bcs d.net prolly threadlocked itself :)
            _log.LogError("Socket client failed to reset itself. Shutting down application...");
            _client.Disconnected -= ClientOnDisconnected;
            _applicationLifetime.StopApplication();

            // If the application lifetime doesn't work, cancel the application token.
            if (_disconnectedCounter > 5)
                GlobalConstants.ApplicationCancellationTokenSource.Cancel();
        }
    }
}<commit_msg>Lets see if this is enough to properly shut down the application...<commit_after>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace SoraBot.Services.Utils
{
    public class HealthChecker
    {
        private readonly IHostApplicationLifetime _applicationLifetime;
        private readonly DiscordSocketClient _client;
        private readonly ILogger<HealthChecker> _log;
        private readonly Timer _timer;

        private int _disconnectedCounter = 0;

        public HealthChecker(
            IHostApplicationLifetime applicationLifetime,
            DiscordSocketClient client,
            ILogger<HealthChecker> log)
        {
            _applicationLifetime = applicationLifetime;
            _client = client;
            _log = log;
            _timer = new Timer(HealthCheck, null, TimeSpan.FromMinutes(30),
                TimeSpan.FromMinutes(30));
            
            _client.Disconnected += ClientOnDisconnected;
            _log.LogInformation("Initialized HealthChecker");
        }

        private Task ClientOnDisconnected(Exception ex)
        {
            _log.LogWarning(ex, "Client disconnected unexpectedly, increasing health monitoring...");
            _timer.Change(TimeSpan.FromMinutes(2), TimeSpan.FromMinutes(2));
            return Task.CompletedTask;
        }

        private void HealthCheck(object state)
        {
            if (_client.ConnectionState == ConnectionState.Connected)
            {
                _log.LogTrace("Connection state came out okey. Resetting timer to 30 minutes check");
                // This means the last check was also successful so let's decrease the amount of operations and memory r/w :)
                if (_disconnectedCounter == 0) return;
                
                _disconnectedCounter = 0;
                _timer.Change(TimeSpan.FromMinutes(30), TimeSpan.FromMinutes(30));
                return;
            }
            // We're not connected
            _disconnectedCounter++;
            if (_disconnectedCounter < 3) return;
            
            // Shut it down and let the systemd service restart it bcs d.net prolly threadlocked itself :)
            _log.LogError("Socket client failed to reset itself. Shutting down application...");
            _client.Disconnected -= ClientOnDisconnected;
            GlobalConstants.ApplicationCancellationTokenSource.Cancel();
            _applicationLifetime.StopApplication();

        }
    }
}
{'fraction_non_alphanumeric': 0.07051891275422924, 'fraction_numerical': 0.006272571754419312, 'mean_word_length': 2.191024863553669, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3392831', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1228, 'n_words': 342}