Esempio n. 1
0
        public async Task ExecuteAsync()
        {
            var jobInfo             = GetJobInfo();
            var downloadCount       = 0;
            var lastWrittenBatchTag = jobInfo.LastBatchTag;

            // Set date to a default of today.
            // Either we continue where we left off, or we grab as many batches as allowed
            // (i.e., a few days worth back from today)
            var date             = _dateTimeProvider.Snapshot.Date;
            var batchTagDatePart = lastWrittenBatchTag.Split("-").FirstOrDefault();

            if (!string.IsNullOrEmpty(batchTagDatePart))
            {
                // All is good, start requesting batch from where we left off.
                // The date and the batchTag's creation date need to be the same,
                // otherwise EFGS will return a HTTP status 404.
                date = DateTime.ParseExact(batchTagDatePart, "yyyyMMdd", null);
            }
            else
            {
                // If lastWrittenBatchTag is somehow unusable or unavailable,
                // go as far back as allowed and don't send a batchTag to EFGS.
                date = date.AddDays(_efgsConfig.DaysToDownload * -1);
                lastWrittenBatchTag = string.Empty;
            }

            // If we have a batchTag, we will re-request that batch from EFGS to start our run.

            // If we do not have a batchTag (it is null or empty), the first batch of the date requested will be returned by EFGS.
            // We may already have some of that requested date's batches,
            // but we may not have all its batches or batches from the subsequent days.

            var result = await _receiverFactory().ExecuteAsync(date, lastWrittenBatchTag);

            downloadCount++;

            while (result != null && downloadCount <= _efgsConfig.MaxBatchesPerRun)
            {
                // If we haven't already received the current batchTag, process it
                if (!_iksInDbContext.Received.Any(x => x.BatchTag == result.BatchTag))
                {
                    _logger.WriteProcessingData(date, result.BatchTag);

                    try
                    {
                        await WriteSingleBatchAsync(result.BatchTag, result.Content);

                        lastWrittenBatchTag = result.BatchTag;
                        await UpdateJobInfoAsync(jobInfo, lastWrittenBatchTag);
                    }
                    catch (Exception e)
                    {
                        _logger.WriteEfgsError(e);
                    }
                }
                else
                {
                    _logger.WriteBatchAlreadyProcessed(result.BatchTag);
                }

                // Move on to the next batchTag
                if (!string.IsNullOrEmpty(result.NextBatchTag))
                {
                    _logger.WriteNextBatchFound(result.NextBatchTag);
                    _logger.WriteBatchProcessedInNextLoop(result.NextBatchTag);

                    result = await _receiverFactory().ExecuteAsync(date, result.NextBatchTag);

                    downloadCount++;
                }
                else
                {
                    // No next batch available, we're done for lastWrittenBatchTag's day's set of batches.
                    _logger.WriteNoNextBatch();

                    // Check if we can move on to a possible next day's worth of batches,
                    // now that this current set of batches is finished.
                    // Don't move past today though :)
                    if (date < _dateTimeProvider.Snapshot.Date)
                    {
                        _logger.WriteMovingToNextDay();
                        date   = date.AddDays(1);
                        result = await _receiverFactory().ExecuteAsync(date, string.Empty);

                        downloadCount++;
                    }
                    else
                    {
                        // No more days with batches available, we're done.
                        _logger.WriteNoNextBatchNoMoreDays();
                        result = null;
                    }
                }

                // Log this for informational purposes
                if (downloadCount > _efgsConfig.MaxBatchesPerRun)
                {
                    _logger.WriteBatchMaximumReached(_efgsConfig.MaxBatchesPerRun);
                }
            }
        }
Esempio n. 2
0
        public async Task ExecuteAsync()
        {
            var downloadCount = 0;
            var jobInfo       = GetJobInfo();
            var batchDate     = jobInfo.LastRun;
            var previousBatch = jobInfo.LastBatchTag;
            var batch         = jobInfo.LastBatchTag;

            // All of the values on first run are empty, all we need to do is set the date to the first date and we can start
            if (jobInfo.LastRun == DateTime.MinValue)
            {
                batchDate = _DateTimeProvider.Snapshot.Date.AddDays(_EfgsConfig.DaysToDownload * -1);
            }

            // TODO: save JobInfo for each loop and refresh it for each loop. Remove the other variables.
            while (true)
            {
                downloadCount++;

                if (downloadCount == _EfgsConfig.MaxBatchesPerRun)
                {
                    _Logger.WriteBatchMaximumReached(_EfgsConfig.MaxBatchesPerRun);
                    break;
                }

                var result = await _ReceiverFactory().ExecuteAsync(batch, batchDate);

                // Handle no found / gone
                if (result == null)
                {
                    // Move to the next day if possible
                    if (batchDate < _DateTimeProvider.Snapshot.Date)
                    {
                        batchDate = batchDate.AddDays(1);
                        batch     = string.Empty;

                        continue;
                    }

                    break;
                }

                _Logger.WriteNextBatchReceived(result.BatchTag, result.NextBatchTag);

                // Process a previous batch
                if (result.BatchTag == previousBatch || _IksInDbContext.Received.Any(_ => _.BatchTag == batch))
                {
                    _Logger.WriteBatchAlreadyProcessed(result.BatchTag);

                    // New batch so process it next time
                    if (!string.IsNullOrWhiteSpace(result.NextBatchTag))
                    {
                        _Logger.WriteBatchProcessedInNextLoop(result.NextBatchTag);

                        batch = result.NextBatchTag;
                    }
                }
                else
                {
                    // Process a new batch
                    var writer = _WriterFactory();

                    await writer.Execute(new IksWriteArgs
                    {
                        BatchTag = result.BatchTag,
                        Content  = result.Content
                    });

                    // Update the job info as a batch has been downloaded
                    jobInfo.LastRun      = batchDate;
                    jobInfo.LastBatchTag = result.BatchTag;

                    previousBatch = result.BatchTag;

                    // Move to the next batch if we have one
                    if (!string.IsNullOrWhiteSpace(result.NextBatchTag))
                    {
                        batch = result.NextBatchTag;
                    }
                }

                // No next batch, move to the next date or stop
                if (string.IsNullOrWhiteSpace(result.NextBatchTag))
                {
                    // Move to the next day if possible
                    if (batchDate < _DateTimeProvider.Snapshot.Date)
                    {
                        batchDate = batchDate.AddDays(1);
                        batch     = string.Empty;

                        _Logger.WriteMovingToNextDay();

                        continue;
                    }

                    _Logger.WriteNoNextBatch();
                    break;
                }

                _Logger.WriteNextBatchFound(result.NextBatchTag);
            }

            // TODO the downloaded batches must also be done in one transaction
            await _IksInDbContext.SaveChangesAsync();
        }