aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs
diff options
context:
space:
mode:
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.cs124
1 files changed, 91 insertions, 33 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 591a1293d..dacd1b69a 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs
@@ -131,7 +131,7 @@ namespace Tango.PPC.Common.MachineUpdate
}
catch (Exception xx)
{
- LogManager.Log(xx, "Error notifying update completed.");
+ LogManager.Log(xx, "Error notifying update failed.");
}
}
}
@@ -163,41 +163,71 @@ namespace Tango.PPC.Common.MachineUpdate
}
}
- private async void OnCompleted(UpdateDBResponse response)
+ private void OnCompleted(UpdateDBResponse response)
{
if (response != null)
{
try
{
- var r = await _client.NotifyUpdateCompleted(new MachineUpdateCompletedRequest()
+ var r = _client.NotifyUpdateCompleted(new MachineUpdateCompletedRequest()
{
Token = response.NotifyCompletedToken,
Status = BL.Enumerations.TangoUpdateStatuses.DatabaseCompleted,
- });
+ }).Result;
}
catch (Exception ex)
{
- LogManager.Log(ex, "Error notifying update completed.");
+ LogManager.Log(ex, "Error notifying database completed.");
}
}
}
- private async void OnFailed(Exception ex, UpdateDBResponse response)
+ private void OnFailed(Exception ex, UpdateDBResponse response, bool performDatabaseRollback, String dbBackupFile, Tango.Core.DataSource localDataSource)
{
+ LogManager.Log(ex, "An error occurred in database update.");
+
+ if (performDatabaseRollback)
+ {
+ LogManager.Log("Rolling back database changes...");
+
+ using (DbManager db = DbManager.FromDataSource(localDataSource))
+ {
+ try
+ {
+ UpdateProgress("Rollback", "Rolling back database changes...");
+ 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 { }
+ }
+ }
+ }
+
if (response != null)
{
try
{
- var r = await _client.NotifyUpdateCompleted(new MachineUpdateCompletedRequest()
+ var r = _client.NotifyUpdateCompleted(new MachineUpdateCompletedRequest()
{
Token = response.NotifyCompletedToken,
Status = BL.Enumerations.TangoUpdateStatuses.DatabaseFailed,
FailedReason = ex.FlattenMessage(),
- });
+ }).Result;
}
catch (Exception xx)
{
- LogManager.Log(xx, "Error notifying update completed.");
+ LogManager.Log(xx, "Error notifying database failed.");
}
}
}
@@ -329,24 +359,23 @@ namespace Tango.PPC.Common.MachineUpdate
throw new InvalidProgramException("Database tango does not exists.");
}
- if (setupFirmware)
- {
- LogManager.Log("Setup firmware is active so a database rollback procedure should be configured.");
- UpdateProgress("Updating Database", "Creating database backup...");
+ UpdateProgress("Updating Database", "Creating database backup...");
- try
- {
- Directory.CreateDirectory("C:\\Backups");
- dbBackupFile = $"C:\\Backups\\{Path.GetRandomFileName()}.bak";
- LogManager.Log($"Creating database backup to '{dbBackupFile}'...");
- await Task.Factory.StartNew(() => db.Backup(localDataSource.Catalog, dbBackupFile));
- LogManager.Log("Database backup created successfully.");
- }
- catch (Exception ex)
- {
- throw LogManager.Log(ex, "Setup manager error while trying to create a database backup.");
- }
+ //Create Database Backup
+ try
+ {
+ Directory.CreateDirectory("C:\\Backups");
+ dbBackupFile = $"C:\\Backups\\{Path.GetRandomFileName()}.bak";
+ LogManager.Log($"Creating database backup to '{dbBackupFile}'...");
+ await Task.Factory.StartNew(() => db.Backup(localDataSource.Catalog, dbBackupFile));
+ performDatabaseRollback = true;
+ LogManager.Log("Database backup created successfully.");
}
+ catch (Exception ex)
+ {
+ throw LogManager.Log(ex, "Update manager error while trying to create a database backup.");
+ }
+
LogManager.Log("Disposing database manager.");
db.Dispose();
@@ -384,14 +413,12 @@ namespace Tango.PPC.Common.MachineUpdate
}
catch (Exception ex)
{
- throw LogManager.Log(ex, "Setup manager error while trying to synchronize database.");
+ throw LogManager.Log(ex, "Update manager error while trying to synchronize database.");
}
//Updating firmware
if (setupFirmware)
{
- performDatabaseRollback = true;
-
UpdateProgress("Updating Firmware", "Connecting to firmware device...");
LogManager.Log("");
LogManager.Log("-------------------------------------------------------------------------");
@@ -501,6 +528,9 @@ namespace Tango.PPC.Common.MachineUpdate
return Task.Factory.StartNew(() =>
{
UpdateDBResponse update_response = null;
+ var localDataSource = SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource;
+ bool performDatabaseRollback = false;
+ String dbBackupFile = null;
try
{
@@ -519,14 +549,42 @@ namespace Tango.PPC.Common.MachineUpdate
update_response = dbCompareResult.UpdateDBResponse;
- var localDataSource = SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource;
-
LogManager.Log($"Updating database '{update_response.DataSource.ToString()}' => '{localDataSource.ToString()}'...");
UpdateProgress("Updating Database", "Initializing update sequence...");
ExaminerSequenceConfiguration config_sequence = ExaminerSequenceConfiguration.FromFile(config_file);
+ UpdateProgress("Updating Database", "Connecting to local database...");
+ LogManager.Log("Initializing database manager...");
+ DbManager db = DbManager.FromDataSource(localDataSource);
+
+ LogManager.Log("Checking Tango database exists on the local machine...");
+ if (!db.Exists(localDataSource.Catalog))
+ {
+ throw new InvalidProgramException("Database tango does not exists.");
+ }
+
+ UpdateProgress("Updating Database", "Creating database backup...");
+
+ //Create Database Backup
+ try
+ {
+ Directory.CreateDirectory("C:\\Backups");
+ dbBackupFile = $"C:\\Backups\\{Path.GetRandomFileName()}.bak";
+ LogManager.Log($"Creating database backup to '{dbBackupFile}'...");
+ db.Backup(localDataSource.Catalog, dbBackupFile);
+ performDatabaseRollback = true;
+ LogManager.Log("Database backup created successfully.");
+ }
+ catch (Exception ex)
+ {
+ throw LogManager.Log(ex, "Update manager error while trying to create a database backup.");
+ }
+
+ LogManager.Log("Disposing database manager.");
+ db.Dispose();
+
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}...'");
@@ -565,7 +623,7 @@ namespace Tango.PPC.Common.MachineUpdate
}
catch (Exception ex)
{
- throw LogManager.Log(ex, "Setup manager error while trying to update the database.");
+ throw LogManager.Log(ex, "Upudate manager error while trying to update the database.");
}
}
@@ -575,8 +633,8 @@ namespace Tango.PPC.Common.MachineUpdate
}
catch (Exception ex)
{
- OnFailed(ex, update_response);
- throw;
+ OnFailed(ex, update_response, performDatabaseRollback, dbBackupFile, localDataSource);
+ throw ex;
}
});
}