private Task StartComponent(ComponentInfo componentInfo, Type type)
        {
            _taskExecutorService.Enqueue(async() =>
            {
                var sw = new Stopwatch();
                sw.Start();

                var runningComponent = new RunningComponentInfo
                {
                    Id          = componentInfo.Id,
                    Name        = componentInfo.Name,
                    Version     = componentInfo.Version,
                    Description = componentInfo.Description,
                    Status      = ComponentStatusEnum.STOPPED
                };
                try
                {
                    _logger.LogInformation($"Initialize component {componentInfo.Name} v{componentInfo.Version}");
                    var obj  = _servicesManager.Resolve(type) as IComponent;
                    var attr = type.GetCustomAttribute <ComponentAttribute>();


                    RunningComponents.Add(runningComponent);
                    runningComponent.Component = obj;
                    var componentConfig        = (IComponentConfig)LoadComponentConfig(attr.ComponentConfigType);


                    if (componentConfig != null)
                    {
                        await obj.InitConfiguration(componentConfig);
                    }
                    else
                    {
                        _logger.LogWarning($"Component {obj.GetType().Name} don't have configuration");
                        componentConfig = (IComponentConfig)obj.GetDefaultConfig();
                        SaveComponentConfig(componentConfig, attr.ComponentConfigType);
                        await obj.InitConfiguration(componentConfig);
                    }

                    if (componentConfig != null && componentConfig.Enabled)
                    {
                        await obj.Start();
                        runningComponent.Status = ComponentStatusEnum.STARTED;
                    }

                    sw.Stop();

                    _logger.LogInformation($"Component {componentInfo.Name} loaded id {sw.Elapsed}");
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Error during start component {componentInfo.Name} => {ex.Message}");
                    runningComponent.Error  = ex;
                    runningComponent.Status = ComponentStatusEnum.ERROR;
                }
            });


            return(Task.CompletedTask);
        }
Example #2
0
        public Task <bool> Start()
        {
            _ioTService.GetEventStream <IIotEntity>().Subscribe(entity =>
            {
                _taskExecutorService.Enqueue(() =>
                {
                    ParseRule(entity);
                    return(Task.CompletedTask);
                });
            });

            return(Task.FromResult(true));
        }