Exemple #1
0
        private async Task RunDispatcherForSession(IMessageSession session, CancellationToken cancelToken)
        {
            while (cancelToken.IsCancellationRequested == false)
            {
                var msg = await session.ReceiveAsync(TimeSpan.FromSeconds(1));

                if (msg != null)
                {
                    bool isPersistedAfterCalculus = false;
                    try
                    {
                        ActorBase actor = null;

                        Type tp = Type.GetType((string)msg.UserProperties[ActorReference.cActorType]);
                        if (tp == null)
                        {
                            throw new ArgumentException($"Cannot find type '{session.SessionId}'");
                        }

                        var id = new ActorId((string)msg.UserProperties[ActorReference.cActorId]);
                        if (!actorMap.ContainsKey(session.SessionId))
                        {
                            if (this.persistenceProvider != null)
                            {
                                actor = await this.persistenceProvider.LoadActor(id);

                                if (actor != null)
                                {
                                    logger?.LogInformation($"{this.Name} - Loaded from pesisted store: {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");
                                }
                            }

                            if (actor == null)
                            {
                                actor = Activator.CreateInstance(tp, id) as ActorBase;
                                logger?.LogInformation($"{this.Name} - New instance created: {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");
                            }

                            actor.PersistenceProvider = this.PersistenceProvider;

                            actor.Logger = logger;

                            actorMap[session.SessionId] = actor;

                            actor.Activated();
                        }

                        actor = actorMap[session.SessionId];

                        logger?.LogInformation($"{this.Name} - Received message: {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");

                        var invokingMsg = ActorReference.DeserializeMsg <object>(msg.Body);

                        var replyMsg = await InvokeOperationOnActorAsync(actor, invokingMsg, (bool)msg.UserProperties[ActorReference.cExpectResponse],
                                                                         msg.MessageId, msg.ReplyTo);

                        logger?.LogInformation($"{this.Name} - Invoked : {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");

                        await persistAndCleanupIfRequired(session);

                        logger?.LogInformation($"{this.Name} - Persisted : {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");

                        isPersistedAfterCalculus = true;

                        // If actor operation was invoked with Ask<>(), then reply is expected.
                        if (replyMsg != null)
                        {
                            await this.sendReplyQueueClients[msg.ReplyTo].SendAsync(replyMsg);

                            logger?.LogTrace($"{this.Name} - Replied : {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");
                        }

                        await session.CompleteAsync(msg.SystemProperties.LockToken);

                        logger?.LogInformation($"{this.Name} - Completed : {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");
                    }
                    catch (Exception ex)
                    {
                        logger?.LogWarning(ex, "Messsage processing error");

                        if (isPersistedAfterCalculus == false)
                        {
                            await persistAndCleanupIfRequired(session);
                        }

                        if (!(ex is SessionLockLostException))
                        {
                            await session.CompleteAsync(msg.SystemProperties.LockToken);
                        }

                        await SendExceptionResponseIfRequired(msg, ex);
                    }
                }
                else
                {
                    logger?.LogTrace($"{this.Name} - No more messages received for sesson {session.SessionId}");
                    await persistAndCleanupIfRequired(session);

                    //return;
                }

                if (IsMemoryCritical())
                {
                    logger?.LogWarning($"Memory reached critical value: {this.CriticalMemInGb}, {Environment.WorkingSet / 1024 / 1024 / 1024}");
                }

                break;
            }
        }
 public Task Complete()
 {
     return(_session.CompleteAsync(_message.SystemProperties.LockToken));
 }
 private static async Task ProcessMessagesInSessionAsync(IMessageSession messageSession, Message message, CancellationToken token)
 {
     await messageSession.CompleteAsync(message.SystemProperties.LockToken);
 }
Exemple #4
0
 public Task Complete()
 {
     return(_deadLettered
         ? TaskUtil.Completed
         : _session.CompleteAsync(_message.SystemProperties.LockToken));
 }