Beispiel #1
0
        public static async Task RunOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
        {
            try
            {
                var searchAndNotifications = await context.CallActivityAsync <List <SearchAndNotification> >(TableStorageDataManager.GetSearchPhrasesFunctionName, null);

                var searchPhrases        = searchAndNotifications.Where(sn => sn.SearchOrNotification == Constants.SearchType);
                var notificationTriggers = searchAndNotifications.Where(sn => sn.SearchOrNotification == Constants.NotificationType);

                var searchTasks = searchPhrases.Select(searchPhrase =>
                {
                    return(new TableAndSearchData
                    {
                        SearchPhrase = searchPhrase,
                        TableData = context.CallActivityAsync <List <SearchResultEntity> >(TableStorageDataManager.GetByPartitionKeyFunctionName, (searchPhrase.Model, TableName.Cars)),
                        CurrentData = context.CallActivityAsync <List <SearchResult> >(BilBasenSearcher.SearchFunctionName, searchPhrase)
                    });
                });

                var triggeredNotifications = new List <TriggeredNotification>();

                foreach (var task in searchTasks)
                {
                    var tableData        = await task.TableData;
                    var currentData      = await task.CurrentData;
                    var triggersForModel = notificationTriggers
                                           .Where(nt => nt.Model.Equals(task.SearchPhrase.Model, StringComparison.InvariantCultureIgnoreCase))
                                           .ToList();

                    var(entitiesToUpsert, newResults) = GetEntitiesToUpdate(tableData, currentData);

                    triggeredNotifications.AddRange(newResults.SelectMany(nr => GetTriggeredNotifications(nr, triggersForModel)));

                    await context.CallActivityAsync("TableStorage_BatchInsert", entitiesToUpsert);
                }

                if (triggeredNotifications.Any())
                {
                    var emails = EmailNotifier.BuildEmails(triggeredNotifications);

                    await Task.WhenAll(emails.Select(e => context.CallActivityAsync(EmailNotifier.NotifyFunctionName, e)));
                }
            }