Ejemplo n.º 1
0
        private void Run()
        {
            if (Interlocked.CompareExchange(ref status, WorkerStatus.Started, WorkerStatus.Starting) != WorkerStatus.Starting)
            {
                return;
            }
            timesStarted++;

            try
            {
                lock (msgLock)
                {
                    while (Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Started)
                    {
                        receivedNewMsgs = false;

                        using (var mqClient = new RedisMessageQueueClient(clientsManager))
                        {
                            var msgsProcessedThisTime = messageHandler.ProcessQueue(mqClient, QueueName,
                                                                                    () => Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Started);

                            totalMessagesProcessed += msgsProcessedThisTime;

                            if (msgsProcessedThisTime > 0)
                            {
                                lastMsgProcessed = DateTime.UtcNow;
                            }
                        }

                        if (!receivedNewMsgs)
                        {
                            Monitor.Wait(msgLock);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Ignore handling rare, but expected exceptions from KillBgThreadIfExists()
                if (ex is ThreadInterruptedException || ex is ThreadAbortException)
                {
                    Log.Warn("Received {0} in Worker: {1}".Fmt(ex.GetType().Name, QueueName));
                    return;
                }

                Stop();
                if (this.errorHandler != null)
                {
                    this.errorHandler(this, ex);
                }
            }
            finally
            {
                //If it's in an invalid state, Dispose() this worker.
                if (Interlocked.CompareExchange(ref status, WorkerStatus.Stopped, WorkerStatus.Stopping) != WorkerStatus.Stopping)
                {
                    Dispose();
                }
            }
        }
        public void Does_serialize_to_correct_MQ_name()
        {
            var message = new Message<Greet>(new Greet {Name = "Test"}) {};

            var mqClient = new RedisMessageQueueClient(new BasicRedisClientManager());

            mqClient.Publish(message);
        }
        public void Does_serialize_to_correct_MQ_name()
        {
            var message = new Message <Greet>(new Greet {
                Name = "Test"
            })
            {
            };

            var mqClient = new RedisMessageQueueClient(new BasicRedisClientManager());

            mqClient.Publish(message);
        }
Ejemplo n.º 4
0
        private void Run()
        {
            if (Interlocked.CompareExchange(ref status, WorkerStatus.Started, WorkerStatus.Starting) != WorkerStatus.Starting) return;
            timesStarted++;

            try
            {
                lock (msgLock)
                {
                    while (Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Started)
                    {
                        receivedNewMsgs = false;

                        using (var mqClient = new RedisMessageQueueClient(clientsManager))
                        {
                            var msgsProcessedThisTime = messageHandler.ProcessQueue(mqClient, QueueName,
                                () => Interlocked.CompareExchange(ref status, 0, 0) == WorkerStatus.Started);
                            
                            totalMessagesProcessed += msgsProcessedThisTime;

                            if (msgsProcessedThisTime > 0)
                                lastMsgProcessed = DateTime.UtcNow;
                        }

                        if (!receivedNewMsgs) 
                            Monitor.Wait(msgLock);
                    }
                }
            }
            catch (Exception ex)
            {
#if !NETSTANDARD1_6
                //Ignore handling rare, but expected exceptions from KillBgThreadIfExists()
                if (ex is ThreadInterruptedException || ex is ThreadAbortException)
                {
                    Log.Warn("Received {0} in Worker: {1}".Fmt(ex.GetType().Name, QueueName));
                    return;
                }
#endif

                Stop();
                if (this.errorHandler != null) this.errorHandler(this, ex);
            }
            finally
            {
                //If it's in an invalid state, Dispose() this worker.
                if (Interlocked.CompareExchange(ref status, WorkerStatus.Stopped, WorkerStatus.Stopping) != WorkerStatus.Stopping)
                {
                    Dispose();
                }
            }
        }