public void PrepareSyncResult(EsasSyncResult esasSyncResult)
        {
            var dbContext = _esasStagingDbContextFactory.CreateDbContext();

            using (dbContext)
            {
                dbContext.EsasSyncResults.Add(esasSyncResult);
                dbContext.SaveChanges();
            }
        }
        public void PerformSynchronization(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                _logger.LogInformation("PerformSynchronization() was called...");

                if (_isSynchronizationActive)
                {
                    // do nothing if a sync is already in progress.
                    _logger.LogInformation("Sync is in progress - won't start a new one.");
                    return;
                }

                var syncBundlesToRun = _syncStrategyBundles.Where(bundle => bundle.IsTimeToExecute(DateTime.Now));

                // for every '_minuteIntervalBetweenEsasWsHealthcheck' minutes, do a health-check
                if (DateTime.Now.Minute % _minuteIntervalBetweenEsasWsHealthcheck == 0)
                {
                    _logger.LogInformation($"Running an ESAS WS-Healthcheck...");
                    EsasWebServiceHealthCheck healthCheck = _esasWebServiceHealthChecker.PerformHealthCheck();
                    var dbContext = _esasStagingDbContextFactory.CreateDbContext();
                    using (dbContext)
                    {
                        dbContext.HealthChecks.Add(healthCheck);
                        dbContext.SaveChanges();
                    }

                    _logger.LogInformation($"ESAS WS-Healthcheck reported status {healthCheck.HttpStatusCode}.");
                    if (healthCheck.HttpStatusCode != "200")
                    {
                        Exception healthCheckException = new Exception(healthCheck.Message);
                        _logger.LogError(healthCheckException.Message, healthCheckException);
                        return;
                    }
                }

                if (syncBundlesToRun.Any())
                {
                    _isSynchronizationActive = true;
                    executeSyncBundles(syncBundlesToRun);
                    _isSynchronizationActive = false;
                }
            }
            catch (Exception ex)
            {
                // Allow the service to continue, but do log the error.
                _logger.LogError("ERROR: " + ex.Message, ex);
                if (ex.InnerException != null)
                {
                    _logger.LogError("ERROR: " + ex.InnerException.Message, ex.InnerException);
                }
                _isSynchronizationActive = false;
            }
        }
        public DateTime GetLoadTimeCutoff(IEsasEntitiesLoaderStrategy esasEntitiesLoaderStrategy)
        {
            string loaderStrategyName = esasEntitiesLoaderStrategy.GetType().Name;
            var    dbContext          = esasDbContextFactory.CreateDbContext();

            using (dbContext)
            {
                var latestSuccesfulLoadTime = dbContext.EsasSyncResults
                                              .OrderByDescending(ls => ls.SyncStartTimeUTC)
                                              .FirstOrDefault(ls => ls.esasLoadResult.LoaderStrategyName == loaderStrategyName && ls.esasLoadResult.EsasLoadStatus == DAL.Models.EsasOperationResultStatus.OperationSuccesful &&
                                                              ls.esasSendResult.SendToDestinationStatus == DAL.Models.EsasOperationResultStatus.OperationSuccesful
                                                              );

                if (latestSuccesfulLoadTime != null)
                {
                    return(latestSuccesfulLoadTime.SyncStartTimeUTC);
                }
                else
                {
                    // there's not a latest load-time available to us, so let's go 10 minutes back in time
                    return(DateTime.UtcNow.AddMinutes(-10));
                }
            }
        }