public async Task Run([ServiceBusTrigger("%membershipQueueName%", Connection = "differenceQueueConnection", IsSessionsEnabled = true)] Message message, IMessageSession messageSession)
        {
            var body = new GroupMembershipMessage {
                Body      = JsonConvert.DeserializeObject <GroupMembership>(Encoding.UTF8.GetString(message.Body)),
                LockToken = message.SystemProperties.LockToken
            };

            await _messageCollector.HandleNewMessage(body, messageSession);
        }
Пример #2
0
        public async Task <GroupMembershipMessageResponse> CollectMessagesAsync([ActivityTrigger] GraphUpdaterFunctionRequest request)
        {
            await _loggingRepository.LogMessageAsync(new LogMessage { Message = nameof(MessageCollectorFunction) + " function started", RunId = request.RunId });

            var body = new GroupMembershipMessage
            {
                Body                 = JsonConvert.DeserializeObject <GroupMembership>(request.Message),
                LockToken            = request.MessageLockToken,
                IsCancelationMessage = request.IsCancelationRequest
            };

            var handleNewMessageResult = await _messageCollector.HandleNewMessageAsync(body, request.MessageSessionId);

            await _loggingRepository.LogMessageAsync(new LogMessage { Message = nameof(MessageCollectorFunction) + " function completed", RunId = request.RunId });

            return(handleNewMessageResult);
        }
        public async Task HandleNewMessage(GroupMembershipMessage body, IMessageSession messageSession)
        {
            var sessionId = messageSession.SessionId;

            _receivedMessages.AddOrUpdate(sessionId, new List <GroupMembershipMessage> {
                body
            }, (key, messages) => {
                messages.Add(body); return(messages);
            });

            //Console.WriteLine($"Session {sessionId} got {body.Body.SourceMembers.Count} users.");
            if (body.Body.IsLastMessage)
            {
                if (!_receivedMessages.TryRemove(sessionId, out var allReceivedMessages))
                {
                    // someone else got to it first. shouldn't happen, but it's good to be prepared.
                    return;
                }

                // remove this message-renewing logic once we can verify that the new, longer message leases work.
                var source            = new CancellationTokenSource();
                var cancellationToken = source.Token;
                var renew             = RenewMessages(messageSession, allReceivedMessages, cancellationToken);

                var received = GroupMembership.Merge(allReceivedMessages.Select(x => x.Body));

                try
                {
                    await _graphUpdater.CalculateDifference(received);

                    // If it succeeded, complete all the messages and close the session
                    await messageSession.CompleteAsync(allReceivedMessages.Select(x => x.LockToken));

                    await messageSession.CloseAsync();
                }
                finally
                {
                    source.Cancel();
                }
            }
        }
Пример #4
0
        public async Task <GroupMembershipMessageResponse> HandleNewMessageAsync(GroupMembershipMessage body, string messageSessionId)
        {
            if (body.IsCancelationMessage)
            {
                _receivedMessages.TryRemove(messageSessionId, out _);
                return(new GroupMembershipMessageResponse());
            }

            _loggingRepository.SyncJobProperties = new Dictionary <string, string>()
            {
                { "LockToken", body.LockToken }, { "RowKey", body.Body.SyncJobRowKey }, { "PartitionKey", body.Body.SyncJobPartitionKey }, { "TargetOfficeGroupId", body.Body.Destination.ObjectId.ToString() }
            };

            var receivedSoFar = _receivedMessages.AddOrUpdate(messageSessionId, new List <GroupMembershipMessage> {
                body
            }, (key, messages) =>
            {
                messages.Add(body); return(messages);
            });

            var handleNewMessageResponse = new GroupMembershipMessageResponse()
            {
                ShouldCompleteMessage = false
            };

            await _loggingRepository.LogMessageAsync(new LogMessage
            {
                RunId   = body.Body.RunId,
                Message = $"Got a message in {nameof(MessageCollector)}. " +
                          $"The message we just received has {body.Body.SourceMembers.Count} users and is {(body.Body.IsLastMessage ? "" : "not ")}the last message in its session. " +
                          $"There are currently {_receivedMessages.Count} sessions in flight. " +
                          $"The current session, the one with ID {messageSessionId}, has {receivedSoFar.Count} messages with {receivedSoFar.Sum(x => x.Body.SourceMembers.Count)} users in total."
            });

            if (body.Body.IsLastMessage)
            {
                if (!_receivedMessages.TryRemove(messageSessionId, out var allReceivedMessages))
                {
                    // someone else got to it first. shouldn't happen, but it's good to be prepared.
                    return(handleNewMessageResponse);
                }

                var received = GroupMembership.Merge(allReceivedMessages.Select(x => x.Body));

                await _loggingRepository.LogMessageAsync(new LogMessage
                {
                    RunId   = body.Body.RunId,
                    Message = $"This message completed the session, so I'm going to sync {received.SourceMembers.Count} users."
                });

                handleNewMessageResponse.CompletedGroupMembershipMessages = allReceivedMessages;
                handleNewMessageResponse.ShouldCompleteMessage            = true;

                return(handleNewMessageResponse);
            }
            else
            {
                await _loggingRepository.LogMessageAsync(new LogMessage
                {
                    RunId   = body.Body.RunId,
                    Message = "This is not the last message, so not doing anything right now."
                });
            }

            _loggingRepository.SyncJobProperties = null;
            return(handleNewMessageResponse);
        }