/* This service should handle automatic updates and restart of application(s) in both application, service modes. * It should be able to detect all the installed Alchemi apps, and update them all. * It should be able to handle manual update mode, and by default do auto-update. * * In service mode, just restart the service(s) once the update download is done. * In app mode, * it should list the Alchemi processes, and restart them if they are just updated. * it should wait for process exit and restart it. * */ public UpdateService() { // This call is required by the Windows.Forms Component Designer. InitializeComponent(); configFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppStart.config"); config = ServiceUpdaterConfig.getUpdaterConfig(configFile); logger.Debug("Config file=" + configFile + ", AppFolderName=" + config.AppFolderName); //config.SaveConfig(configFile+".1"); currentServiceExePath = config.AppExePath; // // updater // updater = new AppUpdater(); updater.ShowDefaultUI = false; updater.UpdateUrl = config.UpdateURL; updater.ChangeDetectionMode = ChangeDetectionModes.ServerManifestCheck; updater.AutoFileLoad = false; if (updater.Downloader != null) { updater.Downloader.ValidateAssemblies = false; } if (updater.Poller != null) { updater.Poller.AutoStart = config.AutoUpdate; updater.Poller.DownloadOnDetection = true; updater.Poller.InitialPollInterval = config.UpdateInterval; updater.Poller.PollInterval = config.UpdateInterval; } else { logger.Debug("Poller is null"); } updater.OnUpdateDetected += new AppUpdater.UpdateDetectedEventHandler(this.updater_OnUpdateDetected); updater.OnUpdateComplete += new AppUpdater.UpdateCompleteEventHandler(this.updater_OnUpdateComplete); updater.Initialize(); config.SaveConfig(configFile); }
//************************************************************** // This event if fired whenever a new update is complete. // shutdown the app //************************************************************** public void updater_OnUpdateComplete(object sender, UpdateCompleteEventArgs e) { string serviceName = null; //If the udpate succeeded... if (e.UpdateSucceeded) { logger.Info("Update succeeded."); //need to restart the service. //for now, this works only for ONE service. //refresh the config. config = ServiceUpdaterConfig.getUpdaterConfig(configFile); if (config.ClientMode == UpdaterClientMode.Service) { if (config.ClientType == UpdaterClientType.Executor) { serviceName = "Alchemi Executor Service"; } else if (config.ClientType == UpdaterClientType.Manager) { serviceName = "Alchemi Manager Service"; } if (serviceName != null) { try { ServiceController sc = new ServiceController(serviceName); sc.Refresh(); if (sc.CanStop) { sc.Stop(); sc.WaitForStatus(ServiceControllerStatus.Stopped); } sc = null; logger.Info("Stopped service: " + serviceName); //need to uninstall the old one, install-&-start the new one. //uninstall here can only be done using the cmd-line since we dont have the ProjectInstaller object with us. //first uninstall the old one. Process pOld = new Process(); pOld.StartInfo = new ProcessStartInfo(currentServiceExePath, " /uninstall"); //using old service exe path pOld.Start(); logger.Info("Uninstall Process started : " + pOld.StartInfo.ToString()); pOld.WaitForExit(); pOld = null; Process pNew = new Process(); pNew.StartInfo = new ProcessStartInfo(config.AppExePath, " /install"); //using new service exe path pNew.Start(); logger.Info("Re-install Process started : " + pNew.StartInfo.ToString()); pNew.WaitForExit(); pNew = null; //now get the service controller again and try to start the service. sc = new ServiceController(serviceName); sc.Refresh(); sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running); sc = null; } catch (Exception ex) { handleUpdaterError("Error updating/restarting: " + serviceName, ex); } } } else { //currently we dont support updating an exec mode client. logger.Info("Updater service currently doesnt support App mode. It can update services only."); } } else //If the update failed.... { handleUpdaterError(e.ErrorMessage, e.FailureException); } }