Beispiel #1
0
        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();
            }
        }
Beispiel #2
0
        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);
            }
        }
Beispiel #3
0
        private TimeoutData MapToTimeoutData(TimeoutDataEntity entity)
        {
            if (entity == null)
            {
                return null;
            }

            TimeoutData timeoutData = new TimeoutData
            {
                Destination = Address.Parse(entity.Destination),
                Headers = entity.Headers.ToDictionary(),
                Id = entity.Id.ToString(),
                OwningTimeoutManager = entity.Endpoint,
                SagaId = entity.SagaId,
                State = entity.State,
                Time = entity.Time
            };

            return timeoutData;
        }
Beispiel #4
0
        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;
        }
Beispiel #5
0
        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;
            }
        }