diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-11-28 16:53:09 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-11-28 16:53:09 +0200 |
| commit | e6e91a5c5435e524876d44d91b92c90f990a60c7 (patch) | |
| tree | ec95738b8d9b349c94255ffdb1748e893b9a6cec /Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs | |
| parent | 242cb9fd566647406792d63547b61ec1d6a5f3bc (diff) | |
| download | Tango-e6e91a5c5435e524876d44d91b92c90f990a60c7.tar.gz Tango-e6e91a5c5435e524876d44d91b92c90f990a60c7.zip | |
Implemented setup and update notifications from PPC to service.
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs | 274 |
1 files changed, 182 insertions, 92 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs index d312609bb..591a1293d 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs @@ -84,6 +84,124 @@ namespace Tango.PPC.Common.MachineUpdate }); } + private async void OnFailed(Exception ex, TaskCompletionSource<MachineUpdateResult> completionSource, DownloadUpdateResponse response, bool performDatabaseRollback, String dbBackupFile, Tango.Core.DataSource localDataSource) + { + LogManager.Log(ex, "An error occurred in machine update."); + + if (performDatabaseRollback) + { + LogManager.Log("Rolling back database changes..."); + + using (DbManager db = DbManager.FromDataSource(localDataSource)) + { + try + { + UpdateProgress("Rollback", "Rolling back database changes..."); + await Task.Factory.StartNew(() => db.Restore(localDataSource.Catalog, dbBackupFile)); + LogManager.Log("Database restored successfully."); + } + catch (Exception e) + { + LogManager.Log(e, "Could not rollback the database."); + throw ex; + } + finally + { + try + { + File.Delete(dbBackupFile); + } + catch { } + } + } + } + + completionSource.SetException(ex); + + if (response != null) + { + try + { + var result = await _client.NotifyUpdateCompleted(new MachineUpdateCompletedRequest() + { + Token = response.NotifyCompletedToken, + Status = BL.Enumerations.TangoUpdateStatuses.UpdateFailed, + FailedReason = ex.FlattenMessage(), + }); + } + catch (Exception xx) + { + LogManager.Log(xx, "Error notifying update completed."); + } + } + } + + private async void OnCompleted(MachineUpdateResult result, TaskCompletionSource<MachineUpdateResult> completionSource, DownloadUpdateResponse response, String dbBackupFile) + { + try + { + File.Delete(dbBackupFile); + } + catch { } + + completionSource.SetResult(result); + + if (response != null) + { + try + { + var r = await _client.NotifyUpdateCompleted(new MachineUpdateCompletedRequest() + { + Token = response.NotifyCompletedToken, + Status = BL.Enumerations.TangoUpdateStatuses.UpdateCompleted, + }); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error notifying update completed."); + } + } + } + + private async void OnCompleted(UpdateDBResponse response) + { + if (response != null) + { + try + { + var r = await _client.NotifyUpdateCompleted(new MachineUpdateCompletedRequest() + { + Token = response.NotifyCompletedToken, + Status = BL.Enumerations.TangoUpdateStatuses.DatabaseCompleted, + }); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error notifying update completed."); + } + } + } + + private async void OnFailed(Exception ex, UpdateDBResponse response) + { + if (response != null) + { + try + { + var r = await _client.NotifyUpdateCompleted(new MachineUpdateCompletedRequest() + { + Token = response.NotifyCompletedToken, + Status = BL.Enumerations.TangoUpdateStatuses.DatabaseFailed, + FailedReason = ex.FlattenMessage(), + }); + } + catch (Exception xx) + { + LogManager.Log(xx, "Error notifying update completed."); + } + } + } + #endregion #region Public Methods @@ -108,6 +226,7 @@ namespace Tango.PPC.Common.MachineUpdate var localDataSource = SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource; bool performDatabaseRollback = false; String dbBackupFile = null; + DownloadUpdateResponse update_response = null; try { @@ -148,8 +267,6 @@ namespace Tango.PPC.Common.MachineUpdate DownloadUpdateRequest request = new DownloadUpdateRequest(); request.SerialNumber = serialNumber; - DownloadUpdateResponse update_response = null; - update_response = await _client.MachineUpdate(request); LogManager.Log($"Machine update response received: {Environment.NewLine}{update_response.ToJsonString()}"); @@ -304,21 +421,21 @@ namespace Tango.PPC.Common.MachineUpdate handler.Failed += (_, ex) => { stream.Dispose(); - throw ex; + OnFailed(ex, result, update_response, performDatabaseRollback, dbBackupFile, localDataSource); }; handler.Completed += (_, __) => { UpdateProgress("Updating Firmware", "Firmware update completed successfully."); stream.Dispose(); - result.SetResult(new MachineUpdateResult() + OnCompleted(new MachineUpdateResult() { UpdatePackagePath = _newPackageTempFolder, - }); + }, result, update_response, dbBackupFile); }; handler.Canceled += (_, __) => { stream.Dispose(); - throw new Exception("The operation has been canceled."); + OnFailed(new Exception("The operation has been canceled."), result, update_response, performDatabaseRollback, dbBackupFile, localDataSource); }; handler.Progress += (_, e) => { @@ -327,53 +444,15 @@ namespace Tango.PPC.Common.MachineUpdate } else { - result.SetResult(new MachineUpdateResult() + OnCompleted(new MachineUpdateResult() { UpdatePackagePath = _newPackageTempFolder, - }); + }, result, update_response, dbBackupFile); } } catch (Exception ex) { - LogManager.Log(ex, "An error occurred in machine update."); - - if (performDatabaseRollback) - { - LogManager.Log("Rolling back database changes..."); - - using (DbManager db = DbManager.FromDataSource(localDataSource)) - { - try - { - UpdateProgress("Rollback", "Rolling back database changes..."); - await Task.Factory.StartNew(() => db.Restore(localDataSource.Catalog, dbBackupFile)); - LogManager.Log("Database restored successfully."); - } - catch (Exception e) - { - LogManager.Log(e, "Could not rollback the database."); - throw ex; - } - finally - { - try - { - File.Delete(dbBackupFile); - } - catch { } - } - } - } - - result.SetException(ex); - } - finally - { - try - { - File.Delete(dbBackupFile); - } - catch { } + OnFailed(ex, result, update_response, performDatabaseRollback, dbBackupFile, localDataSource); } return await result.Task; @@ -421,73 +500,84 @@ namespace Tango.PPC.Common.MachineUpdate { return Task.Factory.StartNew(() => { - LogManager.Log("Starting database update..."); + UpdateDBResponse update_response = null; - UpdateProgress("Updating Database", "Initializing..."); + try + { + LogManager.Log("Starting database update..."); - LogManager.Log("Looking for update scripts configuration on application path..."); + UpdateProgress("Updating Database", "Initializing..."); - String config_file = Path.Combine(PathHelper.GetStartupPath(), "Update Scripts", "config.xml"); + LogManager.Log("Looking for update scripts configuration on application path..."); - if (!File.Exists(config_file)) - { - throw LogManager.Log(new FileNotFoundException($"Could not locate '{config_file}' file on application folder.")); - } + String config_file = Path.Combine(PathHelper.GetStartupPath(), "Update Scripts", "config.xml"); - UpdateDBResponse update_response = dbCompareResult.UpdateDBResponse; - - var localDataSource = SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource; + if (!File.Exists(config_file)) + { + throw LogManager.Log(new FileNotFoundException($"Could not locate '{config_file}' file on application folder.")); + } - LogManager.Log($"Updating database '{update_response.DataSource.ToString()}' => '{localDataSource.ToString()}'..."); + update_response = dbCompareResult.UpdateDBResponse; - UpdateProgress("Updating Database", "Initializing update sequence..."); + var localDataSource = SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource; - ExaminerSequenceConfiguration config_sequence = ExaminerSequenceConfiguration.FromFile(config_file); + LogManager.Log($"Updating database '{update_response.DataSource.ToString()}' => '{localDataSource.ToString()}'..."); - foreach (var item in config_sequence.Items.Where(x => x.Type == ExaminerSequenceItemType.Data || update_response.PerformSchemaUpdate).OrderBy(x => x.Index)) - { - LogManager.Log($"Executing update script '{item.FileName}...'"); + UpdateProgress("Updating Database", "Initializing update sequence..."); - ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(Path.Combine(Path.GetDirectoryName(config_file), item.FileName)); - builder.SetSource(update_response.DataSource); - builder.SetTarget(localDataSource); + ExaminerSequenceConfiguration config_sequence = ExaminerSequenceConfiguration.FromFile(config_file); - if (item.RequiresSerialNumber) + foreach (var item in config_sequence.Items.Where(x => x.Type == ExaminerSequenceItemType.Data || update_response.PerformSchemaUpdate).OrderBy(x => x.Index)) { - builder.SetMachineSerialNumber(serialNumber); - } + LogManager.Log($"Executing update script '{item.FileName}...'"); - builder.Synchronize(); + ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(Path.Combine(Path.GetDirectoryName(config_file), item.FileName)); + builder.SetSource(update_response.DataSource); + builder.SetTarget(localDataSource); - var config = builder.Build(); + if (item.RequiresSerialNumber) + { + builder.SetMachineSerialNumber(serialNumber); + } - ExaminerProcess process = new ExaminerProcess(config, item.Type == ExaminerSequenceItemType.Data ? ExaminerProcessType.Data : ExaminerProcessType.Schema); - process.Progress += (x, msg) => - { - LogManager.Log(msg); - }; + builder.Synchronize(); - try - { - UpdateProgress("Updating Database", item.Name + "..."); + var config = builder.Build(); - var result = process.Execute().Result; + ExaminerProcess process = new ExaminerProcess(config, item.Type == ExaminerSequenceItemType.Data ? ExaminerProcessType.Data : ExaminerProcessType.Schema); + process.Progress += (x, msg) => + { + LogManager.Log(msg); + }; - if (result.ExitCode != ExaminerProcessExitCode.Success) + try { - throw LogManager.Log(new InvalidDataException($"{item.FileName} script has terminated with exit code '{result.ExitCode}'.")); - } + UpdateProgress("Updating Database", item.Name + "..."); - LogManager.Log("Script executed successfully."); - } - catch (Exception ex) - { - throw LogManager.Log(ex, "Setup manager error while trying to update the database."); + var result = process.Execute().Result; + + if (result.ExitCode != ExaminerProcessExitCode.Success) + { + throw LogManager.Log(new InvalidDataException($"{item.FileName} script has terminated with exit code '{result.ExitCode}'.")); + } + + LogManager.Log("Script executed successfully."); + } + catch (Exception ex) + { + throw LogManager.Log(ex, "Setup manager error while trying to update the database."); + } } - } - UpdateProgress("Updating Database", "Database synchronization completed successfully."); - LogManager.Log("Update completed successfully."); + UpdateProgress("Updating Database", "Database synchronization completed successfully."); + LogManager.Log("Update completed successfully."); + OnCompleted(update_response); + } + catch (Exception ex) + { + OnFailed(ex, update_response); + throw; + } }); } |
