<commit_before>﻿using System.Linq;
using Amazon.Runtime;
using Amazon.SQS;
using Amazon.SQS.Model;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

#if ROCKLIB
using System.Diagnostics;
#else
using Rock.BackgroundErrorLogging;
#endif

#if ROCKLIB
namespace RockLib.Messaging.SQS
#else
namespace Rock.Messaging.SQS
#endif
{
    public class SQSQueueReceiver : IReceiver
    {
        private readonly string _name;
        private readonly string _queueUrl;
        private readonly int _maxMessages;
        private readonly bool _autoAcknwoledge;
        private readonly bool _parallelHandling;
        private readonly IAmazonSQS _sqs;
        private readonly Thread _worker;

        private bool _stopped;

        public SQSQueueReceiver(ISQSConfiguration configuration, IAmazonSQS sqs)
        {
            _name = configuration.Name;
            _queueUrl = configuration.QueueUrl;
            _maxMessages = configuration.MaxMessages;
            _autoAcknwoledge = configuration.AutoAcknowledge;
            _parallelHandling = configuration.ParallelHandling;

            _sqs = sqs;

            _worker = new Thread(DoStuff);
        }

        public event EventHandler<MessageReceivedEventArgs> MessageReceived;

        public string Name
        {
            get { return _name; }
        }

        public void Start(string selector)
        {
            if (!_worker.IsAlive && !_stopped)
            {
                _worker.Start();
            }
        }

        private void DoStuff()
        {
            while (!_stopped)
            {
                var receiveMessageRequest = new ReceiveMessageRequest
                {
                    MaxNumberOfMessages = _maxMessages,
                    QueueUrl = _queueUrl,
                    MessageAttributeNames = new List<string> { "*" }
                };

                ReceiveMessageResponse response = null;
                Exception exception = null;

                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        response = Sync.OverAsync(() => _sqs.ReceiveMessageAsync(receiveMessageRequest));

                        if (response.HttpStatusCode == HttpStatusCode.OK)
                        {
                            exception = null;
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }

                if (exception != null || response == null || response.HttpStatusCode != HttpStatusCode.OK)
                {
#if ROCKLIB
                    Trace.TraceError($"Unable to receive SQS messages from AWS. Additional Information - {GetAdditionalInformation(response, null)}");
#else
                    BackgroundErrorLogger.Log(
                        exception,
                        "Unable to receive SQS messages from AWS.",
                        "Rock.Messaging.SQS",
                        GetAdditionalInformation(response, null));
#endif
                    continue;
                }

                if (_parallelHandling)
                {
                    Parallel.ForEach(response.Messages, Handle);
                }
                else
                {
                    foreach (var message in response.Messages)
                    {
                        this.Handle(message);
                    }
                }
            }
        }
<commit_msg>Removing this. for consistency.<commit_after>﻿using System.Linq;
using Amazon.Runtime;
using Amazon.SQS;
using Amazon.SQS.Model;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

#if ROCKLIB
using System.Diagnostics;
#else
using Rock.BackgroundErrorLogging;
#endif

#if ROCKLIB
namespace RockLib.Messaging.SQS
#else
namespace Rock.Messaging.SQS
#endif
{
    public class SQSQueueReceiver : IReceiver
    {
        private readonly string _name;
        private readonly string _queueUrl;
        private readonly int _maxMessages;
        private readonly bool _autoAcknwoledge;
        private readonly bool _parallelHandling;
        private readonly IAmazonSQS _sqs;
        private readonly Thread _worker;

        private bool _stopped;

        public SQSQueueReceiver(ISQSConfiguration configuration, IAmazonSQS sqs)
        {
            _name = configuration.Name;
            _queueUrl = configuration.QueueUrl;
            _maxMessages = configuration.MaxMessages;
            _autoAcknwoledge = configuration.AutoAcknowledge;
            _parallelHandling = configuration.ParallelHandling;

            _sqs = sqs;

            _worker = new Thread(DoStuff);
        }

        public event EventHandler<MessageReceivedEventArgs> MessageReceived;

        public string Name
        {
            get { return _name; }
        }

        public void Start(string selector)
        {
            if (!_worker.IsAlive && !_stopped)
            {
                _worker.Start();
            }
        }

        private void DoStuff()
        {
            while (!_stopped)
            {
                var receiveMessageRequest = new ReceiveMessageRequest
                {
                    MaxNumberOfMessages = _maxMessages,
                    QueueUrl = _queueUrl,
                    MessageAttributeNames = new List<string> { "*" }
                };

                ReceiveMessageResponse response = null;
                Exception exception = null;

                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        response = Sync.OverAsync(() => _sqs.ReceiveMessageAsync(receiveMessageRequest));

                        if (response.HttpStatusCode == HttpStatusCode.OK)
                        {
                            exception = null;
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }

                if (exception != null || response == null || response.HttpStatusCode != HttpStatusCode.OK)
                {
#if ROCKLIB
                    Trace.TraceError($"Unable to receive SQS messages from AWS. Additional Information - {GetAdditionalInformation(response, null)}");
#else
                    BackgroundErrorLogger.Log(
                        exception,
                        "Unable to receive SQS messages from AWS.",
                        "Rock.Messaging.SQS",
                        GetAdditionalInformation(response, null));
#endif
                    continue;
                }

                if (_parallelHandling)
                {
                    Parallel.ForEach(response.Messages, Handle);
                }
                else
                {
                    foreach (var message in response.Messages)
                    {
                        Handle(message);
                    }
                }
            }
        }

{'fraction_non_alphanumeric': 0.06716521257443567, 'fraction_numerical': 0.000553939897521119, 'mean_word_length': 1.3021995537137392, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5438144', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1601, 'n_words': 397}