Esempio n. 1
0
 public static Task ProcessPaymentsAsync(
     [QueueTrigger(Constants.RequestProcessPaymentsQueueName)] ProcessPaymentsMessage message,
     TextWriter webJobsLogger,
     int dequeueCount,
     CancellationToken cancellationToken)
 {
     return(PaymentProcessor.ProcessPaymentsAsync(
                message,
                CreateLogger(webJobsLogger),
                cancellationToken));
 }
        public async Task ProcessPaymentsAsync(
            ProcessPaymentsMessage message,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            ExceptionDispatchInfo exceptionDispatchInfo = null;
            var lease = this.blobLeaseFactory.Create(Fifthweek.Payments.Shared.Constants.ProcessPaymentsLeaseObjectName, cancellationToken);

            try
            {
                if (await lease.TryAcquireLeaseAsync())
                {
                    var timeSinceLastLease = await lease.GetTimeSinceLastLeaseAsync();

                    if (timeSinceLastLease > MinimumTimeBetweenPaymentProcessing)
                    {
                        var errors = new List <PaymentProcessingException>();

                        await this.processAllPayments.ExecuteAsync(lease, errors, cancellationToken);

                        if (errors.Count > 0)
                        {
                            foreach (var error in errors)
                            {
                                logger.Error(error);
                            }
                        }

                        await this.requestProcessPayments.ExecuteAsync();
                    }
                    else
                    {
                        await lease.ReleaseLeaseAsync();
                    }
                }
                else
                {
                    await this.requestProcessPayments.ExecuteRetryAsync();
                }
            }
            catch (Exception t)
            {
                exceptionDispatchInfo = ExceptionDispatchInfo.Capture(t);
                logger.Error(t);
            }

            if (lease.GetIsAcquired())
            {
                if (exceptionDispatchInfo == null)
                {
                    await lease.UpdateTimestampsAsync();
                }

                await lease.ReleaseLeaseAsync();
            }

            if (exceptionDispatchInfo != null)
            {
                exceptionDispatchInfo.Throw();
            }
        }