예제 #1
0
        private async Task InternalHandle(object _)
        {
            var handle = string.Empty;

            try
            {
                UnregisterTimer(_handleTimer);

                var handleList = (await StateManager.GetStateNamesAsync()).ToList();

                if (!handleList.Any())
                {
                    return;
                }

                var messageDataConditional = await StateManager.TryGetStateAsync <MessageData>(handleList.First());

                if (!messageDataConditional.HasValue)
                {
                    _bigBrother.Publish(new WebhookEvent("message was empty"));
                    return;
                }

                var messageData = messageDataConditional.Value;
                handle = messageData.HandleAsString;

                var handler = _eventHandlerFactory.CreateEventHandler(messageData.Type);

                await handler.Call(messageData);

                await StateManager.RemoveStateAsync(messageData.HandleAsString);

                await ActorProxy.Create <IPoolManagerActor>(new ActorId(0)).CompleteWork(messageData.Handle);
            }
            catch (Exception e)
            {
                //don't want msg state managed by fabric just yet, let failures be backed by the service bus subscriptions
                if (handle != string.Empty)
                {
                    await StateManager.RemoveStateAsync(handle);
                }

                BigBrother.Write(e.ToExceptionEvent());
            }
            finally
            {
                //restarts the timer in case there are more than one msg in the state, if not then let it be restarted in the standard msg population flow.
                if ((await StateManager.GetStateNamesAsync()).Any())
                {
                    _handleTimer = RegisterTimer(
                        InternalHandle,
                        null,
                        TimeSpan.FromMilliseconds(100),
                        TimeSpan.MaxValue);
                }
            }
        }
예제 #2
0
        private async Task InternalHandle(object state)
        {
            var         messageDelivered = true;
            MessageData messageData      = null;

            try
            {
                UnregisterTimer(_handleTimer);

                messageData = state as MessageData;
                if (messageData == null)
                {
                    _bigBrother.Publish(new ActorError($" actor timer state could not be parsed to a guid so removing it.", this));
                    return;
                }

                if (string.IsNullOrWhiteSpace(messageData.Type))
                {
                    _bigBrother.Publish(new ActorError($"message is missing type - it cannot be processed", this));
                    return;
                }

                var handler = _eventHandlerFactory.CreateEventHandler(messageData.Type, messageData.SubscriberName);
                await handler.CallAsync(messageData, new Dictionary <string, object>(), CancellationToken.None);
            }
            catch (Exception e)
            {
                BigBrother.Write(e.ToExceptionEvent());
                messageDelivered = false;
            }
            finally
            {
                if (messageData != null)
                {
                    try
                    {
                        await StateManager.RemoveStateAsync(messageData.HandlerId.ToString());

                        await ServiceProxy.Create <IEventReaderService>(new Uri(messageData.ReplyTo)).CompleteMessageAsync(messageData, messageDelivered);
                    }
                    catch (Exception e)
                    {
                        BigBrother.Write(e.ToExceptionEvent());
                    }
                }
            }
        }