private void Receive() { if (_stopped) { return; } var t = Stopwatch.StartNew(); _client.ReceiveMax(_queue, 10.Minutes(), new Result <IEnumerable <AwsSqsMessage> >()).WhenDone(r => { if (r.HasException) { Exception = r.Exception; return; } if (_stopped) { return; } var received = new List <AwsSqsMessage>(r.Value); if (!received.Any()) { _log.DebugFormat("{0}: no messages in queue, sleeping before retry", Id); AsyncUtil.Sleep(1.Seconds(), new Result(TimeSpan.MaxValue)).WhenDone(r2 => Receive()); return; } _log.DebugFormat("{0}: received {1} messages", Id, received.Count); Delete(received, 0, t); }); }
//--- Methods --- private IEnumerator <IYield> PollSqs(Result result) { _log.DebugFormat("polling SQS queue '{0}'", _queuename); while (!_isDisposed) { Result <IEnumerable <AwsSqsMessage> > messageResult; yield return(messageResult = _client.ReceiveMax(_queuename, new Result <IEnumerable <AwsSqsMessage> >()).Catch()); if (messageResult.HasException) { LogError(messageResult.Exception, "fetching messages"); result.Return(); yield break; } var messages = messageResult.Value; if (!messages.Any()) { result.Return(); yield break; } foreach (var msg in messages.Where(msg => !_cache.SetOrUpdate(msg.MessageId, _cacheTimer))) { try { _log.DebugFormat("dispatching message '{0}' from queue '{1}'", msg.MessageId, _queuename); _callback(msg); } catch (Exception e) { _log.Warn( string.Format("dispatching message '{0}' from queue '{1}' threw '{2}': {3}", msg.MessageId, _queuename, e, e.Message ), e ); continue; } Result <AwsSqsResponse> deleteResult; yield return(deleteResult = _client.Delete(msg, new Result <AwsSqsResponse>()).Catch()); if (deleteResult.HasException) { LogError(deleteResult.Exception, string.Format("deleting message '{0}'", msg.MessageId)); } else { _cache.SetOrUpdate(msg.MessageId, _cacheTimer); } } } result.Return(); }