/// <summary> /// Stops all running service apps. /// </summary> /// <param name="dao">The ServiceApp DAO</param> /// <param name="isExiting">Set to <c>true</c> if the service is in the middle of exiting.</param> public void StopAllServiceApps(IServiceAppDao dao, bool isExiting) { foreach (var dom in this.ServiceAppProcesses.Where(d => d.IsProcessRunning)) { this.StopServiceApp(dom.ServiceApp.Name, dao, isExiting); } }
/// <summary> /// Removes the specified service app from the container. /// </summary> /// <param name="appName">Name of the application.</param> /// <param name="dao">The DAO.</param> /// <param name="appListDao">The application list DAO.</param> /// <exception cref="System.IndexOutOfRangeException">The app name could not be found.</exception> /// <exception cref="System.InvalidOperationException">The ServiceApp was not stopped first.</exception> public void RemoveServiceApp(string appName, IServiceAppDao dao, IAppListDao appListDao) { ServiceAppProcess process = ServiceAppProcesses[appName]; if (process != null) { if (process.IsProcessRunning) { throw new InvalidOperationException(string.Format("ServiceApp '{0}' must be stopped before it can be updated or removed.", appName)); } ServiceAppProcesses.Remove(process); // This will prevent leaks process.Started -= ServiceAppProcess_Started; process.Error -= ServiceAppProcess_Error; process.Executed -= ServiceAppProcess_Executed; process.Performance -= ServiceAppProcess_Performance; process.Stopped -= ServiceAppProcess_Stopped; } if (appListDao != null) { appListDao.DeleteServiceApp(appName); } if (dao != null) { dao.DeleteServiceApp(appName); } }
/// <summary> /// Loads the service app which should be added to the pool of service apps. /// </summary> /// <param name="appName">Name of the application.</param> /// <param name="dao">The DAO.</param> /// <returns>A string containing an error message if a recoverable error occured.</returns> /// <exception cref="System.IndexOutOfRangeException">The ServiceApp could not be found in the container.</exception> /// <exception cref="System.InvalidOperationException">The ServiceApp is not in a stopped state.</exception> public string InitializeServiceApp(string appName, IServiceAppDao dao) { ServiceAppProcess process = this.ServiceAppProcesses[appName]; if (process == null) { var e = new IndexOutOfRangeException(string.Format("ServiceApp '{0}' could not be found to initialize.", appName)); this._log.Error("Error in InitializeServiceApp", e); throw e; } else if (process.IsProcessRunning) { var e = new InvalidOperationException(string.Format("ServiceApp '{0}' must be stopped before it can be reinitialized.", appName)); this._log.Error("Error in InitializeServiceApp", e); throw e; } if (process.ServiceApp.StartupTypeEnum != StartupType.Disabled) { process.Start(); return(string.Empty); } else { return(string.Format("ServiceApp '{0}' is disabled", appName)); } }
/// <summary> /// Stops the service apps. /// </summary> private void StopServiceApps() { using (IServiceAppDao serviceAppDao = DaoFactory.Create <IServiceAppDao, ServiceAppDao>()) { _appManager.StopAllServiceApps(serviceAppDao, true); } }
/// <summary> /// Stops the specified service app. /// </summary> /// <param name="appName">Name of the application.</param> /// <param name="dao">The DAO.</param> /// <param name="isExiting">Set to <c>true</c> if the service is in the middle of exiting.</param> /// <exception cref="System.IndexOutOfRangeException">The app name could not be found.</exception> public void StopServiceApp(string appName, IServiceAppDao dao, bool isExiting) { ServiceAppProcess process = this.ServiceAppProcesses[appName]; if (process == null) { var e = new IndexOutOfRangeException(string.Format("appName '{0}' could not be found to stop.", appName)); this._log.Error("Error in StopServiceApp", e); } else { process.Stop(isExiting); _schedulingService.RemoveJob(appName); dao.RecordServiceAppStop(appName); } }
/// <summary> /// Updates the service app in the container or adds a new one to it if it does not exist. /// </summary> /// <param name="serviceApp">The service application.</param> /// <param name="dao">The DAO.</param> /// <param name="appListDao">The application list DAO.</param> /// <returns> /// An error message if there was a problem updating the service app /// </returns> public string UpdateServiceApp(ServiceApp serviceApp, IServiceAppDao dao, IAppListDao appListDao) { // TODO: move validation outside of here (breaks SRP) ServiceAppValidator validator = new ServiceAppValidator(); if (serviceApp == null) { return("Cannot update null service app"); } if (!validator.ValidateServiceApp(serviceApp)) { return(string.Format("Service app to update is invalid. Reason: {0}", string.Join(", ", validator.ErrorMessages))); } return(this.SyncServiceApp(serviceApp, dao, appListDao)); }
/// <summary> /// Loads all the service apps and schedules them. /// </summary> /// <param name="appList">The application list.</param> /// <param name="dao">The DAO.</param> /// <param name="errorList">The error list.</param> public void InitializeAllServiceApps(IEnumerable <ServiceApp> appList, IServiceAppDao dao, IList <string> errorList) { foreach (ServiceApp app in appList) { string errorMessage = this.SyncServiceApp(app, dao, null); if (string.IsNullOrWhiteSpace(errorMessage)) { errorMessage = this.InitializeServiceApp(app.Name, dao); } if (!string.IsNullOrWhiteSpace(errorMessage)) { errorList.Add(errorMessage); } } }
/// <summary> /// Loads and schedules the service apps. /// </summary> private void StartServiceApps() { // load all the services into the container IList <string> errors = new List <string>(); // TODO: inject via abstract factory using (IServiceAppDao serviceAppDao = DaoFactory.Create <IServiceAppDao, ServiceAppDao>()) { IAppListDao appListDao = DaoFactory.Create <IAppListDao, AppListDao>(); this._appManager.InitializeAllServiceApps(appListDao.FindAll(), serviceAppDao, errors); } errors.ToList().ForEach(error => { this._log.Warn(error); }); }
/// <summary> /// Adds the service app to the container or updates it if it already exists and saves it to the AppList configuration. /// </summary> /// <param name="app">The application.</param> /// <param name="dao">The DAO.</param> /// <param name="appListDao">The application list DAO.</param> /// <exception cref="System.InvalidOperationException">The service app is added and is still running.</exception> public string SyncServiceApp(ServiceApp app, IServiceAppDao dao, IAppListDao appListDao) { string errorMessage = string.Empty; this.RemoveServiceApp(app.Name, dao); ServiceAppProcess process = _serviceAppFactory.CreateServiceAppProcess(app, this._log); try { this.ServiceAppProcesses.Add(process); process.EntropyValue = dao.SaveServiceApp(app); if (appListDao != null) { try { appListDao.PersistServiceApp(app); } catch (Exception e) { this._log.Error(string.Format("{0} could not be saved to AppList", app.Name), e); errorMessage = string.Format("ServiceApp '{0}' could not be saved to the configuration.", app.Name); } } process.Started += ServiceAppProcess_Started; process.Error += ServiceAppProcess_Error; process.Executed += ServiceAppProcess_Executed; process.Performance += ServiceAppProcess_Performance; process.Stopped += ServiceAppProcess_Stopped; } catch (ArgumentException) { errorMessage = string.Format("ServiceApp '{0}' is already added.", app.Name); } return(errorMessage); }
/// <summary> /// Removes the specified service app from the container without removing from the app List. /// </summary> /// <param name="appName">Name of the application.</param> /// <param name="dao">The DAO.</param> /// <exception cref="System.IndexOutOfRangeException">The app name could not be found.</exception> /// <exception cref="System.InvalidOperationException">The ServiceApp was not stopped first.</exception> public void RemoveServiceApp(string appName, IServiceAppDao dao) { this.RemoveServiceApp(appName, dao, null); }