protected override void OnStart(string[] args) { try { AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += UnhandledExceptionEventHandler; string info = string.Format(Emailer.startedMessageTempl, Environment.MachineName, DateTime.Now); log.Info(info); InitializeDivaInstance(); InitializeSettings(GetSettingsFromDatabase()); ServiceDAO.ResetIlcState(); ExecutorFactory.Instance.ConstructExecutor(ServiceDAO.GetScenarios()); projectIdsByCronExpressions = GetProjectIdsByCronExpressions(); InitializeSchedulerAsync(); settingsReloadTimer = new Timer(CheckSettingsVersionAndReloadIfNeeded, null, TIMER_DUE, TIMER_PERIOD); fileWatcher = new FileSystemWatcher(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)); fileWatcher.NotifyFilter = NotifyFilters.LastWrite; fileWatcher.Filter = Process.GetCurrentProcess().MainModule.ModuleName + ".config"; fileWatcher.EnableRaisingEvents = true; fileWatcher.Changed += OnConfigFileChanged; if (Emailer.SendEmail("", info, AppSettings.GetCommonRecipientEmailAddresses())) { scheduler.Start(); // Normal exit return; } log.Error("Failed to send email. Service will be stopped."); } catch (CompileErrorException e) { log.Error("Script compiler error."); Emailer.SendEmail(e.Message, "Script compiler error.", AppSettings.GetCommonRecipientEmailAddresses(), e.FileName); } catch (Exception e) { String failureMessage = e.Message + "\n" + e.StackTrace; log.Error("OnStart()", e); // Error handling Emailer.SendEmail(failureMessage, Emailer.generalFailureSubject, AppSettings.GetCommonRecipientEmailAddresses()); } Stop(); }
protected override void OnStop() { // any tear-down necessary to stop your service. LineCheckerJob.serviceShutdown = true; if (scheduler != null) { scheduler.Shutdown(true); } // TODO string info = string.Format(Emailer.stoppedMessageTempl, Environment.MachineName, DateTime.Now); log.Info(info); Emailer.SendEmail("", info, AppSettings.GetCommonRecipientEmailAddresses()); }
/// <summary> /// changes settings on the fly if needed /// </summary> /// <param name="stateInfo">parameter required by callback signature</param> public void CheckSettingsVersionAndReloadIfNeeded(Object stateInfo) { log.Info("CheckSettingsVersionAndReloadIfNeeded()..."); try { // To avoid Timer Event Reentrance settingsReloadTimer.Change(Timeout.Infinite, Timeout.Infinite); if (!IsReloadNeeded()) { return; } // restart quartz scheduler.Shutdown(true); IDictionary settings = null; try { settings = GetSettingsFromDatabase(); projectIdsByCronExpressions = GetProjectIdsByCronExpressions(); log.Info("Trying to apply new settings. Version: " + settings[AppSettings.SettingsVersion]); } catch (Exception ex) { log.Info("Can't get service settings from database", ex); log.Info("Trying to restore previous settings. Version: " + AppSettings.GetSettingsVersion()); settings = AppSettings.AppSettingsMap; } configFileChanged = false; foreach (string s in new string[] { "appSettings", "connectionStrings", "log4net", "common/logging" }) { ConfigurationManager.RefreshSection(s); } LogManager.Adapter = null; log = LogManager.GetLogger(AppSettings.GetCommonLoggerName()); InitializeSettings(settings); InitializeSchedulerAsync(); scheduler.Start(); } catch (Exception e) { // we can't get settings String failureMessage = e.Message + "\n" + e.StackTrace; log.Error("CheckSettingsVersionAndReloadIfNeeded", e); Emailer.SendEmail(failureMessage, Emailer.generalFailureSubject, AppSettings.GetCommonRecipientEmailAddresses()); Stop(); } finally { settingsReloadTimer.Change(TIMER_DUE, TIMER_PERIOD); } }