Example #1
0
        public async Task ExecuteAsync(ILogger logger, IKeepAliveHandler keepAliveHandler, CancellationToken cancellationToken)
        {
            logger.AssertNotNull("logger");
            keepAliveHandler.AssertNotNull("keepAliveHandler");

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var endTimeExclusive = this.timestampCreator.Now().Subtract(Shared.Constants.GarbageCollectionMinimumAge);

            logger.Info("Deleting test user accounts");
            await this.deleteTestUserAccounts.ExecuteAsync(endTimeExclusive);

            logger.Info("Deleting orphaned files");
            var files = await this.getFilesEligibleForGarbageCollection.ExecuteAsync(endTimeExclusive);

            foreach (var file in files)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                Console.Write(".");
                try
                {
                    await keepAliveHandler.KeepAliveAsync();

                    await this.deleteBlobsForFile.ExecuteAsync(file);

                    await this.deleteFileDbStatement.ExecuteAsync(file.FileId);
                }
                catch (Exception t)
                {
                    logger.Warn("Failed to delete orphaned file {0}.", file.FileId);
                    logger.Error(t);
                }
            }

            if (!cancellationToken.IsCancellationRequested)
            {
                logger.Info("Deleting orphaned blob containers");
                await this.deleteOrphanedBlobContainers.ExecuteAsync(logger, keepAliveHandler, endTimeExclusive, cancellationToken);
            }

            stopwatch.Stop();
            logger.Info("Finished garbage collection in {0}s", stopwatch.Elapsed.TotalSeconds);
        }
        public async Task ExecuteAsync(UserId subscriberId, DateTime endTimeExclusive, IKeepAliveHandler keepAliveHandler, List <PaymentProcessingException> errors)
        {
            keepAliveHandler.AssertNotNull("keepAliveHandler");
            subscriberId.AssertNotNull("subscriberId");
            errors.AssertNotNull("errors");

            var creators = await this.getCreatorsAndFirstSubscribedDates.ExecuteAsync(subscriberId);

            var committedAccountBalanceValue = await this.getCommittedAccountBalanceDbStatement.ExecuteAsync(subscriberId);

            if (committedAccountBalanceValue < 0)
            {
                errors.Add(new PaymentProcessingException(string.Format("Committed account balance was {0} for user {1}.", committedAccountBalanceValue, subscriberId), subscriberId, null));
                committedAccountBalanceValue = 0m;
            }

            var committedAccountBalance = new CommittedAccountBalance(committedAccountBalanceValue);

            foreach (var creator in creators)
            {
                try
                {
                    await keepAliveHandler.KeepAliveAsync();

                    var latestCommittedLedgerDate = await this.getLatestCommittedLedgerDate.ExecuteAsync(subscriberId, creator.CreatorId);

                    var startTimeInclusive = latestCommittedLedgerDate ?? PaymentProcessingUtilities.GetPaymentProcessingStartDate(creator.FirstSubscribedDate);

                    if ((endTimeExclusive - startTimeInclusive) <= MinimumProcessingPeriod)
                    {
                        continue;
                    }

                    committedAccountBalance = await this.processPaymentsBetweenSubscriberAndCreator.ExecuteAsync(
                        subscriberId,
                        creator.CreatorId,
                        startTimeInclusive,
                        endTimeExclusive,
                        committedAccountBalance);
                }
                catch (Exception t)
                {
                    errors.Add(new PaymentProcessingException(t, subscriberId, creator.CreatorId));
                }
            }
        }
        public async Task ExecuteAsync(ILogger logger, IKeepAliveHandler keepAliveHandler, DateTime endTimeExclusive, CancellationToken cancellationToken)
        {
            logger.AssertNotNull("logger");
            keepAliveHandler.AssertNotNull("keepAliveHandler");

            var channelIds = await this.getAllChannelIds.ExecuteAsync();

            var channelIdsHashSet = new HashSet <Guid>(channelIds.Select(v => v.Value));

            // Delete blob containers that parse as Guids but don't correspond to any channel id.
            var blobClient = this.cloudStorageAccount.CreateCloudBlobClient();

            BlobContinuationToken token = null;

            do
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                var segment = await blobClient.ListContainersSegmentedAsync(token);

                foreach (var container in segment.Results)
                {
                    Console.Write(".");

                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    await keepAliveHandler.KeepAliveAsync();

                    Guid channelIdGuid;
                    if (!Guid.TryParse(container.Name, out channelIdGuid))
                    {
                        continue;
                    }

                    if (channelIdsHashSet.Contains(channelIdGuid))
                    {
                        continue;
                    }

                    if (container.Properties.LastModified == null)
                    {
                        logger.Warn("Skipping container {0} because last modified date was unavailable", container.Name);
                        continue;
                    }

                    var lastModified = container.Properties.LastModified.Value.UtcDateTime;
                    if (lastModified >= endTimeExclusive)
                    {
                        continue;
                    }

                    await container.DeleteAsync();
                }

                token = segment.ContinuationToken;
            }while (token != null);
        }