<commit_before>﻿using System;
using System.Threading.Tasks;

using Agatha.Common;

using Common.Logging;

using Naru.Core;

namespace Naru.Agatha
{
    public class RequestTask : IRequestTask
    {
        private readonly ILog _log;
        private readonly Func<IRequestDispatcher> _requestDispatcher;

        public RequestTask(ILog log, Func<IRequestDispatcher> requestDispatcher)
        {
            _log = log;
            _requestDispatcher = requestDispatcher;
        }

        public Task<TResponse> Get<TRequest, TResponse>(TRequest request)
            where TRequest : Request<TResponse>
            where TResponse : Response
        {
            return Task.Factory.StartNew(() => Execute<TRequest, TResponse>(request));
        }

        private TResponse Execute<TRequest, TResponse>(TRequest request)
            where TRequest : Request<TResponse>
            where TResponse : Response
        {
            using (var performanceTester = new PerformanceTester())
            {
                request.Id = Guid.NewGuid().ToString();

                _log.Debug(string.Format("Start RequestTask {0}, Id - {1}", typeof(TRequest), request.Id));

                var response = _requestDispatcher().Get<TResponse>(request);

                if (response.Exception != null)
                    throw new RequestException(response.Exception.Message);

                _log.Debug(string.Format("Finished RequestTask {0}, Id - {1}. Duration {2}",
                    typeof(TRequest),
                    request.Id,
                    performanceTester.Result.Milliseconds));

                return response;<commit_msg>resource cleanup of request dispatcher<commit_after>﻿using System;
using System.Threading.Tasks;

using Agatha.Common;

using Common.Logging;

using Naru.Core;

namespace Naru.Agatha
{
    public class RequestTask : IRequestTask
    {
        private readonly ILog _log;
        private readonly Func<IRequestDispatcher> _requestDispatcher;

        public RequestTask(ILog log, Func<IRequestDispatcher> requestDispatcher)
        {
            _log = log;
            _requestDispatcher = requestDispatcher;
        }

        public Task<TResponse> Get<TRequest, TResponse>(TRequest request)
            where TRequest : Request<TResponse>
            where TResponse : Response
        {
            return Task.Factory.StartNew(() => Execute<TRequest, TResponse>(request));
        }

        private TResponse Execute<TRequest, TResponse>(TRequest request)
            where TRequest : Request<TResponse>
            where TResponse : Response
        {
            using (var performanceTester = new PerformanceTester())
            {
                request.Id = Guid.NewGuid().ToString();

                _log.Debug(string.Format("Start RequestTask {0}, Id - {1}", typeof(TRequest), request.Id));

                using (var requestDispatcher = _requestDispatcher())
                {
                    var response = requestDispatcher.Get<TResponse>(request);
    
                    if (response.Exception != null)
                        throw new RequestException(response.Exception.Message);
    
                    _log.Debug(string.Format("Finished RequestTask {0}, Id - {1}. Duration {2}",
                        typeof(TRequest),
                        request.Id,
                        performanceTester.Result.Milliseconds));
    
                    return response;
                }
{'fraction_non_alphanumeric': 0.08885672937771345, 'fraction_numerical': 0.002894356005788712, 'mean_word_length': 1.9948006932409013, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '2729205', 'n_tokens_mistral': 877, 'n_tokens_neox': 817, 'n_words': 195}