public void Add(TimeoutData timeout) { Logger.Debug("Adding timeout"); if (timeout == null) { Logger.Debug("Timeout is null! Throwing"); throw new ArgumentNullException("timeout"); } Guid timeoutId = CombGuid.NewGuid(); Logger.DebugFormat("Created new comb guid for timeout ID {0}", timeoutId); TimeoutDataEntity timeoutEntity = new TimeoutDataEntity { Destination = timeout.Destination.ToString(), Endpoint = timeout.OwningTimeoutManager, Headers = timeout.Headers.ToDictionaryString(), Id = timeoutId, SagaId = timeout.SagaId, State = timeout.State, Time = timeout.Time }; using (ITimeoutDbContext dbc = _dbContextFactory.CreateTimeoutDbContext()) { Logger.Debug("Saving timeout entity"); dbc.Timeouts.Add(timeoutEntity); dbc.SaveChanges(); } }
public TimeoutData Peek(string timeoutId) { Logger.DebugFormat("Peeking for timeout with id {0}", timeoutId); using (ITimeoutDbContext dbc = _dbContextFactory.CreateTimeoutDbContext()) { TimeoutDataEntity entity = dbc.Timeouts.Find(Guid.Parse(timeoutId)); Logger.DebugFormat("Timeout found? {0}", entity != null); return MapToTimeoutData(entity); } }
public void RemoveTimeoutBy(Guid sagaId) { Logger.DebugFormat("Removing timeout for saga ID {0}", sagaId); if (sagaId == Guid.Empty) { Logger.Debug("sageId is empty! Throwing"); throw new ArgumentException("sagaId parameter cannot be empty.", "sagaId"); } using (ITimeoutDbContext dbc = _dbContextFactory.CreateTimeoutDbContext()) { IQueryable<TimeoutDataEntity> toDelete = dbc.Timeouts.Where(t => t.SagaId == sagaId); Logger.DebugFormat("{0} timeout(s) found for saga.", toDelete.Count()); dbc.Timeouts.RemoveRange(toDelete); dbc.SaveChanges(); } }
public bool TryRemove(string timeoutId) { Logger.DebugFormat("Trying to remove timeout with Id {0}.", timeoutId); using (ITimeoutDbContext dbc = _dbContextFactory.CreateTimeoutDbContext()) { Guid timeoutGuid = Guid.Parse(timeoutId); TimeoutDataEntity entity = dbc.Timeouts.Find(timeoutGuid); if (entity == null) { Logger.Debug("Didn't find timeout!"); return false; } Logger.Debug("Found timeout, removing."); dbc.Timeouts.Remove(entity); dbc.SaveChanges(); } return true; }
public IEnumerable<Tuple<string, DateTime>> GetNextChunk(DateTime startSlice, out DateTime nextTimeToRunQuery) { Logger.DebugFormat("Getting next chuch with start slice {0}", startSlice); DateTime now = DateTime.UtcNow; using (ITimeoutDbContext dbc = _dbContextFactory.CreateTimeoutDbContext()) { Logger.Debug("Querying for matching timeouts within time slice"); List<TimeoutDataEntity> matchingTimeouts = dbc.Timeouts .Where( t => t.Endpoint == EndpointName && t.Time >= startSlice && t.Time <= now) .OrderBy(t => t.Time) .ToList(); Logger.DebugFormat("{0} timeout(s) found.", matchingTimeouts.Count); List<Tuple<string, DateTime>> chunks = matchingTimeouts .Select(t => new Tuple<string, DateTime>(t.Id.ToString(), t.Time)) .ToList(); TimeoutDataEntity startOfNextChunk = dbc.Timeouts .Where(t => t.Endpoint == EndpointName && t.Time > now) .OrderBy(t => t.Time) .Take(1) .SingleOrDefault(); nextTimeToRunQuery = startOfNextChunk != null ? startOfNextChunk.Time : DateTime.UtcNow.AddMinutes(10); Logger.DebugFormat("Next time to run query calculated to be {0}.", nextTimeToRunQuery); return chunks; } }