Exemple #1
0
        public async Task <IActionResult> LogFileStatus()
        {
            var filterModel = new FileMonitorFilterModel().ApplyAuth(User);

            var logFileMonitorStateDtos = await _logFileMonitorStateRepository.GetLogFileMonitorStates(filterModel);

            return(View(logFileMonitorStateDtos));
        }
        // ReSharper disable once UnusedMember.Global
        public async Task RunSyncLogFileMonitorState([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogDebug($"Start RunSyncLogFileMonitorState @ {DateTime.UtcNow}");

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            var filterModel  = new FileMonitorFilterModel();
            var fileMonitors = await _fileMonitorsRepository.GetFileMonitors(filterModel);

            var fileMonitorStates = await _logFileMonitorStateRepository.GetLogFileMonitorStates(filterModel);

            var gameServerStatus = await _gameServerStatusRepository.GetAllStatusModels(new GameServerStatusFilterModel(), TimeSpan.Zero);

            foreach (var fileMonitorDto in fileMonitors)
            {
                var fileMonitorState = fileMonitorStates.SingleOrDefault(fm => fm.FileMonitorId == fileMonitorDto.FileMonitorId);
                var statusModel      = gameServerStatus.SingleOrDefault(s => s.ServerId == fileMonitorDto.ServerId);

                var playerCount = 0;
                if (statusModel != null)
                {
                    playerCount = statusModel.PlayerCount;
                }

                if (fileMonitorState == null)
                {
                    await _logFileMonitorStateRepository.UpdateState(new LogFileMonitorStateDto
                    {
                        FileMonitorId   = fileMonitorDto.FileMonitorId,
                        ServerId        = fileMonitorDto.ServerId,
                        GameType        = fileMonitorDto.GameServer.GameType,
                        ServerTitle     = fileMonitorDto.GameServer.Title,
                        FilePath        = fileMonitorDto.FilePath,
                        FtpHostname     = fileMonitorDto.GameServer.FtpHostname,
                        FtpUsername     = fileMonitorDto.GameServer.FtpUsername,
                        FtpPassword     = fileMonitorDto.GameServer.FtpPassword,
                        RemoteSize      = -1,
                        LastReadAttempt = DateTime.UtcNow,
                        LastRead        = DateTime.UtcNow,
                        PlayerCount     = playerCount
                    });

                    log.LogInformation($"Creating new log file monitor state object for {fileMonitorDto.GameServer.Title} against path {fileMonitorDto.FilePath}");
                }
                else
                {
                    fileMonitorState.ServerTitle = fileMonitorDto.GameServer.Title;

                    if (fileMonitorState.FilePath != fileMonitorDto.FilePath)
                    {
                        fileMonitorState.FilePath        = fileMonitorDto.FilePath;
                        fileMonitorState.RemoteSize      = -1;
                        fileMonitorState.LastReadAttempt = DateTime.UtcNow;
                        fileMonitorState.LastRead        = DateTime.UtcNow;
                    }

                    if (fileMonitorState.FtpHostname != fileMonitorDto.GameServer.FtpHostname ||
                        fileMonitorState.FtpUsername != fileMonitorDto.GameServer.FtpUsername ||
                        fileMonitorState.FtpPassword != fileMonitorDto.GameServer.FtpPassword)
                    {
                        fileMonitorState.FtpHostname = fileMonitorDto.GameServer.FtpHostname;
                        fileMonitorState.FtpUsername = fileMonitorDto.GameServer.FtpUsername;
                        fileMonitorState.FtpPassword = fileMonitorDto.GameServer.FtpPassword;

                        if (FtpClients.ContainsKey(fileMonitorState.ServerId))
                        {
                            FtpClients.TryRemove(fileMonitorState.ServerId, out var ftpClient);
                            ftpClient?.Dispose();
                        }
                    }

                    fileMonitorState.PlayerCount = playerCount;

                    await _logFileMonitorStateRepository.UpdateState(fileMonitorState);
                }
            }

            foreach (var fileMonitorState in fileMonitorStates)
            {
                var fileMonitor = fileMonitors.SingleOrDefault(fm => fm.FileMonitorId == fileMonitorState.FileMonitorId);

                if (fileMonitor == null)
                {
                    log.LogInformation($"Removing file monitor state object as file monitor no longer exists for {fileMonitorState.ServerTitle} against path {fileMonitorState.FilePath}");
                    await _logFileMonitorStateRepository.DeleteLogFileMonitorState(fileMonitorState);
                }
            }

            stopWatch.Stop();
            log.LogDebug($"Stop RunSyncLogFileMonitorState @ {DateTime.UtcNow} after {stopWatch.ElapsedMilliseconds} milliseconds");
        }