diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-10-21 05:10:59 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-10-21 05:10:59 +0300 |
| commit | 573eed2d8575a29d09c9602acf1f2765adc9abcb (patch) | |
| tree | 1c5d1b7b1f606b69e79d33097aade57571cc194e /Software/Visual_Studio/PPC/Tango.PPC.Common/Synchronization | |
| parent | 7956c30b196eb5d0d322c08bd5125eaae30f69e5 (diff) | |
| download | Tango-573eed2d8575a29d09c9602acf1f2765adc9abcb.tar.gz Tango-573eed2d8575a29d09c9602acf1f2765adc9abcb.zip | |
DataStore Synchronization !
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/Synchronization')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Common/Synchronization/DefaultMachineDataSynchronizer.cs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Synchronization/DefaultMachineDataSynchronizer.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Synchronization/DefaultMachineDataSynchronizer.cs index 645ddbdf5..4ffb6b751 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Synchronization/DefaultMachineDataSynchronizer.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Synchronization/DefaultMachineDataSynchronizer.cs @@ -41,6 +41,7 @@ namespace Tango.PPC.Common.Synchronization public int MaxJobRuns { get; set; } public int MaxMachinesEvents { get; set; } public int MaxOfflineUpdates { get; set; } + public int MaxDataStoreItems { get; set; } private SynchronizationStatus _currentStatus; public SynchronizationStatus CurrentStatus @@ -82,6 +83,7 @@ namespace Tango.PPC.Common.Synchronization MaxJobRuns = 10; MaxMachinesEvents = 10; MaxOfflineUpdates = 10; + MaxDataStoreItems = 100; var settings = SettingsManager.Default.GetOrCreate<PPCSettings>(); Interval = settings.SynchronizationInterval; @@ -207,6 +209,21 @@ namespace Tango.PPC.Common.Synchronization request.OfflineUpdates.Add(dto); } } + + { //Always synchronize data store items + LogManager.Log("Checking Data Store Items..."); + + var dataStoreItems = await db.DataStoreItems.Where(x => !x.IsSynchronized).Take(MaxDataStoreItems).ToListAsync(); + List<DataStoreItemDTO> dtos = new List<DataStoreItemDTO>(); + + foreach (var item in dataStoreItems) + { + item.IsSynchronized = true; + var dto = DataStoreItemDTO.FromObservable(item); + dto.MachineGuid = null; + request.DataStoreItems.Add(dto); + } + } } return request; @@ -280,6 +297,22 @@ namespace Tango.PPC.Common.Synchronization } } + //Finalize data store items + foreach (var dataStoreItem in request.DataStoreItems) + { + var failedDataStoreItem = response.FailedDataStoreItems.SingleOrDefault(x => x.Guid == dataStoreItem.Guid); + + if (failedDataStoreItem == null) + { + var dbDataStoreItem = await db.DataStoreItems.SingleOrDefaultAsync(x => x.Guid == dataStoreItem.Guid); + dbDataStoreItem.IsSynchronized = true; + } + else + { + LogManager.Log($"Synchronization Error - DataStoreItem '{dataStoreItem.Key}' cannot be stored on the server due to the following reason:\n{failedDataStoreItem.Reason}", LogCategory.Error); + } + } + await db.SaveChangesAsync(); } } @@ -291,9 +324,11 @@ namespace Tango.PPC.Common.Synchronization RequestJobs = syncJobs, RequestJobRuns = syncDiagnostics, RequestMachineEvents = syncDiagnostics, + RequestDataStoreItems = true, MaxJobs = MaxJobs, MaxJobRuns = MaxJobRuns, MaxMachinesEvents = MaxMachinesEvents, + MaxDataStoreItems = MaxDataStoreItems, }); } @@ -342,6 +377,48 @@ namespace Tango.PPC.Common.Synchronization } } + //Insert/Update Data Store Items. + if (response.DataStoreItems.Count > 0) + { + LogManager.Log("Inserting/Updating Data Store Items..."); + } + foreach (var dto in response.DataStoreItems) + { + using (ObservablesContext db = ObservablesContext.CreateDefault()) + { + try + { + var dataStoreItem = dto.ToObservable(); + + dataStoreItem.ID = 0; + dataStoreItem.MachineGuid = null; + dataStoreItem.IsSynchronized = true; + + var existingItem = db.DataStoreItems.SingleOrDefault(x => x.Guid == dataStoreItem.Guid); + + if (existingItem == null) + { + db.DataStoreItems.Add(dataStoreItem); + db.SaveChanges(); + } + else if (dataStoreItem.LastUpdated >= existingItem.LastUpdated) + { + existingItem.DataType = dataStoreItem.DataType; + existingItem.Value = dataStoreItem.Value; + existingItem.IsSynchronized = true; + existingItem.LastUpdated = dataStoreItem.LastUpdated; + db.SaveChanges(); + } + + request.SynchronizedDataStoreItems.Add(dataStoreItem.Guid); + } + catch (Exception ex) + { + LogManager.Log($"Synchronization Error - DataStoreItem '{dto.Key}' cannot be stored locally due to the following reason:\n{ex.FlattenMessage()}", LogCategory.Error); + } + } + } + //Insert JobRuns. if (response.JobRuns.Count > 0) { |
