public async Task <IActionResult> GetFilteredImportedPollingStations(
            [FromRoute] Guid jobId,
            [FromQuery] PaginationQuery pagination,
            [FromQuery] ImportedPollingStationsQuery query)
        {
            var result = await _mediator.Send(new SearchImportedPollingStations(jobId, pagination, query));

            if (result.IsFailure)
            {
                return(Problem(result.Error));
            }

            return(Ok(result.Value));
        }
        private async Task BackgroundProcessing(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                var jobId = await _jobsQueue.DequeueAsync(stoppingToken);

                using (var scope = _serviceScopeFactory.CreateScope())
                {
                    var importJobsRepository = scope.ServiceProvider.GetService <IImportJobsRepository>();
                    var importedPollingStationsRepository =
                        scope.ServiceProvider.GetService <IImportedPollingStationsRepository>();
                    var locationSearchService = scope.ServiceProvider.GetService <IAddressLocationSearchService>();


                    try
                    {
                        var jobStatusResult = await importJobsRepository.GetImportJobStatus(jobId);

                        if (jobStatusResult.IsFailure)
                        {
                            continue;
                        }

                        var jobStatus = jobStatusResult.Value.Status;
                        if (jobStatus == JobStatus.Canceled || jobStatus == JobStatus.Finished)
                        {
                            continue;
                        }

                        var query = new ImportedPollingStationsQuery
                        {
                            ResolvedAddressStatus = ResolvedAddressStatusType.NotProcessed
                        };

                        var defaultPagination = new PaginationQuery();

                        var pollingStations =
                            await importedPollingStationsRepository.GetImportedPollingStationsAsync(jobId, query,
                                                                                                    defaultPagination);

                        if (pollingStations.IsFailure)
                        {
                            continue;
                        }

                        if (pollingStations.Value.RowCount == 0)
                        {
                            await importJobsRepository.UpdateJobStatus(jobId, JobStatus.Finished);

                            continue;
                        }

                        await importJobsRepository.UpdateJobStatus(jobId, JobStatus.Started);

                        foreach (var ps in pollingStations.Value.Results)
                        {
                            var locationSearchResult = await locationSearchService.FindCoordinates(ps.County, ps.Address);

                            ps.ResolvedAddressStatus = locationSearchResult.OperationStatus;
                            ps.Latitude    = locationSearchResult.Latitude;
                            ps.Longitude   = locationSearchResult.Longitude;
                            ps.FailMessage = locationSearchResult.ErrorMessage;

                            await importedPollingStationsRepository.UpdateImportedPollingStation(jobId, ps);
                        }

                        _jobsQueue.QueueBackgroundWorkItem(jobId);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex,
                                         "Error occurred executing {WorkItem}.", nameof(jobId));
                    }
                }
            }
        }
Example #3
0
 public SearchImportedPollingStations(Guid jobId, PaginationQuery pagination, ImportedPollingStationsQuery query)
 {
     JobId      = jobId;
     Pagination = pagination;
     Query      = query;
 }