async Task ProcessMessagesAsync(IMessageSession messageSession, Message message, CancellationToken token)
        {
            try
            {
                var student = JsonConvert.DeserializeObject <Student>(Encoding.UTF8.GetString(message.Body));

                lock (thisLock)
                {
                    //revalidation if course is available.
                    if (_signupRepo.CourseIsAvailable(student.CourseId))
                    {
                        _signupRepo.SigningupCourse(student);

                        //TODO: Send Email
                    }
                    else
                    {
                        //TODO: SEND EMAIL THAT COURSE IS NOT AVAILABLE.
                    }
                }


                await messageSession.CompleteAsync(message.SystemProperties.LockToken);
            }
            catch (SessionLockLostException)
            {
                await messageSession.RenewSessionLockAsync();

                await messageSession.CompleteAsync(message.SystemProperties.LockToken);
            }
        }
        private async Task RenewMessages(IMessageSession messageSession, List <GroupMembershipMessage> messages, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                await messageSession.RenewSessionLockAsync();

                foreach (var message in messages)
                {
                    await messageSession.RenewLockAsync(message.LockToken);
                }
                await Task.Delay(_waitBetweenRenew);
            }
        }
Пример #3
0
        async Task RenewSessionLockTaskAsync(IMessageSession session, CancellationToken renewLockCancellationToken)
        {
            while (!this.pumpCancellationToken.IsCancellationRequested &&
                   !renewLockCancellationToken.IsCancellationRequested)
            {
                try
                {
                    var amount = MessagingUtilities.CalculateRenewAfterDuration(session.LockedUntilUtc);

                    MessagingEventSource.Log.SessionReceivePumpSessionRenewLockStart(this.clientId, session.SessionId, amount);
                    await Task.Delay(amount, renewLockCancellationToken).ConfigureAwait(false);

                    if (!this.pumpCancellationToken.IsCancellationRequested &&
                        !renewLockCancellationToken.IsCancellationRequested)
                    {
                        await session.RenewSessionLockAsync().ConfigureAwait(false);

                        MessagingEventSource.Log.SessionReceivePumpSessionRenewLockStop(this.clientId, session.SessionId);
                    }
                    else
                    {
                        break;
                    }
                }
                catch (Exception exception)
                {
                    MessagingEventSource.Log.SessionReceivePumpSessionRenewLockException(this.clientId, session.SessionId, exception);

                    // TaskCanceled is expected here as renewTasks will be cancelled after the Complete call is made.
                    // ObjectDisposedException should only happen here because the CancellationToken was disposed at which point
                    // this renew exception is not relevant anymore. Lets not bother user with this exception.
                    if (!(exception is TaskCanceledException) && !(exception is ObjectDisposedException))
                    {
                        await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.RenewLock).ConfigureAwait(false);
                    }
                    if (!MessagingUtilities.ShouldRetry(exception))
                    {
                        break;
                    }
                }
            }
        }
        private async Task RenewMessages(
            IDurableOrchestrationClient starter,
            IMessageSession messageSession,
            CancellationTokenSource cancellationToken,
            string messageId)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                var action = await TrackLastMessageTimeout(starter, messageSession, messageId);

                if (action == SessionTrackerAction.Stop)
                {
                    break;
                }

                await messageSession.RenewSessionLockAsync();

                await Task.Delay(_waitBetweenRenew);
            }
        }
        async Task RenewSessionLockTaskAsync(IMessageSession session, CancellationToken renewLockCancellationToken)
        {
            while (!this.pumpCancellationToken.IsCancellationRequested &&
                   !renewLockCancellationToken.IsCancellationRequested)
            {
                try
                {
                    TimeSpan amount = MessagingUtilities.CalculateRenewAfterDuration(session.LockedUntilUtc);

                    MessagingEventSource.Log.SessionReceivePumpSessionRenewLockStart(this.clientId, session.SessionId, amount);
                    await Task.Delay(amount, renewLockCancellationToken).ConfigureAwait(false);

                    if (!this.pumpCancellationToken.IsCancellationRequested &&
                        !renewLockCancellationToken.IsCancellationRequested)
                    {
                        await session.RenewSessionLockAsync().ConfigureAwait(false);

                        MessagingEventSource.Log.SessionReceivePumpSessionRenewLockStop(this.clientId, session.SessionId);
                    }
                    else
                    {
                        break;
                    }
                }
                catch (Exception exception)
                {
                    MessagingEventSource.Log.SessionReceivePumpSessionRenewLockExeption(this.clientId, session.SessionId, exception);

                    // TaskCancelled is expected here as renewTasks will be cancelled after the Complete call is made.
                    // Lets not bother user with this exception.
                    if (!(exception is TaskCanceledException))
                    {
                        this.sessionHandlerOptions.RaiseExceptionReceived(new ExceptionReceivedEventArgs(exception, "RenewLock"));
                    }
                    if (!MessagingUtilities.ShouldRetry(exception))
                    {
                        break;
                    }
                }
            }
        }
        private async Task ReaquireClientSessionLock()
        {
            var waitTime = TimeSpan.FromSeconds(0.5);

            while (true)
            {
                try
                {
                    await _activeClientSession.RenewSessionLockAsync();

                    Logger.Info("Successfully aquired lock");
                    break;
                }
                catch (Exception renewException)
                {
                    // Sometimes necessary when there are connection issues
                    Logger.Warn($"Encountered error while re-aquiring session lock ({renewException.Message}), will create new session lock");

                    try
                    {
                        _activeClientSession = await ClientSessionListener.Value.AcceptMessageSessionAsync(State[ApolloConstants.RegisteredAsKey].ToString(), TimeSpan.FromMinutes(30));

                        Logger.Info("Successfully created a new session lock");

                        break;
                    }
                    catch (ServiceBusCommunicationException ex)
                    {
                        Logger.Debug(ex.Message);
                        Logger.Info("Connection to service bus lost, waiting for it to re-establish");
                        while (true)
                        {
                            try
                            {
                                var entry = await Dns.GetHostEntryAsync(Configuration.ConnectionStringBuilder.Endpoint.Replace("sb://", string.Empty));

                                if (entry != null)
                                {
                                    Logger.Info($"Managed to resolve service bus endpoint to {entry.HostName}, will now retry");
                                    break;
                                }
                            }
                            catch
                            {
                                // Suppress
                            }
                            await Task.Delay(300, _clientSessionListenCancellationToken.Token);
                        }
                        Configuration.Reconnect();
                        await Impl.Recreate();

                        continue;
                    }
                    catch (Exception recreateException)
                    {
                        Logger.Warn($"Encountered error while creating new session lock, will wait and try again in {waitTime.TotalSeconds} seconds");
                        Logger.Debug(recreateException);
                    }

                    await Task.Delay(waitTime, _clientSessionListenCancellationToken.Token);

                    if (_clientSessionListenCancellationToken.Token.IsCancellationRequested)
                    {
                        break;
                    }
                    waitTime = TimeSpan.FromMilliseconds(Math.Min(waitTime.TotalMilliseconds * 1.5, TimeSpan.FromMinutes(1).TotalMilliseconds));
                }
            }
        }