Beispiel #1
0
        protected async ValueTask<bool> RaiseEvent(IEventBase<K> @event, string uniqueId = null, string hashKey = null)
        {
            try
            {
                this.State.IncrementDoingVersion();//标记将要处理的Version
                @event.StateId = GrainId;
                @event.Version = this.State.Version + 1;
                @event.Timestamp = DateTime.UtcNow;
                using (var ms = new PooledMemoryStream())
                {
                    Serializer.Serialize(ms, @event);
                    var bytes = ms.ToArray();
                    var result = await EventStorage.SaveAsync(@event, bytes, uniqueId);
                    if (result)
                    {
                        EventHandle.Apply(this.State, @event);
                        await AfterEventSavedHandle(@event, bytes, hashKey: hashKey);

                        this.State.UpdateVersion(@event);//更新处理完成的Version

                        await SaveSnapshotAsync();
                        return true;
                    }
                    else
                        this.State.DecrementDoingVersion();//还原doing Version
                }
            }
            catch (Exception ex)
            {
                this.GetLogger("Event_Raise").Log(LogCodes.EventRaiseError, Orleans.Runtime.Severity.Error, $"applay event {@event.TypeCode} error, eventId={@event.Version}", null, ex);
                await OnActivateAsync();//重新激活Actor
                throw ex;
            }
            return false;
        }