コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UpdaterService"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="twitchMessageService">The twitch message service.</param>
        /// <param name="discordMessageService">The discord message service.</param>
        /// <param name="steamCmdService">The steam command service.</param>
        /// <param name="messageTemplates">The message templates.</param>
        /// <param name="autorestartService">The autorestart service.</param>
        /// <param name="rconMessageService">The rcon message service.</param>
        public UpdaterService(
            ILogger <UpdaterService> logger,
            IOptionsSnapshot <Settings> settings,
            ITwitchMessageService twitchMessageService,
            IDiscordMessageService discordMessageService,
            ISteamCmdService steamCmdService,
            IOptionsSnapshot <Messages> messageTemplates,
            IAutoRestartServerService autorestartService,
            IRconMessageService rconMessageService)
        {
            _logger                = logger;
            _settings              = settings.Value;
            _autorestartService    = autorestartService;
            _twitchMessageService  = twitchMessageService;
            _discordMessageService = discordMessageService;
            _rconMessageService    = rconMessageService;
            _steamCmdService       = steamCmdService;
            _twitchMessages        = messageTemplates.Value.Twitch;
            _discordMessages       = messageTemplates.Value.Discord;
            _rconMessages          = messageTemplates.Value.RCON;
            _logger.LogInformation("Updater Service has Started");

            if (_settings.Update.ShouldInstallSteamCmdIfMissing)
            {
                InstallSteamCMD();
            }

            if (_settings.Update.ShouldInstallAtlasServerIfMissing)
            {
                Process process = Process.GetProcesses().FirstOrDefault(c => c.ProcessName.Contains(_settings.Atlas.ServerProcessName));
                if (process is null)
                {
                    _steamCmdService.InstallAndUpdateAtlasServer();
                }
            }


            _updateTimer = new System.Timers.Timer(_settings.Update.UpdateCheckInterval * 1000 * 60)
            {
                AutoReset = true,
                Enabled   = false
            };

            if (_settings.General.ShouldRestartAtlasOnNotRunning)
            {
                SetupServerProcessMonitor();
            }

            if (_settings.General.RestartServerAfterHours != 0)
            {
                _autorestartService.StartTimer();
            }
        }
コード例 #2
0
        /// <summary>
        /// Timers the elapsed.
        /// </summary>
        /// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param>
        /// <returns>Task.</returns>
        private async Task _timer_Elapsed(System.Timers.ElapsedEventArgs e)
        {
            _checkGameRunningTimer?.Dispose();

            if (!_steamCmdService.IsSteamCmdInstalled())
            {
                _logger.LogError("Could not find SteamCMD - we need it to perform the update. Aborting");
                return;
            }

            _logger.LogInformation("Performing Update Check");

            (bool Result, string Version)updateCheck = await _steamCmdService.DetectUpdate();

            if (updateCheck.Result)
            {
                if (_settings.Update.AnnounceTwitch)
                {
                    string twitchMessage = _twitchMessages.TwitchUpdateMessage.Replace("@version", $"{updateCheck.Version}").Replace(AnnounceBefore, $"{_settings.Update.AnnounceMinutesBefore}{MinutesPluralisation()}");

                    _twitchMessageService.SendMessage(twitchMessage);
                }

                if (_settings.Update.AnnounceTwitch)
                {
                    string discordMessage = _discordMessages.DiscordUpdateMessage.Replace("@version", $"{updateCheck.Version}").Replace(AnnounceBefore, $"{_settings.Update.AnnounceMinutesBefore}{MinutesPluralisation()}");

                    await _discordMessageService.SendMessage(discordMessage);
                }

                if (_settings.Update.AnnounceRCon)
                {
                    string rconMessage = _rconMessages.RconUpdateMessage.Replace("@version", $"{updateCheck.Version}").Replace(AnnounceBefore, $"{_settings.Update.AnnounceMinutesBefore}{MinutesPluralisation()}");

                    await _rconMessageService.SendMessage(rconMessage);
                }

                await Task.Delay(TimeSpan.FromMinutes(_settings.Update.AnnounceMinutesBefore));

                _logger.LogInformation("Updating...");
                bool updateResult = await _steamCmdService.KillAtlas();

                if (!updateResult)
                {
                    _logger.LogInformation("Update process has failed to stop the running server.");
                    return;
                }

                _steamCmdService.InstallAndUpdateAtlasServer();

                bool result = _steamCmdService.StartAtlasServer();
                if (result)
                {
                    if (_settings.Update.AnnounceTwitch)
                    {
                        _twitchMessageService.SendMessage(_twitchMessages.TwitchServerRestartingMessage);
                    }

                    if (_settings.Update.AnnounceTwitch)
                    {
                        await _discordMessageService.SendMessage(_discordMessages.DiscordServerRestartingMessage);
                    }
                }

                _logger.LogInformation("Server Has Started Back Up.");
            }

            if (_settings.General.ShouldRestartAtlasOnNotRunning)
            {
                SetupServerProcessMonitor();
            }
        }