Ejemplo n.º 1
0
        /// <summary>
        /// Try to get message with back off if waitFor is specified
        /// </summary>
        /// <param name="queueName">queue name</param>
        /// <param name="method">get message lambda</param>
        /// <param name="waitFor">wait for (optional)</param>
        /// <returns></returns>
        private async Task <InternalMessageV1> TryWithBackoff(string queueName, Func <Task <InternalMessageV1> > method, TimeSpan?waitFor)
        {
            InternalMessageV1 result = await method();

            if (result != null || waitFor == null)
            {
                return(result);
            }

            AutoResetEvent autoResetEvent;

            if (!_event.TryGetValue(queueName, out autoResetEvent))
            {
                autoResetEvent = null;
            }

            DateTime start = DateTime.Now;
            TimeSpan sleep = _startSleep;

            while (true)
            {
                if (DateTime.Now - start > waitFor)
                {
                    return(null);
                }

                bool signal = false;
                if (autoResetEvent == null)
                {
                    await Task.Delay(sleep);
                }
                else
                {
                    signal = autoResetEvent.WaitOne(sleep);
                }

                result = await method();

                if (result != null)
                {
                    return(result);
                }

                if (sleep < waitFor && sleep < _maxSleep)
                {
                    sleep += _incrementSleep;
                }
            }
        }
Ejemplo n.º 2
0
        public static MessageContractV1 ConvertTo(this InternalMessageV1 self)
        {
            if (self == null)
            {
                return(null);
            }

            return(new MessageContractV1
            {
                MessageId = self.MessageId,
                QueueId = self.QueueId,
                ClientMessageId = self.ClientMessageId,
                Cv = self.Cv,
                Payload = self.Payload,
                LockedDate = self.LockedDate,
                LockedByAgentId = self.LockedByAgentId,
                RetryCount = self.RetryCount,
                CreatedByAgentId = self.CreatedByAgentId,
                _createdDate = self._createdDate,
            });
        }