Example #1
0
        private void CheckUserSettings(Data.Json.UserJson json, ILogger log)
        {
            if (json == null)
            {
                return;
            }

            try {
                var data   = ServiceContainer.Resolve <AuthManager> ().User;
                var errors =
                    (data.StartOfWeek == json.StartOfWeek ? "" : "StartOfWeek ") +
                    (data.DateFormat == json.DateFormat ? "" : "DateFormat ") +
                    (data.TimeFormat == json.TimeFormat ? "" : "TimeFormat ") +
                    (data.ImageUrl == json.ImageUrl ? "" : "ImageUrl ") +
                    (data.Locale == json.Locale ? "" : "Locale ") +
                    (data.Timezone == json.Timezone ? "" : "Timezone ") +
                    (data.SendProductEmails == json.SendProductEmails ? "" : "SendProductEmails ") +
                    (data.SendTimerNotifications == json.SendTimerNotifications ? "" : "SendTimerNotifications ") +
                    (data.SendWeeklyReport == json.SendWeeklyReport ? "" : "SendWeeklyReport ") +
                    (data.TrackingMode == (json.StoreStartAndStopTime ? TrackingMode.StartNew : TrackingMode.Continue) ? "" : "TrackingMode ") +
                    //(data.DefaultWorkspaceId == defaultWorkspaceId ? "" : "defaultWorkspaceId ") +
                    (data.DurationFormat == json.DurationFormat ? "" : "DurationFormat ") +
                    (data.ExperimentIncluded == (json.OBM != null && json.OBM.Included) ? "" : "ExperimentIncluded ") +
                    (data.ExperimentNumber == (json.OBM == null ? 0 : json.OBM.Number) ? "" : "ExperimentNumber ");

                if (!string.IsNullOrWhiteSpace(errors))
                {
                    log.Error(Tag, "Server and app user settings differ: " + errors);
                }
            } catch (Exception e) {
                log.Error(Tag, e, "Cannot check user data: {0}", e.Message);
            }
        }
Example #2
0
        private async Task <DateTime?> RunInBackground(SyncMode mode, DateTime?lastRun)
        {
            var bus = ServiceContainer.Resolve <MessageBus> ();
            var log = ServiceContainer.Resolve <ILogger> ();

            var syncDuration = Stopwatch.StartNew();

            // Resolve automatic sync mode to actual mode
            if (mode == SyncMode.Auto)
            {
                if (lastRun != null && lastRun > Time.UtcNow - TimeSpan.FromMinutes(5))
                {
                    mode = SyncMode.Push;
                }
                else
                {
                    mode = SyncMode.Full;
                }
                log.Info(Tag, "Starting automatic ({0}) sync.", mode.ToString());
            }
            else
            {
                log.Info(Tag, "Starting {0} sync.", mode.ToString());
            }

            bus.Send(new SyncStartedMessage(this, mode));

            bool      hasErrors = false;
            Exception ex        = null;

            try {
                Data.Json.UserJson userJson = null;
                if (mode == SyncMode.Full)
                {
                    await CollectGarbage().ConfigureAwait(false);
                }

                if (mode.HasFlag(SyncMode.Pull))
                {
                    var tuple = await PullChanges(lastRun).ConfigureAwait(false);

                    lastRun  = tuple.Item1;
                    userJson = tuple.Item2;
                }

                // Resolve conflicts in client (not server)
                await ResolveConflicts();

                if (mode.HasFlag(SyncMode.Push))
                {
                    hasErrors = await PushChanges().ConfigureAwait(false);
                }

                // TODO: Check userData has synced properly #1390
                CheckUserSettings(userJson, log);
            } catch (Exception e) {
                if (e.IsNetworkFailure() || e is TaskCanceledException)
                {
                    log.Info(Tag, e, "Sync ({0}) failed.", mode);
                    if (e.IsNetworkFailure())
                    {
                        ServiceContainer.Resolve <INetworkPresence> ().RegisterSyncWhenNetworkPresent();
                    }
                }
                else
                {
                    log.Warning(Tag, e, "Sync ({0}) failed.", mode);
                }

                hasErrors = true;
                ex        = e;
            } finally {
                syncDuration.Stop();
                log.Info(Tag, "Sync finished in {0}ms{1}.", syncDuration.ElapsedMilliseconds, hasErrors ? " (with errors)" : String.Empty);
                bus.Send(new SyncFinishedMessage(this, mode, hasErrors, ex));
            }

            return(lastRun);
        }