public async Task <IEventHandler> LoadAsync(IEventSubscription subscription, IEventHandlerDto config) { switch (config.HandlerType) { case EventHandlerType.Rule: CheckDependency(EventRuleRepository); var ruleConfig = await EventRuleRepository.GetAsync(config.Id).ConfigureAwait(false); return(new EventRule(subscription, config.Id, ruleConfig)); case EventHandlerType.Aggregate: CheckDependency(EventAggregatorRepository); var aggregatorConfig = await EventAggregatorRepository.GetAsync(config.Id).ConfigureAwait(false); return(new EventAggregator(subscription, config.Id, aggregatorConfig)); case EventHandlerType.ContractQuery: CheckDependency(ContractQueryFactory); CheckDependency(EthApi); var queryConfig = await ContractQueryFactory.GetAsync(subscription.SubscriberId, config.Id).ConfigureAwait(false); return(new ContractQueryEventHandler(subscription, config.Id, EthApi, queryConfig)); case EventHandlerType.Queue: CheckDependency(SubscriberQueueFactory); CheckDependency(SubscriberQueueRepository); var queueConfig = await SubscriberQueueRepository.GetAsync(subscription.SubscriberId, config.SubscriberQueueId).ConfigureAwait(false); var queue = await SubscriberQueueFactory.GetSubscriberQueueAsync(queueConfig).ConfigureAwait(false); return(new QueueHandler(subscription, config.Id, queue)); case EventHandlerType.GetTransaction: CheckDependency(GetTransactionProxy); return(new GetTransactionEventHandler(subscription, config.Id, GetTransactionProxy)); case EventHandlerType.Index: CheckDependency(SubscriberSearchIndexFactory); CheckDependency(SubscriberSearchIndexRepository); var indexConfig = await SubscriberSearchIndexRepository.GetAsync(subscription.SubscriberId, config.SubscriberSearchIndexId).ConfigureAwait(false); var searchIndex = await SubscriberSearchIndexFactory.GetSubscriberSearchIndexAsync(indexConfig).ConfigureAwait(false); return(new SearchIndexHandler(subscription, config.Id, searchIndex)); case EventHandlerType.Store: CheckDependency(SubscriberRepositoryFactory); var storageConfig = await SubscriberStorageRepository.GetAsync(subscription.SubscriberId, config.SubscriberRepositoryId).ConfigureAwait(false); var logRepository = await SubscriberRepositoryFactory.GetLogRepositoryHandlerAsync(storageConfig).ConfigureAwait(false); return(new StorageHandler(subscription, config.Id, logRepository)); default: throw new ArgumentException("unsupported handler type"); } }
public async Task WebJobExample() { var config = TestConfiguration.LoadConfig(); string azureStorageConnectionString = config["AzureStorageConnectionString"]; string azureSearchKey = config["AzureSearchApiKey"]; var configurationContext = EventProcessingConfigMock.Create(PARTITION, out IdGenerator idGenerator); IEventProcessingConfigurationRepository configurationRepository = configurationContext.CreateMockRepository(idGenerator); var web3 = new Web3.Web3(TestConfiguration.BlockchainUrls.Infura.Rinkeby); // search components var searchService = new AzureSearchService(serviceName: AZURE_SEARCH_SERVICE_NAME, searchApiKey: azureSearchKey); var subscriberSearchIndexFactory = new SubscriberSearchIndexFactory(async indexName => { if (await searchService.IndexExistsAsync(indexName) == false) { //TODO: REPLACE THIS WITH Nethereum.BlockchainStore.Search.Azure.EventToGenericSearchDocMapper await searchService.CreateIndexAsync(EventToGenericSearchDocMapper.CreateAzureIndexDefinition(indexName)); } return(searchService.CreateIndexer <DecodedEvent, GenericSearchDocument>( indexName, decodedEvent => EventToGenericSearchDocMapper.Map(decodedEvent, decodedEvent.State))); }); // queue components //AzureStorageQueueFactory var azureQueueFactory = new AzureStorageQueueFactory(azureStorageConnectionString); var subscriberQueueFactory = new SubscriberQueueFactory( queueName => azureQueueFactory.GetOrCreateQueueAsync(queueName)); // subscriber repository var repositoryFactory = new AzureTablesSubscriberRepositoryFactory(azureStorageConnectionString); // load subscribers and event subscriptions var eventSubscriptionFactory = new EventSubscriptionFactory( web3, configurationRepository, subscriberQueueFactory, subscriberSearchIndexFactory, repositoryFactory); var eventSubscriptions = await eventSubscriptionFactory.LoadAsync(PARTITION); // progress repo (dictates which block ranges to process next) // maintain separate progress per partition via a prefix var storageCloudSetup = new AzureTablesRepositoryFactory(azureStorageConnectionString, prefix: $"Partition{PARTITION}"); var blockProgressRepo = storageCloudSetup.CreateBlockProgressRepository(); var logProcessor = web3.Processing.Logs.CreateProcessor( logProcessors: eventSubscriptions, blockProgressRepository: blockProgressRepo); // execute try { var ctx = new System.Threading.CancellationTokenSource(); await logProcessor.ExecuteAsync(BLOCK_TO, ctx.Token, BLOCK_FROM); } finally { await ClearDown(configurationContext, storageCloudSetup, searchService, azureQueueFactory, repositoryFactory); } // save event subscription state await configurationRepository.EventSubscriptionStates.UpsertAsync(eventSubscriptions.Select(s => s.State)); // assertions var subscriptionState1 = configurationContext.GetEventSubscriptionState(eventSubscriptionId: 1); // interested in transfers with contract queries and aggregations var subscriptionState2 = configurationContext.GetEventSubscriptionState(eventSubscriptionId: 2); // interested in transfers with simple aggregation var subscriptionState3 = configurationContext.GetEventSubscriptionState(eventSubscriptionId: 3); // interested in any event for a specific address Assert.Equal("4009000000002040652615", subscriptionState1.Values["RunningTotalForTransferValue"].ToString()); Assert.Equal((uint)19, subscriptionState2.Values["CurrentTransferCount"]); var txForSpecificAddress = (List <string>)subscriptionState3.Values["AllTransactionHashes"]; Assert.Equal("0x362bcbc78a5cc6156e8d24d95bee6b8f53d7821083940434d2191feba477ae0e", txForSpecificAddress[0]); Assert.Equal("0xe63e9422dedf84d0ce13f9f75ebfd86333ce917b2572925fbdd51b51caf89b77", txForSpecificAddress[1]); var blockNumbersForSpecificAddress = (List <HexBigInteger>)subscriptionState3.Values["AllBlockNumbers"]; Assert.Equal(4063362, blockNumbersForSpecificAddress[0].Value); Assert.Equal(4063362, blockNumbersForSpecificAddress[1].Value); }