From 334c0692074470fd6a500a8d93113e3684f3fc5b Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Sat, 2 Mar 2013 10:01:38 -0500 Subject: [PATCH 1/3] Improve waiting for things to end in (un)installer --- MediaBrowser.Installer/MainWindow.xaml.cs | 40 +++++++++++++++++-- .../MainWindow.xaml.cs | 32 ++++++++++++++- MediaBrowser.Uninstaller/Program.cs | 5 ++- 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/MediaBrowser.Installer/MainWindow.xaml.cs b/MediaBrowser.Installer/MainWindow.xaml.cs index 1ead1fced9..6445663251 100644 --- a/MediaBrowser.Installer/MainWindow.xaml.cs +++ b/MediaBrowser.Installer/MainWindow.xaml.cs @@ -80,6 +80,20 @@ namespace MediaBrowser.Installer PackageClass = (PackageVersionClass) Enum.Parse(typeof (PackageVersionClass), ConfigurationManager.AppSettings["class"] ?? "Release"); var cmdArgs = Environment.GetCommandLineArgs(); Archive = cmdArgs.Length > 1 ? cmdArgs[1] : null; + var callerId = cmdArgs.Length > 2 ? cmdArgs[2] : null; + if (callerId != null) + { + // Wait for our caller to exit + try + { + var process = Process.GetProcessById(Convert.ToInt32(callerId)); + process.WaitForExit(); + } + catch (ArgumentException) + { + // wasn't running + } + } switch (product.ToLower()) { @@ -108,13 +122,15 @@ namespace MediaBrowser.Installer /// protected async Task DoInstall(string archive) { - lblStatus.Text = string.Format("Downloading {0}...", FriendlyName); + lblStatus.Text = string.Format("Installing {0}...", FriendlyName); // Determine Package version var version = await GetPackageVersion(); // Now try and shut down the server if that is what we are installing and it is running - if (PackageName == "MBServer" && Process.GetProcessesByName("mediabrowser.serverapplication").Length != 0) + var procs = Process.GetProcessesByName("mediabrowser.serverapplication"); + var server = procs.Length > 0 ? procs[0] : null; + if (PackageName == "MBServer" && server != null) { lblStatus.Text = "Shutting Down Media Browser Server..."; using (var client = new WebClient()) @@ -122,6 +138,14 @@ namespace MediaBrowser.Installer try { client.UploadString("http://localhost:8096/mediabrowser/System/Shutdown", ""); + try + { + server.WaitForExit(); + } + catch (ArgumentException) + { + // already gone + } } catch (WebException e) { @@ -278,7 +302,17 @@ namespace MediaBrowser.Installer { // Delete old content of system var systemDir = Path.Combine(RootPath, "system"); - if (Directory.Exists(systemDir)) Directory.Delete(systemDir, true); + if (Directory.Exists(systemDir)) + { + try + { + Directory.Delete(systemDir, true); + } + catch + { + // we tried... + } + } // And extract using (var fileStream = File.OpenRead(archive)) diff --git a/MediaBrowser.Uninstaller.Execute/MainWindow.xaml.cs b/MediaBrowser.Uninstaller.Execute/MainWindow.xaml.cs index 934ff284bd..3064115e30 100644 --- a/MediaBrowser.Uninstaller.Execute/MainWindow.xaml.cs +++ b/MediaBrowser.Uninstaller.Execute/MainWindow.xaml.cs @@ -18,10 +18,28 @@ namespace MediaBrowser.Uninstaller.Execute public MainWindow() { - Thread.Sleep(800); // be sure our caller is shut down var args = Environment.GetCommandLineArgs(); var product = args.Length > 1 ? args[1] : "server"; + var callerId = args.Length > 2 ? args[2] : null; + if (callerId != null) + { + // Wait for our caller to exit + try + { + var process = Process.GetProcessById(Convert.ToInt32(callerId)); + process.WaitForExit(); + } + catch (ArgumentException) + { + // wasn't running + } + } + else + { + Thread.Sleep(1000); // crude method + } + InitializeComponent(); @@ -79,7 +97,9 @@ namespace MediaBrowser.Uninstaller.Execute if (Product == "Server") { RemoveShortcut(Path.Combine(startMenu, "MB Dashboard.lnk")); - if (Process.GetProcessesByName("mediabrowser.serverapplication").Length != 0) + var procs = Process.GetProcessesByName("mediabrowser.serverapplication"); + var server = procs.Length > 0 ? procs[0] : null; + if (server != null) { using (var client = new WebClient()) { @@ -87,6 +107,14 @@ namespace MediaBrowser.Uninstaller.Execute try { client.UploadString("http://localhost:8096/mediabrowser/system/shutdown", ""); + try + { + server.WaitForExit(); + } + catch (ArgumentException) + { + // already gone + } } catch (WebException ex) { diff --git a/MediaBrowser.Uninstaller/Program.cs b/MediaBrowser.Uninstaller/Program.cs index 8ea1a950aa..2947d1e15d 100644 --- a/MediaBrowser.Uninstaller/Program.cs +++ b/MediaBrowser.Uninstaller/Program.cs @@ -24,8 +24,11 @@ namespace MediaBrowser.Uninstaller var sourceDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? ""; File.WriteAllBytes(tempExe, File.ReadAllBytes(Path.Combine(sourceDir, "MediaBrowser.Uninstaller.Execute.exe"))); File.Copy(Path.Combine(sourceDir, "MediaBrowser.Uninstaller.Execute.exe.config"), tempConfig, true); + + //get our pid to pass to the uninstaller so it can wait for us to exit + var pid = Process.GetCurrentProcess().Id; //kick off the copy - Process.Start(tempExe, product); + Process.Start(tempExe, product + " " + pid); //and shut down } } From cb17eca3a4f2153289c5b18b7cc8d6c16770a3a0 Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Sat, 2 Mar 2013 11:10:06 -0500 Subject: [PATCH 2/3] Full cmdline support for installer --- MediaBrowser.Installer/MainWindow.xaml.cs | 22 +++++++++++++++---- .../MediaBrowser.Installer.csproj | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/MediaBrowser.Installer/MainWindow.xaml.cs b/MediaBrowser.Installer/MainWindow.xaml.cs index 6445663251..6f64ded86d 100644 --- a/MediaBrowser.Installer/MainWindow.xaml.cs +++ b/MediaBrowser.Installer/MainWindow.xaml.cs @@ -76,11 +76,25 @@ namespace MediaBrowser.Installer protected void GetArgs() { - var product = ConfigurationManager.AppSettings["product"] ?? "server"; - PackageClass = (PackageVersionClass) Enum.Parse(typeof (PackageVersionClass), ConfigurationManager.AppSettings["class"] ?? "Release"); + //cmd line args should be name/value pairs like: product=server archive="c:\.." caller=34552 var cmdArgs = Environment.GetCommandLineArgs(); - Archive = cmdArgs.Length > 1 ? cmdArgs[1] : null; - var callerId = cmdArgs.Length > 2 ? cmdArgs[2] : null; + var args = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var pair in cmdArgs) + { + var nameValue = pair.Split('='); + if (nameValue.Length == 2) + { + args[nameValue[0]] = nameValue[1]; + } + } + + Archive = args.GetValueOrDefault("archive", null); + + var product = args.GetValueOrDefault("product", null) ?? ConfigurationManager.AppSettings["product"] ?? "server"; + PackageClass = (PackageVersionClass) Enum.Parse(typeof (PackageVersionClass), args.GetValueOrDefault("class", null) ?? ConfigurationManager.AppSettings["class"] ?? "Release"); + PackageVersion = new Version(args.GetValueOrDefault("version", "4.0")); + + var callerId = args.GetValueOrDefault("caller", null); if (callerId != null) { // Wait for our caller to exit diff --git a/MediaBrowser.Installer/MediaBrowser.Installer.csproj b/MediaBrowser.Installer/MediaBrowser.Installer.csproj index 3a7d9102ee..2d95ae5ad1 100644 --- a/MediaBrowser.Installer/MediaBrowser.Installer.csproj +++ b/MediaBrowser.Installer/MediaBrowser.Installer.csproj @@ -29,7 +29,7 @@ Media Browser Team Media Browser false - 32 + 34 0.1.1.%2a false true From dd5bb6890eed6643cae15fac8b5adc1372baa04a Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Sat, 2 Mar 2013 12:41:00 -0500 Subject: [PATCH 3/3] Remove archive after extraction --- MediaBrowser.Installer/MainWindow.xaml.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/MediaBrowser.Installer/MainWindow.xaml.cs b/MediaBrowser.Installer/MainWindow.xaml.cs index 6f64ded86d..a85a0cb9a4 100644 --- a/MediaBrowser.Installer/MainWindow.xaml.cs +++ b/MediaBrowser.Installer/MainWindow.xaml.cs @@ -212,6 +212,19 @@ namespace MediaBrowser.Installer try { ExtractPackage(archive); + // We're done with it so delete it (this is necessary for update operations) + try + { + File.Delete(archive); + } + catch (FileNotFoundException) + { + } + catch (Exception e) + { + + SystemClose("Error Removing Archive - " + e.GetType().FullName + "\n\n" + e.Message); + } } catch (Exception e) {