Пример #1
0
        public async Task RunPriceListSynchronizationAsync()
        {
            var statistics = new PriceListSynchronizationStatistics();

            try
            {
                await _ekSearchManagementClient.CreateProductIndexIfNotExistAsync();

                var       from  = 0;
                const int count = 1_000;

                using (var searchIndexClient = _ekSearchManagementClient.CreateSearchIndexClient())
                {
                    searchIndexClient.IndexName = _ekSearchManagementClient.ProductsIndexName;
                    var updateUtcTimestamp = TimestampHelper.GetCurrentUtcTotalMinutes();

                    while (true)
                    {
                        var isFinalPage = await RequestAndIndexPriceListPageAsync(searchIndexClient, from, count, updateUtcTimestamp, statistics);

                        if (isFinalPage)
                        {
                            break;
                        }

                        from += count;
                    }

                    // wait half of minute until changes are applied
                    await Task.Delay(TimeSpan.FromSeconds(30));

                    await PriceListHelper.CleanExpiredRecordsAsync(
                        searchIndexClient,
                        EkProductSourceEnum.OmegaAutoBiz,
                        updateUtcTimestamp,
                        statistics,
                        _logger);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.UnhandledException, ex, "Omega price list synchronization failed.");
                await _notificationManager.SendWorkerMessageAsync($"ERROR! Omega price list synchronization failed, {statistics}: {ex.Message}");

                throw;
            }

            await _notificationManager.SendWorkerMessageAsync($"Omega price list synchronization succeed, {statistics}.");
        }
Пример #2
0
        public async Task RunPriceListSynchronizationAsync()
        {
            var    cancellationToken = CancellationToken.None;
            var    statistics        = new PriceListSynchronizationStatistics();
            string priceListStatusMessage;

            try
            {
                var lastAppliedPriceListId = await _persistentCache.GetValueAsync(LastAppliedElitPriceListIdKey, cancellationToken);

                var priceList = await _elitUaClient.GetUnappliedPriceListAsync(lastAppliedPriceListId, cancellationToken);

                if (!priceList.IsSuccess)
                {
                    throw new InvalidOperationException(priceList.StatusMessage);
                }

                priceListStatusMessage = priceList.StatusMessage;

                if (priceList.Records?.Count > 0)
                {
                    await _ekSearchManagementClient.CreateProductIndexIfNotExistAsync();

                    using (var searchIndexClient = _ekSearchManagementClient.CreateSearchIndexClient())
                    {
                        searchIndexClient.IndexName = _ekSearchManagementClient.ProductsIndexName;
                        var updateUtcTimestamp = TimestampHelper.GetCurrentUtcTotalMinutes();

                        // max size of Azure Search batch
                        const int PageSize = 1_000;
                        var       page     = 0;
                        foreach (var priceListPage in priceList.Records.Batch(PageSize))
                        {
                            _logger.LogTrace(LoggingEvents.Synchronization, $"Processing {page*PageSize}+{PageSize}...");

                            await IndexPriceListPageAsync(searchIndexClient, priceListPage.ToArray(), updateUtcTimestamp, statistics);

                            page++;
                        }

                        // wait half of minute until changes are applied
                        await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);

                        await PriceListHelper.CleanExpiredRecordsAsync(
                            searchIndexClient,
                            EkProductSourceEnum.ElitUa,
                            updateUtcTimestamp,
                            statistics,
                            _logger);
                    }
                }

                await _persistentCache.SetValueAsync(LastAppliedElitPriceListIdKey, priceList.PriceListId, CancellationToken.None);
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.UnhandledException, ex, "Elit price list synchronization failed.");
                await _notificationManager.SendWorkerMessageAsync($"ERROR! Elit price list synchronization failed, {statistics}: {ex.Message}");

                throw;
            }

            await _notificationManager.SendWorkerMessageAsync($"Elit price list synchronization succeed, {statistics}, status: {priceListStatusMessage}");
        }