private void PersistentRecords(object state)
        {
            Debug.WriteLine($"{nameof(PersistentRecords)} is running on thread id={Thread.CurrentThread.ManagedThreadId}");

            if (_isIdle)
            {
                return;
            }

            if (_mainRepositoryUnavailable)
            {
                var result = TryGetFromMainRepository();

                if (result.Item2 == false)
                {
                    BackupLocally();
                    return;
                }
                else
                {
                    _mainRepositoryUnavailable = false;

                    if (result.Item1 != null)
                    {
                        _bufferedTodayAppRecord.MergeWith(result.Item1);
                        _backupRepository.Delete(_bufferedTodayAppRecord.Id);
                    }
                }
            }

            try
            {
                var todayPersistentRecord = _repository.Get(AppUsageRecord.GetGeneratedId(DateTime.Now));
                if (todayPersistentRecord != null)
                {
                    todayPersistentRecord.ActiveApps = _bufferedTodayAppRecord.ActiveApps;

                    _repository.Update(todayPersistentRecord, todayPersistentRecord.Id);
                }
                else
                {
                    _repository.Add(_bufferedTodayAppRecord);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error when persisting data: {ex.Message}");

                LoggingService.LogException(ex);

                _mainRepositoryUnavailable = true;

                this.PersistentRecords(state);
            }


            Debug.WriteLine("Persisting data sucessfully");
        }
Ejemplo n.º 2
0
        void Should_merge_with_other_record_correctly()
        {
            var record1 = new AppUsageRecord(new DateTime(2018, 1, 1));

            record1.ActiveApps.Add(new KeyValuePair <string, ProcessInfo>("key1", new ProcessInfo("key1")
            {
                TotalAmountOfTime = TimeSpan.FromSeconds(1)
            }));

            var record2 = new AppUsageRecord(new DateTime(2018, 1, 1));

            record2.ActiveApps.Add(new KeyValuePair <string, ProcessInfo>("key2", new ProcessInfo("key2")
            {
                TotalAmountOfTime = TimeSpan.FromSeconds(2)
            }));


            record1.MergeWith(record2);

            Assert.Equal("key2", record1.ActiveApps["key2"].Id);
            Assert.Equal(TimeSpan.FromSeconds(2), record1.ActiveApps["key2"].TotalAmountOfTime);
        }