/// <summary> /// Pull from remote /// </summary> public void Pull(SynchronizationPullTriggerType trigger) { // Pool startup sync if configured.. this.m_threadPool.QueueUserWorkItem((state) => { this.m_serverDrift = null; using (AuthenticationContext.EnterSystemContext()) { var logSvc = ApplicationContext.Current.GetService <ISynchronizationLogService>(); bool initialSync = !logSvc.GetAll().Any(); if (Monitor.TryEnter(this.m_lock, 100)) // Do we have a lock? { try { this.IsSynchronizing = true; DateTime lastSync = DateTime.MinValue; if (logSvc.GetAll().Count() > 0) { lastSync = logSvc.GetAll().Min(o => o.LastSync); } // Trigger if (!this.m_integrationService.IsAvailable()) { return; } int totalResults = 0; var syncTargets = this.m_configuration.SynchronizationResources.Where(o => (o.Triggers & trigger) != 0).ToList(); for (var i = 0; i < syncTargets.Count; i++) { var syncResource = syncTargets[i]; ApplicationContext.Current.SetProgress(String.Format(Strings.locale_startingPoll, syncResource.Name), (float)i / syncTargets.Count); foreach (var fltr in syncResource.Filters) { totalResults += this.Pull(syncResource.ResourceType, NameValueCollection.ParseQueryString(fltr), syncResource.Always, syncResource.Name); } if (syncResource.Filters.Count == 0) { totalResults += this.Pull(syncResource.ResourceType, new NameValueCollection(), false, syncResource.Name); } } ApplicationContext.Current.SetProgress(Strings.locale_pullComplete, 1.0f); if (totalResults > 0 && initialSync) { this.PullCompleted?.Invoke(this, new SynchronizationEventArgs(true, totalResults, lastSync)); } else if (totalResults > 0) { this.PullCompleted?.Invoke(this, new SynchronizationEventArgs(totalResults, lastSync)); } else { this.PullCompleted?.Invoke(this, new SynchronizationEventArgs(0, lastSync)); } } catch (Exception e) { this.m_tracer.TraceError("Cannot process startup command: {0}", e); } finally { this.IsSynchronizing = false; ApplicationContext.Current.SetProgress(Strings.locale_pullComplete, 1.0f); Monitor.Exit(this.m_lock); } } else { this.m_tracer.TraceWarning("Will not execute {0} due to - already pulling", trigger); } } }); }
/// <summary> /// Pull from remote /// </summary> private void Pull(SynchronizationPullTriggerType trigger) { // Pool startup sync if configured.. this.m_threadPool.QueueUserWorkItem((state) => { bool initialSync = !SynchronizationLog.Current.GetAll().Any(); if (Monitor.TryEnter(this.m_lock, 100)) // Do we have a lock? { try { this.IsSynchronizing = true; DateTime lastSync = DateTime.MinValue; if (SynchronizationLog.Current.GetAll().Count() > 0) { lastSync = SynchronizationLog.Current.GetAll().Min(o => o.LastSync); } // Trigger if (!this.m_integrationService.IsAvailable()) { if (trigger == SynchronizationPullTriggerType.OnStart) { ApplicationContext.Current.GetService <IThreadPoolService>().QueueUserWorkItem(new TimeSpan(0, 5, 0), o => this.Pull(SynchronizationPullTriggerType.OnStart), null); } return; } int totalResults = 0; var syncTargets = this.m_configuration.SynchronizationResources.Where(o => (o.Triggers & trigger) != 0).ToList(); for (var i = 0; i < syncTargets.Count; i++) { var syncResource = syncTargets[i]; ApplicationContext.Current.SetProgress(Strings.locale_startingPoll, (float)i / syncTargets.Count); foreach (var fltr in syncResource.Filters) { totalResults += this.Pull(syncResource.ResourceType, NameValueCollection.ParseQueryString(fltr), syncResource.Always, syncResource.Name); } if (syncResource.Filters.Count == 0) { totalResults += this.Pull(syncResource.ResourceType, new NameValueCollection(), false, syncResource.Name); } } ApplicationContext.Current.SetProgress(Strings.locale_startingPoll, 1.0f); // Pull complete? this.IsSynchronizing = false; if (totalResults > 0 && initialSync) { var alertService = ApplicationContext.Current.GetService <IAlertRepositoryService>(); alertService?.BroadcastAlert(new AlertMessage(AuthenticationContext.Current.Principal.Identity.Name, "everyone", Strings.locale_importDoneSubject, Strings.locale_importDoneBody, AlertMessageFlags.System)); this.PullCompleted?.Invoke(this, new SynchronizationEventArgs(true, totalResults, lastSync)); SQLiteConnectionManager.Current.Compact(); // Compact the DB on initial sync } else if (totalResults > 0) { this.PullCompleted?.Invoke(this, new SynchronizationEventArgs(totalResults, lastSync)); } else { this.PullCompleted?.Invoke(this, new SynchronizationEventArgs(0, lastSync)); } } catch (Exception e) { this.m_tracer.TraceError("Cannot process startup command: {0}", e); } finally { this.IsSynchronizing = false; Monitor.Exit(this.m_lock); } } else { this.m_tracer.TraceWarning("Will not execute {0} due to - already pulling", trigger); } }); }