public void Startup() { if (m_thread != null) { throw new Exception("Startup: Starting while already running"); } logging.EventLog elog = new ApplicationEventLog(); elog.LogInformation("Starting"); GlobalIsRunning.IsRunning = true; m_shutdown.Reset(); elog.LogInformation("Initializing database"); Stopwatch watch = Stopwatch.StartNew(); Database db = new Database(); new Initializer(null).Initialize(db); elog.LogInformation($"Database initialization took {watch.ElapsedMilliseconds} ms"); using (SQLiteConnection conn = db.Connection) { conn.Open(); db.Attribute attr = new db.Attribute(); attr.Set("service.startup_time", DateTimeOffset.Now.ToString("o"), conn); string assembly_ver = Assembly.GetExecutingAssembly().GetName().Version.ToString(); attr.Set("software.version", assembly_ver, conn); } elog.LogInformation("Setting up responders"); m_responders.ForEach(r => RequestBus.Instance.Subscribe(r)); elog.LogInformation("Starting web server"); watch.Restart(); m_host.Start(); elog.LogInformation($"Web server startup took {watch.ElapsedMilliseconds} ms"); elog.LogInformation("Starting work thread"); watch.Restart(); m_thread = new Thread(new ThreadStart(ThreadFunc)); m_thread.Start(); // Wait for the thread to start while (!m_thread.IsAlive) { Thread.Sleep(25); } elog.LogInformation($"Work thread startup took {watch.ElapsedMilliseconds} ms"); elog.LogInformation("Completed startup"); }
public void Shutdown() { logging.EventLog elog = new ApplicationEventLog(); elog.LogInformation("Stopping"); Stopwatch watch = Stopwatch.StartNew(); GlobalIsRunning.IsRunning = false; m_shutdown.Set(); while (m_thread != null && m_thread.ThreadState == System.Threading.ThreadState.Running) { Thread.Sleep(100); } elog.LogInformation($"Stopping worker thread took {watch.ElapsedMilliseconds} ms"); Database db = new Database(); using (SQLiteConnection conn = db.Connection) { conn.Open(); db.Attribute attr = new db.Attribute(); attr.Set("service.stop_time", DateTimeOffset.Now.ToString("o"), conn); } elog.LogInformation("Stopping web server"); m_host.Stop(); m_host.Dispose(); elog.LogInformation("Clearing responders"); m_responders.ForEach(r => RequestBus.Instance.Unsubscribe(r)); m_thread = null; elog.LogInformation("Completed stopping"); }