Ejemplo n.º 1
0
        /// <summary>
        /// When implemented in a derived class, executes when a Start command is sent to the
        /// service by the Service Control Manager (SCM) or when the operating system starts
        /// (for a service that starts automatically). Specifies actions to take when the service starts.
        /// </summary>
        /// <param name="args">Data passed by the start command.</param>
        protected override void OnStart(string[] args)
        {
            const int timeToWaitForTekkitToStartUp = 10000;

            try
            {
                EventLog.WriteEntry(this.ServiceName, "LAWST is responding to a Start event and will begin initializing the Tekkit Server.");

                if (_processTekkit == null)
                {
                    EventLog.WriteEntry(this.ServiceName, "LAWST detected that no Tekkit Server process exists, so it will create one now.");
                    _processTekkit = new TekkitServerProcess(this.ServiceName);
                }

                if (!_processTekkit.IsTekkitRunning)
                {
                    EventLog.WriteEntry(this.ServiceName, "LAWST will now begin to initiate the Tekkit Server.");

                    // start the Tekkit Server
                    _processTekkit.StartTheTekkitServer();

                    // wait for Tekkit to start up (usually 4 seconds...)
                    Thread.Sleep(timeToWaitForTekkitToStartUp);

                    _processTekkit.SendProcessText("say Server initialization complete...");
                    _processTekkit.SendProcessText("say Welcome to the Tekkit Server managed by LAWST!");

                    EventLog.WriteEntry(this.ServiceName, "LAWST successfully started the Tekkit Server.");

                    ExitCode = 0;
                }
                else
                {
                    EventLog.WriteEntry(this.ServiceName, "LAWST detected that the Tekkit Server was already running during the service's Start event.  This might mean the Tekkit Server is in an unstable state.  LAWST recommends stopping this service to shutdown the Tekkit Server, and then restart this service to ensure the Tekkit Server is in a proepr, stable state.  LAWST has started.");
                    ExitCode = -1;
                }
            }
            catch (Exception ex)
            {
                _processTekkit = null;
                EventLog.WriteEntry(this.ServiceName, string.Format("LAWST encountered the following exception the service's Start event:{0}{0}{1}", Environment.NewLine, ex.ToString()));
                ExitCode = -1;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// When implemented in a derived class, executes when a Stop command is sent to the service by the
        /// Service Control Manager (SCM). Specifies actions to take when a service stops running.
        /// </summary>
        protected override void OnStop()
        {
            if (_processTekkit != null)
            {
                try
                {
                    if (_processTekkit.IsTekkitRunning)
                    {
                        if (!_shutdownRequested)
                        {
                            // the Stop event was raised normally, not due to the server shutting down...
                            EventLog.WriteEntry(this.ServiceName, "LAWST is responding to a Stop event and will begin shutting down the Tekkit Server.");

                            int secondsToWaitForTekkitToStop = Properties.Settings.Default.SecondsToWaitForTekkitToStop;

                            // request additional time to stop
                            this.RequestAdditionalTime(secondsToWaitForTekkitToStop * 1000);

                            _processTekkit.SaveTekkitServerWorld();

                            _processTekkit.SendProcessText(string.Format("say LAWST has been asked to shutdown the Tekkit Server. It will be shutting down in {0} seconds.", secondsToWaitForTekkitToStop));

                            // wait for a quarter of the specified time
                            Thread.Sleep(secondsToWaitForTekkitToStop * 250);
                            _processTekkit.SendProcessText(string.Format("say Evacuate the Server immediately.  The Server will be shutting down in {0} seconds.", secondsToWaitForTekkitToStop * 3 / 4));

                            // wait for a quarter of the specified time
                            Thread.Sleep(secondsToWaitForTekkitToStop * 250);
                            _processTekkit.SendProcessText(string.Format("say Time is running out.  The Server will be shutting down in {0} seconds.", secondsToWaitForTekkitToStop / 2));

                            // wait for a quarter of the specified time
                            Thread.Sleep(secondsToWaitForTekkitToStop * 250);
                            _processTekkit.SendProcessText(string.Format("say This is your last chance to leave the Server before you will be booted.  The Server will be shutting down in {0} seconds.", secondsToWaitForTekkitToStop / 4));

                            // wait for a quarter of the specified time
                            Thread.Sleep(secondsToWaitForTekkitToStop * 250);

                            _processTekkit.StopTheTekkitServer();

                            _processTekkit = null;
                            EventLog.WriteEntry(this.ServiceName, "LAWST successfully stopped the Tekkit Server.");

                            ExitCode = 0;
                        }
                        else
                        {
                            // stop is being called due to the server shutting down,
                            // so we need to shutdown gracefully and quickly
                            _processTekkit.SendProcessText("say The Server is being forced to shutdown.  Evacuate the server imemdiately.");
                            _processTekkit.StopTheTekkitServer();
                            this.RequestAdditionalTime(5000);
                        }
                    }
                    else
                    {
                        EventLog.WriteEntry(this.ServiceName, "LAWST detected that the Tekkit Server was not running during the service's Stop event.  No action was taken and this service was stopped.");
                        ExitCode = -1;
                    }
                }
                catch (Exception ex)
                {
                    _processTekkit = null;
                    EventLog.WriteEntry(this.ServiceName, string.Format("LAWST encountered the following exception the service's Stop event:{0}{0}{1}", Environment.NewLine, ex.ToString()));
                    ExitCode = -100;
                }
            }
            else
            {
                EventLog.WriteEntry(this.ServiceName, "LAWST is responding to a Stop event but there is no Tekkit Server process, so there is nothing to do.");
            }
        }