Exemplo n.º 1
0
        public void Process(ServiceConfiguration configuration, MessageEnvelope envelope)
        {
            var watch = Stopwatch.StartNew();

            Trace("Processing message of type '{0}' for oranisation with Id;={1}",
                  envelope.MessageType,
                  envelope.OrganisationId);

            try
            {
                for (int attempt = 1; attempt <= configuration.ConcurrencyRetryLimit; attempt++)
                {
                    try
                    {
                        Trace("Attempt {0} processing message", attempt);
                        TryProcessMessage(envelope);
                        Trace("Attempt {0} successfully processed message", attempt);
                        break;
                    }
                    catch (ConcurrencyException ex)
                    {
                        Trace("Concurrency exception proccessing attempt #{0}, message:{1}", attempt, ex.Message);

                        //failed on last attempt so rethrow the error
                        if (attempt == configuration.ConcurrencyRetryLimit)
                        {
                            throw;
                        }
                    }

                    if (configuration.ConcurrencyRetryDelayMilliseconds > 0)
                    {
                        Thread.Sleep(configuration.ConcurrencyRetryDelayMilliseconds);
                    }
                }
            }
            catch (Exception e)
            {
                Trace("Failed all attempts for message with Id:{0}", envelope.Id);

                using (ObjectFactory.Container.BeginScope())
                {
                    using (var session = ObjectFactory.GetObject <IAppSession>())
                    {
                        envelope.ErrorMessage = e.Message;

                        Trace("Message for Organisation:={0} failed, logging to RavenDB", envelope.OrganisationId);

                        session.MasterRaven.Store(envelope);
                        session.Commit();
                    }

                    e.Data.Add("MessageEnvelopeId", envelope.FriendlyId);
                    Error(e);
                    ErrorditeClient.ReportException(e);
                }
            }

            Trace("Processing for message with Id:={0} completed in {1}ms", envelope.Id, watch.ElapsedMilliseconds);
        }
Exemplo n.º 2
0
        public void Generate()
        {
            /*
             * Asos Test Token: i3OVOLrniDWzv3YLY1pNcA==
             * Marketplace Test Token: 4b4stuqfB7tVTZv4nYbXOw==
             */
            Stopwatch watch = Stopwatch.StartNew();

            for (int i = 0; i < 5; i++)
            {
                try
                {
                    if (i % 3 == 0)
                    {
                        throw new ArgumentException("No such value for parameter named Bob!, iteration:={0}".FormatWith(i));
                    }
                    if (i % 3 == 1)
                    {
                        throw new InvalidOperationException("You cant do that to this thing!, iteration:={0}".FormatWith(i));
                    }

                    throw new ErrorditeConfigurationException("You specified invalid configuration!, iteration:={0}".FormatWith(i));
                }
                catch (Exception e)
                {
                    ErrorditeClient.ReportException(e, false);
                }
            }

            Console.WriteLine(watch.ElapsedMilliseconds + "ms");
        }
Exemplo n.º 3
0
        public static void Main(string[] args)
        {
            try
            {
                var task = ParseCommandLine(Environment.CommandLine);

                if (task.IsNullOrEmpty())
                {
                    Trace.Write("Failed to parse task from command line");
                    return;
                }

                string ravenInstanceId = Environment.GetEnvironmentVariable("raveninstanceid");
                if (ravenInstanceId.IsNullOrEmpty())
                {
                    Trace.Write("No raven instance set, defaulting to instance #1");
                    ravenInstanceId = "1";
                }

                ObjectFactory.Container.Install(new TasksMasterInstaller());

                try
                {
                    ObjectFactory.GetObject <ITask>(task).Execute(ravenInstanceId);
                }
                catch (Exception e)
                {
                    e.Data.Add("task", task);
                    e.Data.Add("ravenInstanceId", ravenInstanceId);
                    ObjectFactory.GetObject <IComponentAuditor>().Error(typeof(Program), e);
                    ErrorditeClient.ReportException(e);
                }
            }
            catch (Exception e)
            {
                Trace.Write(e.ToString());
            }
        }
Exemplo n.º 4
0
        private void ReceiveFromQueue()
        {
            int emptyReceiptCount = 0;

            while (_serviceRunning)
            {
                try
                {
                    var request = new ReceiveMessageRequest
                    {
                        QueueUrl            = _queueUrl,
                        MaxNumberOfMessages = _serviceConfiguration.MaxNumberOfMessagesPerReceive,
                        WaitTimeSeconds     = 20
                    };

                    Trace("Attmepting to receive message from queue '{0}'", _queueUrl);

                    ReceiveMessageResponse response;

                    try
                    {
                        response = _amazonSQS.ReceiveMessage(request);
                    }
                    catch (AmazonSQSException e)
                    {
                        //create the queue if the exception indicates the queue does not exist
                        if (e.Message.ToLowerInvariant().Contains("the specified queue does not exist"))
                        {
                            _createSQSQueueCommand.Invoke(new CreateSQSQueueRequest
                            {
                                QueueName = _queueUrl.GetQueueName()
                            });
                        }

                        emptyReceiptCount = 9;                         //equiv to 3 mins pause, the maximum we wait
                        Thread.Sleep(_requestThrottler.GetDelayMilliseconds(emptyReceiptCount));
                        continue;
                    }

                    if (!response.IsSetReceiveMessageResult() || response.ReceiveMessageResult.Message.Count == 0)
                    {
                        emptyReceiptCount++;
                        Trace("No message returned from queue '{0}', zero message count:={1}", _queueUrl, emptyReceiptCount);

                        //sleep for delay as specified by throttler (unless instructed to poll now)
                        int       delay       = _requestThrottler.GetDelayMilliseconds(emptyReceiptCount);
                        const int sleepPeriod = 100;
                        int       sleepCount  = 0;
                        while (sleepPeriod * ++sleepCount < delay)
                        {
                            if (_pollNow)
                            {
                                emptyReceiptCount = 0;
                                _pollNow          = false;
                                break;
                            }
                            Thread.Sleep(sleepPeriod);
                        }
                        continue;
                    }

                    //reset the zero message count
                    emptyReceiptCount = 0;

                    Trace("Received {0} messages from queue '{1}'", response.ReceiveMessageResult.Message.Count, _queueUrl);

                    foreach (var message in response.ReceiveMessageResult.Message)
                    {
                        var envelope = GetEnvelope(message);

                        Trace("Receiving message for organisation:={0}", envelope.OrganisationId);

                        try
                        {
                            ObjectFactory.GetObject <IMessageProcessor>().Process(_serviceConfiguration, envelope);

                            _amazonSQS.DeleteMessage(new DeleteMessageRequest
                            {
                                QueueUrl      = _queueUrl,
                                ReceiptHandle = envelope.ReceiptHandle
                            });
                        }
                        catch (Exception e)
                        {
                            var de = new ErrorditeDeleteSQSMessageException("Failed to delete message with receipt handle:={0}".FormatWith(envelope.ReceiptHandle), true, e);

                            if (e is ThreadAbortException)
                            {
                                continue;
                            }

                            Error(de);
                            ErrorditeClient.ReportException(de);
                        }
                    }
                }
                catch (Exception e)
                {
                    if (e is ThreadAbortException)
                    {
                        continue;
                    }

                    Error(e);
                    ErrorditeClient.ReportException(e);
                }
            }
        }