コード例 #1
0
        public void Initialize(IHost host)
        {
            Host    = host;
            RConfig = host.LoadPluginConfiguration <RestartConfig>(this.GetType());

            bds           = (ProcessManager)host.GetPluginByName("ProcessManager");
            backupManager = (BackupManager)host.GetPluginByName("BackupManager");
            renderManager = (RenderManager)host.GetPluginByName("RenderManager");
            bdsWatchdog   = (Watchdog)host.GetPluginByName("Watchdog");

            // add console ignore patterns
            foreach (string s in RConfig.IgnorePatterns)
            {
                if (s != null)
                {
                    bds.AddIgnorePattern(s);
                }
            }

            DateTime restartTime = DateTime.Parse(RConfig.DailyRestartTime);

            if (restartTime < DateTime.Now || (!RConfig.TestingMode && restartTime.Subtract(DateTime.Now).TotalMinutes < 480))
            {
                restartTime = restartTime.AddDays(1);
            }
            double restartMins = restartTime.Subtract(DateTime.Now).TotalMinutes;

            firstRestartTimer           = new Timer(restartMins * 60000 + 1);
            firstRestartTimer.AutoReset = false;
            firstRestartTimer.Elapsed  += (object sender, ElapsedEventArgs e) =>
            {
                dailyRestartTimer.Start();
                TriggerRestart(sender, e);
            };
            firstRestartTimer.Start();

            dailyRestartTimer           = new Timer(86400000);
            dailyRestartTimer.AutoReset = true;
            dailyRestartTimer.Elapsed  += TriggerRestart;

            delayTimer           = new Timer(RConfig.WarningTime * 2 * 60000 + 1);
            delayTimer.AutoReset = false;
            delayTimer.Elapsed  += TriggerRestart;

            /* - enable notifications for console-scheduled shutdown: need an event to be implemented in base vellum
             * bds.OnShutdownScheduled += (object sender, ShutdownScheduledEventArgs e) =>
             * {
             *  // if someone already ran "stop ##" in the console, doing it again doesn't overwrite the previous timer
             *  // so if this has already happened, don't redo anything here
             *  if (!alreadyStopping)
             *  {
             *      StartNotifyTimers(e.Seconds);
             *      alreadyStopping = true;
             *  }
             * };
             */

            // set up shutdown messages
            StartNotifyTimers((uint)(restartMins * 60));
            Log(String.Format(RConfig.TextStrings.LogLoad, (uint)restartMins, RConfig.DailyRestartTime));

            // set up unexpected shutdown/crash handling
            bds.Process.Exited += (object sender, EventArgs e) =>
            {
                System.Threading.Thread.Sleep(1000); // give the watchdog a second to run the crashing hook if it's crashing
                if (!crashing && !restarting && !backupManager.Processing)
                {
                    Unload();
                }
            };
            ((IPlugin)bdsWatchdog).RegisterHook((byte)Watchdog.Hook.CRASH, (object sender, EventArgs e) => {
                crashing = true;
            });
            ((IPlugin)bdsWatchdog).RegisterHook((byte)Watchdog.Hook.LIMIT_REACHED, (object sender, EventArgs e) => {
                if (!restarting)
                {
                    Unload();
                }
            });
            ((IPlugin)bdsWatchdog).RegisterHook((byte)Watchdog.Hook.STABLE, (object sender, EventArgs e) => {
                crashing = false;
            });
        }