示例#1
0
        public override async Task StartAsync(CancellationToken cancellationToken)
        {
            if (!await _elasticSearchService.CheckIndex(ElasticSearchIndexDocumentNames.EmployeeIndexName))
            {
                await _elasticSearchService.CretaeIndex <ProductViewModel>(ElasticSearchIndexDocumentNames.EmployeeIndexName,
                                                                           ElasticSearchIndexDocumentNames.EmployeeIndexAliasName);
            }
            var lastCheckpoint = await new CreateAndUpdatePointDocumentIndex(_elasticSearchService).addPointDocument(
                ElasticSearchIndexDocumentNames.EmployeeDocumentPositionIndexName,
                ElasticSearchIndexDocumentNames.EmployeeDocumentPositionAliasName,
                ElasticSearchIndexDocumentNames.EmployeeDocumentPositionName);

            var settings = new CatchUpSubscriptionSettings(
                maxLiveQueueSize: 10000,
                readBatchSize: 500,
                verboseLogging: false,
                resolveLinkTos: false,
                subscriptionName: "employee");

            subscription = _eventStore.SubscribeToAllFrom(
                lastCheckpoint: lastCheckpoint,
                settings: settings,
                eventAppeared: async(sub, @event) =>
            {
                if (@event.OriginalEvent.EventType.StartsWith("$"))
                {
                    return;
                }

                try
                {
                    var eventType = Type.GetType(Encoding.UTF8.GetString(@event.OriginalEvent.Metadata) + ",NorthwindApi.Domain");
                    var eventData = Newtonsoft.Json.JsonConvert.DeserializeObject(Encoding.UTF8.GetString(@event.OriginalEvent.Data), eventType);

                    if (eventType != typeof(EmployeeAddEvent) && eventType != typeof(EmployeeUpdateEvent) && eventType != typeof(EmployeeRemoveEvent))
                    {
                        return;
                    }

                    var eventSaveData = CreateViewModel(eventData);

                    if (eventType == typeof(EmployeeAddEvent) || eventType == typeof(EmployeeUpdateEvent))
                    {
                        await _elasticSearchService.AddOrUpdateIndex <EmployeeViewModel>(ElasticSearchIndexDocumentNames.EmployeeIndexName,
                                                                                         ElasticSearchIndexDocumentNames.EmployeeIndexAliasName, eventSaveData);
                    }
                    else
                    {
                        await _elasticSearchService.Delete <EmployeeViewModel>(ElasticSearchIndexDocumentNames.EmployeeIndexName, eventSaveData.Id);
                    }

                    await new CreateAndUpdatePointDocumentIndex(_elasticSearchService).UpdatePointDocument(
                        ElasticSearchIndexDocumentNames.EmployeeDocumentPositionIndexName,
                        ElasticSearchIndexDocumentNames.EmployeeDocumentPositionAliasName,
                        ElasticSearchIndexDocumentNames.EmployeeDocumentPositionName, @event.OriginalPosition.GetValueOrDefault());
                }
                catch (Exception exception)
                {
                    _logger.LogError(exception, exception.Message);
                }
            },
                liveProcessingStarted: (sub) =>
            {
                _logger.LogInformation("{SubscriptionName} subscription started.", sub.SubscriptionName);
            },
                subscriptionDropped: (sub, subDropReason, exception) =>
            {
                _logger.LogWarning("{SubscriptionName} dropped. Reason: {SubDropReason}.", sub.SubscriptionName, subDropReason);
            });
        }