<commit_before>﻿using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using log4net;
using Stardust.Node.Extensions;
using Stardust.Node.Helpers;
using Stardust.Node.Interfaces;
using Timer = System.Timers.Timer;

namespace Stardust.Node.Timers
{
    public class PingToManagerTimer : Timer
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof (PingToManagerTimer));

        public PingToManagerTimer(INodeConfiguration nodeConfiguration,
                                  Uri callbackUri,
                                  double interval = 10000) : base(interval)
        {
            nodeConfiguration.ThrowArgumentNullException();
            callbackUri.ThrowArgumentNullExceptionWhenNull();

            CancellationTokenSource = new CancellationTokenSource();

            NodeConfiguration = nodeConfiguration;
            CallbackUri = callbackUri;

            WhoAmI = NodeConfiguration.CreateWhoIAm(Environment.MachineName);

            Elapsed += OnTimedEvent;

            AutoReset = true;
        }

        protected override void Dispose(bool disposing)
        {
            LogHelper.LogDebugWithLineNumber(Logger,"Start disposing.");

            if (CancellationTokenSource != null &&
                !CancellationTokenSource.IsCancellationRequested)
            {
                CancellationTokenSource.Cancel();
            }

            base.Dispose(disposing);

            LogHelper.LogDebugWithLineNumber(Logger, "Finished disposing.");
        }

        public string WhoAmI { get; private set; }

        public INodeConfiguration NodeConfiguration { get; private set; }

        public Uri CallbackUri { get; private set; }


        private async Task<HttpResponseMessage> SendPing(Uri nodeAddress,
                                                         CancellationToken cancellationToken)
        {
            var httpResponseMessage =
                await nodeAddress.PostAsync(CallbackUri,
                                            cancellationToken);

            return httpResponseMessage;
        }

        private CancellationTokenSource CancellationTokenSource { get; set; }

        private async void OnTimedEvent(object sender,
                                        ElapsedEventArgs e)
        {
            try
            {
                await SendPing(NodeConfiguration.BaseAddress,
                               CancellationTokenSource.Token);
            }

            catch
            {
                LogHelper.LogErrorWithLineNumber(Logger,
                                                 WhoAmI + ": Heartbeat failed. Is the manager up and running?");
            }<commit_msg>error to info in heartbeat<commit_after>﻿using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using log4net;
using Stardust.Node.Extensions;
using Stardust.Node.Helpers;
using Stardust.Node.Interfaces;
using Timer = System.Timers.Timer;

namespace Stardust.Node.Timers
{
    public class PingToManagerTimer : Timer
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof (PingToManagerTimer));

        public PingToManagerTimer(INodeConfiguration nodeConfiguration,
                                  Uri callbackUri,
                                  double interval = 10000) : base(interval)
        {
            nodeConfiguration.ThrowArgumentNullException();
            callbackUri.ThrowArgumentNullExceptionWhenNull();

            CancellationTokenSource = new CancellationTokenSource();

            NodeConfiguration = nodeConfiguration;
            CallbackUri = callbackUri;

            WhoAmI = NodeConfiguration.CreateWhoIAm(Environment.MachineName);

            Elapsed += OnTimedEvent;

            AutoReset = true;
        }

        protected override void Dispose(bool disposing)
        {
            LogHelper.LogDebugWithLineNumber(Logger,"Start disposing.");

            if (CancellationTokenSource != null &&
                !CancellationTokenSource.IsCancellationRequested)
            {
                CancellationTokenSource.Cancel();
            }

            base.Dispose(disposing);

            LogHelper.LogDebugWithLineNumber(Logger, "Finished disposing.");
        }

        public string WhoAmI { get; private set; }

        public INodeConfiguration NodeConfiguration { get; private set; }

        public Uri CallbackUri { get; private set; }


        private async Task<HttpResponseMessage> SendPing(Uri nodeAddress,
                                                         CancellationToken cancellationToken)
        {
            var httpResponseMessage =
                await nodeAddress.PostAsync(CallbackUri,
                                            cancellationToken);

            return httpResponseMessage;
        }

        private CancellationTokenSource CancellationTokenSource { get; set; }

        private async void OnTimedEvent(object sender,
                                        ElapsedEventArgs e)
        {
            try
            {
                await SendPing(NodeConfiguration.BaseAddress,
                               CancellationTokenSource.Token);
            }

            catch
            {
                LogHelper.LogInfoWithLineNumber(Logger,
                                                 WhoAmI + ": Heartbeat failed. Is the manager up and running?");
            }
{'fraction_non_alphanumeric': 0.06231778425655977, 'fraction_numerical': 0.002186588921282799, 'mean_word_length': 1.7569060773480663, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4876605', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1205, 'n_words': 270}