From 00a74a9292a17efd4e6a07e229fa578af4766346 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 24 Dec 2024 18:44:52 +0100 Subject: [PATCH] Update step di backup/zip per install --- .../Compo/ApplicationCheck.razor.cs | 28 +- .../EgwControlCenter.App.csproj | 2 +- .../PublishProfiles/ClickOnceProfile.pubxml | 4 +- EgwControlCenter.Core/AppControlService.cs | 267 ++++++++++++------ 4 files changed, 188 insertions(+), 113 deletions(-) diff --git a/EgwControlCenter.App/Components/Compo/ApplicationCheck.razor.cs b/EgwControlCenter.App/Components/Compo/ApplicationCheck.razor.cs index b4b4a67..f42ff6d 100644 --- a/EgwControlCenter.App/Components/Compo/ApplicationCheck.razor.cs +++ b/EgwControlCenter.App/Components/Compo/ApplicationCheck.razor.cs @@ -56,6 +56,8 @@ namespace EgwControlCenter.App.Components.Compo protected async void DoUpdate(VersStatusDTO ReqApp) { isUpdating = true; + await InvokeAsync(StateHasChanged); + await Task.Delay(50); // verifico il TIPO di oggetto gestito x la procedura di update... switch (ReqApp.ApplicationType) { @@ -69,37 +71,17 @@ namespace EgwControlCenter.App.Components.Compo break; case CoreEnum.AppType.WinApp: // esegue update delle app Iob-* - await DoUpdateIobWin(ReqApp); + await ACService.DoUpdateIobWin(ReqApp); break; default: break; } isUpdating = false; await InvokeAsync(StateHasChanged); + await Task.Delay(50); } - // metodo update specifico x app IobWin - private async Task DoUpdateIobWin(VersStatusDTO reqApp) - { - // step 0: inserisce i file semaforo di segnalazione update in corso - - // step 1: backup configurazioni - - // step 2: kill processi - - // step 3: backup intera folder - - // step 4: download - - // step 5: unzip - - // step 6: restore conf - - // step 7: rimozione file lock semaforo - - - await Task.Delay(1000); - } + protected async Task ForceCheck() { diff --git a/EgwControlCenter.App/EgwControlCenter.App.csproj b/EgwControlCenter.App/EgwControlCenter.App.csproj index d7f0f5d..7013267 100644 --- a/EgwControlCenter.App/EgwControlCenter.App.csproj +++ b/EgwControlCenter.App/EgwControlCenter.App.csproj @@ -5,7 +5,7 @@ enable true enable - 1.1.2412.2411 + 1.1.2412.2418 Debug;Release;DEBUG_Local diff --git a/EgwControlCenter.App/Properties/PublishProfiles/ClickOnceProfile.pubxml b/EgwControlCenter.App/Properties/PublishProfiles/ClickOnceProfile.pubxml index a9d156e..9481e19 100644 --- a/EgwControlCenter.App/Properties/PublishProfiles/ClickOnceProfile.pubxml +++ b/EgwControlCenter.App/Properties/PublishProfiles/ClickOnceProfile.pubxml @@ -4,8 +4,8 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - 2411 - 1.1.2412.2411 + 2418 + 1.1.2412.2418 True Release True diff --git a/EgwControlCenter.Core/AppControlService.cs b/EgwControlCenter.Core/AppControlService.cs index e43d785..4dbe4e9 100644 --- a/EgwControlCenter.Core/AppControlService.cs +++ b/EgwControlCenter.Core/AppControlService.cs @@ -5,6 +5,8 @@ using Newtonsoft.Json; using NLog; using System; using System.Collections.Generic; +using System.Diagnostics; +using System.IO.Compression; using System.Linq; using System.Reflection; using System.Text; @@ -103,88 +105,6 @@ namespace EgwControlCenter.Core } } - /// - /// Directory di backup x IobWin - /// - private string backupDir = @"C:\Steamware\backup"; - - /// - /// File segnaposto x lock fasi di update - /// - private string updFilePath = @"c:\Steamware\IOB-MAN\update.run"; - - /// - /// metodo update specifico x app IobWin - /// - /// - /// - public async Task DoUpdateIobWin(VersStatusDTO reqApp) - { - // controllo di avere un file di riferimento... - string appDir = Path.GetDirectoryName(reqApp.LocalPath) ?? ""; - if (!string.IsNullOrEmpty(appDir)) - { - // step 0: inserisce i file semaforo di segnalazione update in corso - using (FileStream fs = File.Create(updFilePath)) - { - // File is created and access is released when the using block ends - } - - // step 1: backup configurazioni partendo da path applicazione... - string srcDir = Path.Combine(appDir, "DATA", "CONF"); - string destDir = Path.Combine(backupDir, reqApp.CodApp, "DATA", "CONF"); - // se MAN uso folder "accorciata" - if(reqApp.CodApp=="IOB-MAN") - { - srcDir = Path.Combine(appDir, "CONF"); - destDir = Path.Combine(backupDir, reqApp.CodApp, "CONF"); - } - CopyDirectory(srcDir, destDir); - - // step 2: kill processi - - // step 3: backup + zip intera folder - - // step 4: download - - // step 5: unzip - - // step 6: restore conf - - await Task.Delay(1000); - } - // step 7: rimozione file lock semaforo - File.Delete(updFilePath); - } - - /// - /// Helper copia directory + contenuto - /// - /// - /// - protected void CopyDirectory(string sourceDir, string destDir) - { - // Create the destination directory if it doesn't exist - if (!Directory.Exists(destDir)) - { - Directory.CreateDirectory(destDir); - } - - // Copy all files - foreach (var file in Directory.GetFiles(sourceDir)) - { - string destFile = Path.Combine(destDir, Path.GetFileName(file)); - File.Copy(file, destFile, true); - } - - // Copy all subdirectories - foreach (var dir in Directory.GetDirectories(sourceDir)) - { - string destSubDir = Path.Combine(destDir, Path.GetFileName(dir)); - CopyDirectory(dir, destSubDir); - } - } - public bool CloudCallActive { get => cloudCallActive; @@ -309,11 +229,6 @@ namespace EgwControlCenter.Core return fatto; } - /// - /// DataOra ultimo controllo effettuato - /// - private DateTime lastCheckDone = DateTime.Today.AddMonths(-1); - /// /// Effettua un controllo completo (loacele remoto) /// @@ -385,6 +300,80 @@ namespace EgwControlCenter.Core ReportConfigUpd(); } + /// + /// metodo update specifico x app IobWin + /// + /// + /// + public async Task DoUpdateIobWin(VersStatusDTO reqApp) + { + // controllo di avere un file di riferimento... + string appDir = Path.GetDirectoryName(reqApp.LocalPath) ?? ""; + if (!string.IsNullOrEmpty(appDir)) + { + // step 0: inserisce i file semaforo di segnalazione update in corso + using (FileStream fs = File.Create(updFilePath)) + { + // File is created and access is released when the using block ends + } + + // step 1: backup configurazioni partendo da path applicazione... + string srcDir = Path.Combine(appDir, "DATA", "CONF"); + string destDir = Path.Combine(backupDir, "Conf", reqApp.CodApp, "DATA", "CONF"); + // se MAN uso folder "accorciata" + if (reqApp.CodApp == "IOB-MAN") + { + srcDir = Path.Combine(appDir, "CONF"); + destDir = Path.Combine(backupDir, "Conf", reqApp.CodApp, "CONF"); + } + CopyDirectory(srcDir, destDir); + + // step 2: kill processi + ForceKillByName(reqApp.CodApp); + + // step 3: backup + zip intera folder applicativo + string tempZip = Path.Combine(tempDir, "FULL", reqApp.CodApp); + if (Directory.Exists(tempZip)) + { + Directory.Delete(tempZip, true); + } + Directory.CreateDirectory(tempZip); + CopyDirectory(appDir, tempZip); + + string zipPath = Path.Combine(backupDir, "Archive", reqApp.CodApp, $"{DateTime.Now:yyyyMMdd}_{DateTime.Now:HHmmss}.zip"); + if (File.Exists(zipPath)) + { + File.Delete(zipPath); + } + else + { + string zipDir = Path.GetDirectoryName(zipPath) ?? ""; + if (!string.IsNullOrEmpty(zipDir)) + { + if (!Directory.Exists(zipDir)) + { + Directory.CreateDirectory(zipDir); + } + } + } + await Task.Run(() => + { + ZipFile.CreateFromDirectory(tempZip, zipPath); + }); + + // step 4: download + + // step 5: unzip + overwrite + + //// step 6: restore conf + //CopyDirectory(destDir, srcDir); + + } + // step 7: rimozione file lock semaforo + File.Delete(updFilePath); + await Task.Delay(10); + } + public void ResetConf() { bool fatto = CurrCheck.ResetConf(); @@ -421,6 +410,38 @@ namespace EgwControlCenter.Core #endregion Protected Properties + #region Protected Methods + + /// + /// Helper copia directory + contenuto + /// + /// + /// + protected void CopyDirectory(string sourceDir, string destDir) + { + // Create the destination directory if it doesn't exist + if (!Directory.Exists(destDir)) + { + Directory.CreateDirectory(destDir); + } + + // Copy all files + foreach (var file in Directory.GetFiles(sourceDir)) + { + string destFile = Path.Combine(destDir, Path.GetFileName(file)); + File.Copy(file, destFile, true); + } + + // Copy all subdirectories + foreach (var dir in Directory.GetDirectories(sourceDir)) + { + string destSubDir = Path.Combine(destDir, Path.GetFileName(dir)); + CopyDirectory(dir, destSubDir); + } + } + + #endregion Protected Methods + #region Private Fields /// @@ -428,6 +449,21 @@ namespace EgwControlCenter.Core /// private static Logger Log = LogManager.GetCurrentClassLogger(); + /// + /// Directory di backup x IobWin + /// + private string backupDir = @"C:\Steamware\backup"; + + /// + /// Directory temp di appoggio + /// + private string tempDir = @"C:\Steamware\temp"; + + /// + /// DataOra ultimo controllo effettuato + /// + private DateTime lastCheckDone = DateTime.Today.AddMonths(-1); + /// /// Valore massimo ammesso refresh sec /// @@ -438,6 +474,11 @@ namespace EgwControlCenter.Core /// private int refPMin = 1; + /// + /// File segnaposto x lock fasi di update + /// + private string updFilePath = @"c:\Steamware\IOB-MAN\update.run"; + /// /// Valore massimo ammesso veto minuti /// @@ -448,19 +489,71 @@ namespace EgwControlCenter.Core /// private int vetoPMin = 30; + /// + /// Ms di attesa x uscita processo (std) + /// + private int waitForExitMsec = 250; + #endregion Private Fields #region Private Properties private bool cloudCallActive { get; set; } = false; + private string ConfDir { get; set; } = ""; + private ReleaseChecker CurrCheck { get; set; } = null!; + private string DataDir { get; set; } = ""; #endregion Private Properties #region Private Methods + /// + /// Effettua un force kill dato nome processo + /// + /// + private void ForceKillByName(string nomeProc) + { + Process[] stillRunningProc = Process.GetProcessesByName(nomeProc); + if (stillRunningProc != null) + { + if (stillRunningProc.Length > 0) + { + foreach (var item in stillRunningProc) + { + try + { + Process p = Process.GetProcessById(item.Id); + { + if (!p.HasExited) + { + p.Kill(); + p.WaitForExit(waitForExitMsec); + } + if (!p.HasExited) + { + Log.Error($"Process not Killed, 2nd try p.kill()"); + p.Kill(); + p.WaitForExit(waitForExitMsec * 2); + } + if (!p.HasExited) + { + Log.Error($"Process not Killed, 3th try p.kill()"); + p.Kill(); + } + } + } + catch (Exception exc) + { + Log.Error($"Errore in fase di kill processo da nome {exc}"); + } + } + } + } + } + private void ReportConfigUpd() { if (EA_ConfigUpdated != null)