/// <summary>
        ///  Enqueues an event onto the event queue
        /// </summary>
        public void ThrowEvent(object sender, IEventBase thrownEvent)
        {
            if (sender.IsNull() || thrownEvent.IsNull())
            {
                return;
            }
            if (!_events.ContainsKey(thrownEvent.GetType()))
            {
                return;
            }

            Log.DebugFormat("Locate Event to Throw {0}", thrownEvent.GetType());

            IList <EventListener> tupleList;

            _events.TryGetValue(thrownEvent.GetType(), out tupleList);
            if (tupleList.IsNull() || tupleList.Count == 0)
            {
                return;
            }

            Log.DebugFormat("ThrowEvent {0}/{1}", sender.GetType(), thrownEvent.GetType());

            thrownEvent.Sender = sender;
            _eventQueue.Enqueue(thrownEvent);

            if (!Timer.IsNotNull() || Timer.Enabled)
            {
                return;
            }
            Log.DebugFormat("Starting the Event Timer with interval frequency of {0}", Timer.Interval);
            Timer.Start();
        }
Beispiel #2
0
 public void TrackAppEvent(IEventBase smartHiveEvent)
 {
     try
     {
         Dictionary <string, string> propsDictionary = smartHiveEvent.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                                       .ToDictionary <PropertyInfo, string, string>(prop => prop.Name, prop => prop.GetValue(smartHiveEvent, null).ToString());
         HockeyClient.Current.TrackEvent(smartHiveEvent.GetType().Name, propsDictionary);
         return;
     }
     catch (Exception ex)
     {
         this.TrackAppException(ex);
     }
     HockeyClient.Current.TrackEvent(smartHiveEvent.GetType().Name);
 }
        protected async Task <bool> Release(IEventBase @event)
        {
            var limiter = RedisManager.GetRateLimiterClient($"{@event.GetType().FullName}_{@event.EventId}",
                                                            TimeSpan.FromMinutes(2));

            return(await limiter.ReleaseAsync());
        }
Beispiel #4
0
 protected override void OnRaiseSuccess(IEventBase <K> @event, byte[] bytes)
 {
     using (var dms = new MemoryStream(bytes))
     {
         Apply(BackupState, (IEventBase <K>)Serializer.Deserialize(@event.GetType(), dms));
     }
     BackupState.FullUpdateVersion(@event, GrainType);//更新处理完成的Version
 }
        protected async Task <bool> IsHandleNeeded(IEventBase @event)
        {
            var limiter = RedisManager.GetRateLimiterClient($"{@event.GetType().FullName}_{@event.EventId}",
                                                            TimeSpan.FromMinutes(2));

            if (await limiter.AcquireAsync())
            {
                return(true);
            }
            return(false);
        }
Beispiel #6
0
 protected override void OnRaiseSuccess(IEventBase <K> @event, byte[] bytes)
 {
     if (MessageTypeMapper.TryGetValue(@event.GetType().FullName, out var type))
     {
         using (var dms = new MemoryStream(bytes))
         {
             Apply(BackupState, (IEventBase <K>)Serializer.Deserialize(type, dms));
         }
         BackupState.FullUpdateVersion(@event);//更新处理完成的Version
     }
 }
Beispiel #7
0
    public static void Post(IEventBase e)
    {
        Type eventType = e.GetType();
        // Find all living subscribers of matching event type
        IEnumerable <EventSubscriber> candidates = subscribers.Where(s => s.eventType == eventType && s.instance);

        // Invoke all subscribers
        foreach (EventSubscriber sub in candidates)
        {
            try
            {
                sub.function.Invoke(sub.instance, new object[] { e });
            }
            catch (TargetInvocationException ex) // Occurs when an error is thrown from within the executed function
            {
                // Coalesce expression val1 ?? val2 uses first value, unless it is null, where it uses the second value
                Debug.LogError(ex.InnerException ?? ex);
            }
            catch (Exception ex)
            {
                Debug.LogError("Cannot invoke event handler because: \n" + ex);
            }
        }
    }
Beispiel #8
0
        protected virtual async Task <bool> RaiseEvent(IEventBase <K> @event, string uniqueId = null, string hashKey = null)
        {
            try
            {
                State.IncrementDoingVersion();//标记将要处理的Version
                @event.StateId   = GrainId;
                @event.Version   = State.Version + 1;
                @event.Timestamp = DateTime.UtcNow;
                using (var ms = new PooledMemoryStream())
                {
                    Serializer.Serialize(ms, @event);
                    var bytes = ms.ToArray();
                    var getEventStorageTask = GetEventStorage();
                    if (!getEventStorageTask.IsCompleted)
                    {
                        await getEventStorageTask;
                    }
                    if (await getEventStorageTask.Result.SaveAsync(@event, bytes, uniqueId))
                    {
                        if (SupportAsync)
                        {
                            var data = new W
                            {
                                TypeCode    = @event.GetType().FullName,
                                BinaryBytes = bytes
                            };
                            ms.Position = 0;
                            ms.SetLength(0);
                            Serializer.Serialize(ms, data);
                            //消息写入消息队列,以提供异步服务
                            Apply(State, @event);
                            var mqServiceTask = GetMQService();
                            if (!mqServiceTask.IsCompleted)
                            {
                                await mqServiceTask;
                            }
                            var publishTask = mqServiceTask.Result.Publish(ms.ToArray(), string.IsNullOrEmpty(hashKey) ? GrainId.ToString() : hashKey);
                            if (!publishTask.IsCompleted)
                            {
                                await publishTask;
                            }
                        }
                        else
                        {
                            Apply(State, @event);
                        }
                        State.UpdateVersion(@event);//更新处理完成的Version
                        var saveSnapshotTask = SaveSnapshotAsync();
                        if (!saveSnapshotTask.IsCompleted)
                        {
                            await saveSnapshotTask;
                        }
                        OnRaiseSuccess(@event, bytes);
                        return(true);
                    }
                    else
                    {
                        State.DecrementDoingVersion();//还原doing Version
                    }
                }
            }
            catch (Exception ex)
            {
                await RecoveryState();//还原状态

                ExceptionDispatchInfo.Capture(ex).Throw();
            }
            return(false);
        }
 /// <summary>
 ///  Enqueues an event onto the event queue
 /// </summary>
 public void ThrowEvent(object sender, IEventBase thrownEvent)
 {
     _log.DebugFormat(Resources.MSG_DEBUG_THROW, sender.GetType(), thrownEvent.GetType());
     _eventHandler.ThrowEvent(sender, thrownEvent);
 }