public async Task RequestTimeOut(string writeTo, Guid id, object message, DateTime time, IDictionary <string, object> metaData) { var timeOutManager = TimeOutManager.GetCurrent(); if (timeOutManager == null) { throw new InvalidOperationException("There is no timeout manager"); } await timeOutManager.Add(new TimeoutData(writeTo, id, message, time, metaData)).ConfigureAwait(false); }
private async Task Poll(CancellationToken cancellationToken) { var startSlice = DateTime.UtcNow.AddYears(-10); while (!cancellationToken.IsCancellationRequested) { if (_nextRetrieval > DateTime.UtcNow) { await Task.Delay(_secondsToSleepBetweenPolls * 1000, cancellationToken).ConfigureAwait(false); continue; } var timeOutManager = TimeOutManager.GetCurrent(); if (timeOutManager == null) { return; } var nextExpiredTimeout = await timeOutManager.GetNextChunk(startSlice, x => { if (startSlice < x.Item2) { startSlice = x.Item2; } return(_eventStoreConnection.AppendToStreamAsync(x.Item1.WriteTo, ExpectedVersion.Any, ToEventData(x.Item1.Id, x.Item1.Message, x.Item1.MetaData))); }).ConfigureAwait(false); _nextRetrieval = nextExpiredTimeout; lock (_lockObject) { if (nextExpiredTimeout < _nextRetrieval) { _nextRetrieval = nextExpiredTimeout; } } var maxNextRetrieval = DateTime.UtcNow + TimeSpan.FromMinutes(1); if (_nextRetrieval > maxNextRetrieval) { _nextRetrieval = maxNextRetrieval; } } }