// https://codehollow.com/2017/02/azure-functions-time-trigger-cron-cheat-sheet/
        public static async Task RunAsync([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log, ExecutionContext context)
        {
            config = Configuration.InitializeConfiguration(config, context);
            KeyVaultExtensions.InitializeConfiguration(config);
            keyVaultClient = KeyVaultExtensions.GetInstance();

            var tableStorageAccountConnectionString = await KeyVaultExtensions.GetSecretAsync(Constants.KeyVaultConstants.AzureWebjobsStorageKey);

            cloudTableClient = CloudTableClientExtensions.InitializeCloudTableClientAsync(cloudTableClient, tableStorageAccountConnectionString);

            var queueConnectionString = await KeyVaultExtensions.GetSecretAsync(Constants.KeyVaultConstants.ServiceBusConnectionString);

            queueClient = QueueClientExtensions.InitializeQueueClient(queueClient, queueConnectionString, "participants");

            uint.TryParse(config["CascadiaGamersGroupId"], out var extraLifeGroupId);

            log.LogInformation($"Getting gamers: {DateTime.Now}");
            var donorDriveClient = new DonorDriveClient();
            var gamers           = new List <ParticipantDto>(); //AutoMapper.Mapper.Map<List<ParticipantDto>>(await donorDriveClient.GetTeamParticipantsAsync(extraLifeGroupId));

            log.LogInformation($"Retrieved: {gamers.Count()} gamers.");

            var participants = AutoMapper.Mapper.Map <List <ParticipantTableEntity> >(gamers);

            await TableStorageExtensions <ParticipantTableEntity> .WriteToTableStorageAsync(cloudTableClient, participants,
                                                                                            "CascadiaGamersParticipants");

            log.LogInformation($"Wrote to Table Storage values for: {gamers.Count()} participants.");

            await QueueExtensions <ParticipantTableEntity> .WriteToQueueAsync(queueClient, participants,
                                                                              "parcicipants");

            log.LogInformation($"Wrote {gamers.Count()} participants to Service Bus to check for new donations.");

            log.LogInformation($"Writing to Sql Server values for: {gamers.Count()} participants.");

            var sqlConnectionString = await KeyVaultExtensions.GetSecretAsync(Constants.KeyVaultConstants.SqlConnectionString);

            await SqlExtensions <ParticipantTableEntity> .WriteParticipantsToSqlAsync(participants, sqlConnectionString);

            log.LogInformation($"Wrote to Sql Server values for: {gamers.Count()} participants.");
        }
Пример #2
0
        public static async Task RunAsync(
            [ServiceBusTrigger("participants", Connection = "AzureWebJobsServiceBus")]
            ParticipantTableEntity myQueueItem,
            ILogger log,
            ExecutionContext context)
        {
            config = Configuration.InitializeConfiguration(config, context);
            KeyVaultExtensions.InitializeConfiguration(config);
            keyVaultClient = KeyVaultExtensions.GetInstance();
            logging        = log;

            var tableStorageAccountConnectionString = await KeyVaultExtensions.GetSecretAsync(Constants.KeyVaultConstants.AzureWebjobsStorageKey);

            cloudTableClient = CloudTableClientExtensions.InitializeCloudTableClientAsync(cloudTableClient, tableStorageAccountConnectionString);

            logging.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem.DisplayName}");

            var table = cloudTableClient.GetTableReference("CascadiaGamersParticipants");
            var query = new TableQuery <ParticipantTableEntity>().Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, myQueueItem.RowKey));;

            Guid.TryParse(myQueueItem.RowKey, out var participantId);
            if (!participants.ContainsKey(participantId))
            {
                await AddParticipantToCacheAsync(table, query);
            }

            table = cloudTableClient.GetTableReference("CascadiaGamersDonations");
            var donationQuery = new TableQuery <DonationTableEntity>()
                                .Where(TableQuery.GenerateFilterCondition("ParticipantId", QueryComparisons.Equal, myQueueItem.ParticipantId.ToString()));

            await AddDonationToCacheAsync(table, donationQuery, participants[participantId]);

            logging.LogInformation($"Getting donations for {myQueueItem.DisplayName} at: {DateTime.Now}");
            var donorDriveClient = new DonorDriveClient();
            var donations        = await donorDriveClient.GetDonorParticipantsAsync(uint.Parse(myQueueItem.ParticipantId));

            logging.LogInformation($"Retrieved {donations.Count()} donations for {myQueueItem.DisplayName} at: {DateTime.Now}");

            var participantDto = participants[participantId];
            var newDonations   = new List <Donor>();

            foreach (var donation in donations)
            {
                var donationid = participantDto.CreateGuidFromSHA256Hash(donation);
                if (!participantDto.Donations.ContainsKey(donationid))
                {
                    participantDto.Donations.Add(donationid, donation);
                    newDonations.Add(donation);
                }
            }

            var donationsForTableStorage = AutoMapper.Mapper.Map <List <DonationTableEntity> >(donations);

            donationsForTableStorage.Select(
                d =>
                { d.ParticipantUniqueIdentifier = participantDto.CreateGuidFromSHA256Hash().ToString(); return(d); }
                ).ToList();

            logging.LogInformation($"Updating or Inserting {donationsForTableStorage.Count} donations for {myQueueItem.DisplayName} to Table Storage at: {DateTime.Now}");

            await TableStorageExtensions <DonationTableEntity> .WriteToTableStorageAsync(cloudTableClient, donationsForTableStorage,
                                                                                         "CascadiaGamersDonations");

            logging.LogInformation($"Updating or Inserting {donationsForTableStorage.Count} donations for {myQueueItem.DisplayName} to Sql Server at: {DateTime.Now}");
            var sqlConnectionString = await KeyVaultExtensions.GetSecretAsync(Constants.KeyVaultConstants.SqlConnectionString);

            await SqlExtensions <DonationTableEntity> .WriteDonationsToSqlAsync(donationsForTableStorage, sqlConnectionString);

            logging.LogInformation($"Completed upserting {donationsForTableStorage.Count} donations for {myQueueItem.DisplayName} to Sql Server at: {DateTime.Now}");

            if (newDonations.Count() > 0)
            {
                var donationsForNewDonationsQueue = AutoMapper.Mapper.Map <List <DonationTableEntity> >(newDonations);

                var queueConnectionString = await KeyVaultExtensions.GetSecretAsync(Constants.KeyVaultConstants.ServiceBusConnectionString);

                var queueName = "newdonations";
                queueClient = QueueClientExtensions.InitializeQueueClient(queueClient, queueConnectionString, queueName);
                logging.LogInformation($"Adding {donationsForNewDonationsQueue.Count} new donations for {myQueueItem.DisplayName} to Service Bus at: {DateTime.Now}");
                await QueueExtensions <DonationTableEntity> .WriteToQueueAsync(queueClient, donationsForNewDonationsQueue, queueName);
            }
        }