<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using JabbR.Models;
using JabbR.Services;
using JabbR.ViewModels;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Infrastructure;
using Ninject;

namespace JabbR
{
    public partial class Startup
    {
        // Background task info
        private static volatile bool _sweeping;
        private static Timer _backgroundTimer;
        private static readonly TimeSpan _sweepInterval = TimeSpan.FromMinutes(2);

        private static void StartBackgroundWork(IKernel kernel, IDependencyResolver resolver)
        {
            // Resolve the hub context, so we can broadcast to the hub from a background thread
            var connectionManager = resolver.Resolve<IConnectionManager>();

            // Start the sweeper
            _backgroundTimer = new Timer(_ =>
            {
                var hubContext = connectionManager.GetHubContext<Chat>();
                Sweep(kernel, hubContext);
            }, 
            null, 
            _sweepInterval, 
            _sweepInterval);

            // Clear all connections on app start
            ClearConnectedClients(kernel);
        }

        private static void ClearConnectedClients(IKernel kernel)
        {
            using (var repository = kernel.Get<IJabbrRepository>())
            {
                try
                {
                    repository.RemoveAllClients();
                    repository.CommitChanges();
                }
                catch (Exception ex)
                {
                    ReportError(ex);
                }
            }
        }

        private static void Sweep(IKernel kernel, IHubContext hubContext)
        {
            if (_sweeping)
            {
                return;
            }
<commit_msg>Start background work after 10 mins of app start.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using JabbR.Models;
using JabbR.Services;
using JabbR.ViewModels;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Infrastructure;
using Ninject;

namespace JabbR
{
    public partial class Startup
    {
        // Background task info
        private static volatile bool _sweeping;
        private static Timer _backgroundTimer;
        private static readonly TimeSpan _sweepInterval = TimeSpan.FromMinutes(2);
        private static readonly TimeSpan _sweepStart = TimeSpan.FromMinutes(8);

        private static void StartBackgroundWork(IKernel kernel, IDependencyResolver resolver)
        {
            // Resolve the hub context, so we can broadcast to the hub from a background thread
            var connectionManager = resolver.Resolve<IConnectionManager>();

            // Start the sweeper
            _backgroundTimer = new Timer(_ =>
            {
                var hubContext = connectionManager.GetHubContext<Chat>();
                Sweep(kernel, hubContext);
            }, 
            null,
            _sweepStart,
            _sweepInterval);

            // Clear all connections on app start
            ClearConnectedClients(kernel);
        }

        private static void ClearConnectedClients(IKernel kernel)
        {
            using (var repository = kernel.Get<IJabbrRepository>())
            {
                try
                {
                    repository.RemoveAllClients();
                    repository.CommitChanges();
                }
                catch (Exception ex)
                {
                    ReportError(ex);
                }
            }
        }

        private static void Sweep(IKernel kernel, IHubContext hubContext)
        {
            if (_sweeping)
            {
                return;
            }

{'fraction_non_alphanumeric': 0.06528033693077126, 'fraction_numerical': 0.0013161358252171624, 'mean_word_length': 1.7065527065527066, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '1998471', 'n_tokens_mistral': 936, 'n_tokens_neox': 885, 'n_words': 246}