Пример #1
0
        public async Task Step()
        {
            var path = _options.ProcessInfo.FullPath;

            //var processWasRunning = false;

            if (State == ProcessWatchdogStates.Initing && !_filesystem.FileExists(path))
            {
                _logger.LogCritical($"Could not find file '{path}'");
                State = ProcessWatchdogStates.FileNotFound;
            }
            else if (State == ProcessWatchdogStates.FileNotFound)
            {
                _failCounter++;
                await _time.Delay(_options.RestartInterval);

                if (_filesystem.FileExists(path))
                {
                    State = ProcessWatchdogStates.Initing;
                }
            }
            else
            {
                //check if its already running
                var processes = GetProcesses();

                if (processes.Count == 0)
                {
                    if (State == ProcessWatchdogStates.Running)
                    {
                        _failCounter++;
                        _logger.LogError($"Process unexpectedly quit '{path}'.");
                        //await _time.Delay(_options.RestartInterval);
                        State = ProcessWatchdogStates.Initing;
                    }
                    else
                    {
                        if (State == ProcessWatchdogStates.Starting)
                        {
                            _failCounter++;
                            _logger.LogError($"Failed to start '{path}', trying again.");
                            //await _time.Delay(_options.RestartInterval);
                        }

                        State = ProcessWatchdogStates.Starting;

                        //needs started
                        _logger.LogInformation($"Starting process '{path}'");
                        _process.Run(_options.ProcessInfo);
                        await _time.Delay(_options.WarmupInterval);
                    }
                }
                else if (processes.Count == 1)
                {
                    //already running, monitor
                    if (State != ProcessWatchdogStates.Running)
                    {
                        _logger.LogInformation($"Found instance of '{path}'.");
                    }

                    _failCounter = 0;
                    State        = ProcessWatchdogStates.Running;
                    await _time.Delay(_options.CheckInterval);
                }
                else
                {
                    if (State != ProcessWatchdogStates.MultipleProcesses)
                    {
                        //more than 1 running ?!?!
                        _logger.LogCritical($"More than one instance of '{path}', waiting for exit.");
                        //if (!await StopAll())
                        _failCounter++;
                    }

                    State = ProcessWatchdogStates.MultipleProcesses;
                    await _time.Delay(_options.RestartInterval);
                }
            }
        }