コード例 #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                _logger.LogInformation($"Starting NeDaemon service (version {Version})...");

                if (_netDaemonSettings == null)
                {
                    _logger.LogError("No configuration specified, in appsettings or environment variables! Exiting...");
                    return;
                }

                _sourcePath = _netDaemonSettings.GetAppSourceDirectory();

                _logger.LogTrace("Finding apps in {Folder}...", _sourcePath);

                // Automatically create source directories
                if (string.IsNullOrEmpty(_sourcePath))
                {
                    _logger.LogError("Path to folder cannot be null, exiting...", _sourcePath);
                    return;
                }
                if (!Directory.Exists(_sourcePath))
                {
                    _logger.LogError("Path to app source does not exist {Path}, exiting...", _sourcePath);
                    return;
                }

                _loadedDaemonApps = null;

                await using var daemonHost = _serviceProvider.GetService <NetDaemonHost>()
                                             ?? throw new NetDaemonException("Failed to get service for NetDaemonHost");
                {
                    await Run(daemonHost, stoppingToken).ConfigureAwait(false);
                }
            }
            catch (OperationCanceledException)
            {
            } // Normal exit
            catch (Exception e)
            {
                _logger.LogError(e, "NetDaemon had unhandled exception, closing...");
            }

            _logger.LogInformation("NetDaemon service exited!");
        }
コード例 #2
0
ファイル: RunnerService.cs プロジェクト: jpgdev/netdaemon
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                if (_netDaemonSettings == null)
                {
                    _logger.LogError("No config specified, file or environment variables! Exiting...");
                    return;
                }

                _sourcePath = _netDaemonSettings.GetAppSourceDirectory();

                // EnsureApplicationDirectoryExists(_netDaemonSettings);


                _logger.LogDebug("Finding apps in {folder}...", _sourcePath);

                // Automatically create source directories
                if (_sourcePath is string && !Directory.Exists(_sourcePath))
                {
                    throw new FileNotFoundException("Source path {path} does not exist", _sourcePath);
                }

                var hasConnectedBefore = false;

                _loadedDaemonApps = null;

                await using var daemonHost = _serviceProvider.GetService <NetDaemonHost>()
                                             ?? throw new ApplicationException("Failed to get service for NetDaemonHost");

                while (!stoppingToken.IsCancellationRequested)
                {
                    try
                    {
                        if (hasConnectedBefore)
                        {
                            // This is due to re-connect, it must be a re-connect
                            // so delay before retry connect again
                            await Task.Delay(ReconnectInterval, stoppingToken).ConfigureAwait(false); // Wait x seconds

                            _logger.LogInformation($"Restarting NeDaemon (version {Version})...");
                        }

                        var daemonHostTask = daemonHost.Run(
                            _homeAssistantSettings.Host,
                            _homeAssistantSettings.Port,
                            _homeAssistantSettings.Ssl,
                            _homeAssistantSettings.Token,
                            stoppingToken
                            );

                        if (await WaitForDaemonToConnect(daemonHost, stoppingToken).ConfigureAwait(false) == false)
                        {
                            continue;
                        }

                        if (!stoppingToken.IsCancellationRequested)
                        {
                            if (daemonHost.Connected)
                            {
                                try
                                {
                                    // Generate code if requested
                                    if (_sourcePath is string)
                                    {
                                        await GenerateEntities(daemonHost, _sourcePath);
                                    }

                                    if (_loadedDaemonApps is null)
                                    {
                                        _loadedDaemonApps = _daemonAppCompiler.GetApps();
                                    }

                                    if (_loadedDaemonApps is null || !_loadedDaemonApps.Any())
                                    {
                                        _logger.LogError("No apps found, exiting...");
                                        return;
                                    }

                                    IInstanceDaemonApp?codeManager = new CodeManager(_loadedDaemonApps, _logger, _yamlConfig);
                                    await daemonHost.Initialize(codeManager).ConfigureAwait(false);

                                    // Wait until daemon stops
                                    await daemonHostTask.ConfigureAwait(false);

                                    if (!stoppingToken.IsCancellationRequested)
                                    {
                                        // It is disconnected, wait
                                        // _logger.LogWarning($"Home assistant is unavailable, retrying in {ReconnectInterval / 1000} seconds...");
                                    }
                                }
                                catch (TaskCanceledException)
                                {
                                    _logger.LogInformation("Canceling NetDaemon service...");
                                }
                                catch (Exception e)
                                {
                                    _logger.LogError(e, "Failed to load applications");
                                }
                            }
                            else
                            {
                                _logger.LogWarning($"Home Assistant Core still unavailable, retrying in {ReconnectInterval / 1000} seconds...");
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        if (!stoppingToken.IsCancellationRequested)
                        {
                            _logger.LogWarning($"Home assistant is disconnected, retrying in {ReconnectInterval / 1000} seconds...");
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, "MAJOR ERROR!");
                    }
                    finally
                    {
                        await daemonHost.Stop().ConfigureAwait(false);
                    }

                    // If we reached here it could be a re-connect
                    hasConnectedBefore = true;
                }
            }
            catch (OperationCanceledException)
            {
            } // Normal exit
            catch (Exception e)
            {
                _logger.LogError(e, "NetDaemon had unhandled exception, closing...");
            }

            _logger.LogInformation("NetDaemon exited!");
        }