Пример #1
0
        public async Task <VesselUpdateModel> GetVesselUpdatesAsync(VesselAisUpdateModel aisUpdateModel, CancellationToken token, SemaphoreSlim semaphoreThrottel)
        {
            VesselUpdateModel vessel = null;

            try
            {
                bool skip = false;

                if (aisUpdateModel.Mmsi == 0) //if could not be scrapped with "full"
                {
                    skip = true;
                    _progress.AddSkipped();
                }

                await semaphoreThrottel.WaitAsync();

                if (!skip)
                {
                    vessel          = _scrapper.ScrapSingleVessel(aisUpdateModel.Mmsi, aisUpdateModel.Imo);
                    vessel.VesselId = aisUpdateModel.VesselId;

                    if (vessel.IMO != aisUpdateModel.Imo)
                    {
                        throw new Exception("Received vessel imo differs from the one passed.");
                    }

                    while (_progress.GetIsUpdatingDatabase() || _progress.GetIsUpdatingPaused())
                    {
                        await Task.Delay(100);
                    }

                    _progress.AddToReturnedResultsQuantity();
                }
            }
            catch (Exception ex)
            {
                _progress.SetLastError(ex.Message);
                _progress.AddFailedRequest();
                vessel = null;
            }
            finally
            {
                while (_progress.GetIsUpdatingDatabase() || _progress.GetIsUpdatingPaused())
                {
                    await Task.Delay(100);
                }

                semaphoreThrottel.Release();
            }

            return(vessel);
        }
        private void UpdateAllVessels(List <VesselUpdateModel> updatedVessels)
        {
            if (updatedVessels.Count > 0)
            {
                try
                {
                    _queryBuilder.CreateAndSendUpdatesQuery(updatedVessels); //todo: unit test catch
                }
                catch (Exception ex)
                {
                    _progress.SetLastError(ex.Message);
                    _progress.AddFailedRequest();
                }
            }

            UpdatePortCalls(updatedVessels);
        }
        private async Task ProcessNextStepAsync(List <VesselAisUpdateModel> updateList)
        {
            List <VesselUpdateModel> updatedVesselsStep = new List <VesselUpdateModel>();

            try
            {
                List <Task>             currentRunningTasks = new List <Task>();
                CancellationTokenSource tokenSource         = GetCancellationTokenSource();
                SemaphoreSlim           semaphoreThrottel   = GetSemaphoreThrottel();

                for (int i = _counter; i < _progress.GetCurrentUpdateStep(_counter, _configuration.GetValue <int>("Iteration:Step")); i++)
                {
                    int iteration = i;

                    currentRunningTasks.Add(Task.Run(async() =>
                    {
                        VesselUpdateModel updatedVessel = await _vesselUpdates.GetVesselUpdatesAsync(updateList[iteration], tokenSource.Token, semaphoreThrottel);

                        _progress.UpdateMissingProperties(updatedVessel);
                        _progress.SetLastUpdatedVessel(_stringParser.BuildUpdatedVesselInfo(updatedVessel));

                        lock (((ICollection)updatedVesselsStep).SyncRoot)
                        {
                            updatedVesselsStep.Add(updatedVessel);
                        }

                        _counter++;
                    }, tokenSource.Token));
                }

                await Task.WhenAll(currentRunningTasks);
            }
            catch (Exception ex)
            {
                _counter++;
                _progress.SetLastError(ex.Message + " from: " + _exceptionProcessor.GetMethodNameThrowingException(ex));
            }
            finally
            {
                _progress.SetUpdatingDatabaseTrue();
                SaveUpdatedVesselsStep(updatedVesselsStep);
                _progress.SetUpdatingDatabaseFalse();
            }
        }