public void AddEventLog(IEventStorage eventStorage)
        {
            if (eventStorage == null)
            {
                throw new ArgumentNullException("eventStorage");
            }

            foreach (XmlNode testCaseNode in _testResultXml.GetElementsByTagName(TestCaseElementName))
            {
                var testCaseId = GetTestCaseId(testCaseNode);

                if (!eventStorage.Exist(testCaseId))
                {
                    continue;
                }

                XmlElement eventLogElement = _testResultXml.CreateElement("events");

                using (var stringWriter = new StringWriter())
                {
                    _jsonSerializer.Serialize(stringWriter, eventStorage.Get(testCaseId));

                    eventLogElement.AppendChild(
                        _testResultXml.CreateCDataSection(stringWriter.ToString())
                        );
                }

                testCaseNode.AppendChild(eventLogElement);
            }
        }
Esempio n. 2
0
        protected EventMessageHeader PopMessage(IEventStorage storage, string queueName, bool withBody = false, bool backup = false)
        {
            var msgKey = backup ? storage.RightPopLeftPush(queueName, queueName + this.BackupQueueSuffix) : storage.RightPop(queueName);

            if (msgKey == null)
            {
                return(null);
            }
            var messageId = msgKey.ToGuidString();
            EventMessageHeader header;

            if (withBody)
            {
                var result = storage.MultiGet(this.MessageHeaderPrefix + messageId, this.MessagePrefix + messageId);
                if (result == null || result.Length < 2)
                {
                    return(null);
                }
                header         = this.Serializer.Deserialize <EventMessageHeader>(result[0], null);
                header.Message = this.Serializer.Deserialize <EventMessage>(result[1], null);
            }
            else
            {
                var result = storage.Get(this.MessageHeaderPrefix + messageId);
                if (result == null || result.Length <= 0)
                {
                    return(null);
                }
                header = this.Serializer.Deserialize <EventMessageHeader>(result, null);
            }
            return(header);
        }
Esempio n. 3
0
        //移除不活跃的订阅(默认24小时前)
        protected void RemoveSubscriber(IEventStorage storage, EventMessage subscriber)
        {
            //判断队列是否有消息
            var queueName = subscriber.ChannelID;
            var qkey      = this.SubscriberQueuePrefix + queueName;
            var qlength   = storage.GetLength(qkey);

            if (qlength <= 0)             //没有任何消息,忽略(实际上此队列在Redis中是不存在的)
            {
                return;
            }
            //24小时前
            var now      = DateTime.Now;
            var minTicks = subscriber.Priority > 0 ? now.AddSeconds(-subscriber.Priority).Ticks : now.AddHours(-24).Ticks;
            //获取最后一次活跃时间
            var qfkey  = this.SubscriberRefKeyPrefix + queueName;
            var bytes  = storage.Get(qfkey);
            var remove = true;

            if (bytes != null && bytes.Length > 0)
            {
                var ticks = BitConverter.ToInt64(bytes, 0);
                if (ticks > minTicks)                 //还是活跃的
                {
                    remove = false;
                }
            }
            if (!remove)             //此订阅还是活跃的,忽略
            {
                return;
            }
            //把订阅信息移除
            var sk = this.SubscriberKeyPrefix + queueName;

            storage.Delete(sk);
            //把队列里的消息引用数-1
            var mlist = new List <string>();

            do
            {
                bytes = storage.RightPop(qkey);
                if (bytes != null && bytes.Length > 0)
                {
                    var messageId = bytes.ToGuidString();
                    mlist.Add(this.MessageRefPrefix + messageId);
                }
            } while(bytes != null);
            //把订阅队列移除
            storage.Pipeline(s => {
                foreach (var msgid in mlist)
                {
                    s.Decrement(msgid);
                }
                s.Delete(qkey);
            });
        }
Esempio n. 4
0
        private async Task <TAggregate> LoadAggregate <TAggregate>(Guid id)
            where TAggregate : AggregateRoot
        {
            var events = await _eventStorage.Get(id, -1, typeof(TAggregate).GetCollectionName());

            if (!events.Any())
            {
                throw new AggregateNotFoundException(id);
            }
            var aggregate = AggregateRootFactory <TAggregate> .CreateAggregate(id);

            aggregate.LoadFromHistory(events);
            return(aggregate);
        }
Esempio n. 5
0
        public async Task <T> Get <T>(Guid aggregateId)
            where T : AggregateRoot
        {
            var aggregate = AggregateRootFactory <T> .CreateAggregate(aggregateId);

            var snapshotVersion = await TryRestoreAggregateFromSnapshot(aggregateId, aggregate);


            IEnumerable <Event> events;

            if (snapshotVersion == -1)
            {
                events = await _eventStorage.Get(aggregateId, -1, typeof(T).GetCollectionName());

                if (!events.Any())
                {
                    throw new AggregateNotFoundException(aggregateId);
                }
            }
            else
            {
                events = await _eventStorage.Get(aggregateId, snapshotVersion, typeof(T).GetCollectionName());
            }

            if (events.Any())
            {
                aggregate.LoadFromHistory(events);
            }


            if (aggregate.Removed)
            {
                throw new AggregateRemovedException(aggregate.Id);
            }

            return(aggregate);
        }
Esempio n. 6
0
        protected void PersistMessages(IEventStorage storage, string[] keys)          //消息持久化(备查)
        {
            if (keys == null || keys.Length <= 0)
            {
                return;
            }
            var result = storage.MultiGet(keys);

            if (result == null || result.Length <= 0)
            {
                return;
            }
            var list = new List <EventMessage>();

            foreach (var bytes in result)
            {
                if (bytes == null || bytes.Length <= 0)
                {
                    continue;
                }
                var message = this.Serializer.Deserialize <EventMessage>(bytes, null);
                if (message != null)
                {
                    //把被分发到的订阅者也一并保存
                    var    dsptBytes = storage.Get(this.MessageDispatchPrefix + message.MessageGuid);
                    string channels  = null;
                    if (dsptBytes != null && dsptBytes.Length > 0)
                    {
                        channels = this.Encoding.GetString(dsptBytes);
                    }
                    if (!string.IsNullOrEmpty(channels))
                    {
                        message.Data["__Channels__"] = channels;
                    }
                    list.Add(message);
                }
            }
            if (list.Count > 0)
            {
                storage.PersistToDatabase(list.ToArray());
            }
        }