예제 #1
0
        public void TryInitializeService(string id, ServiceConfiguration configuration)
        {
            if (!configuration.IsEnabled)
            {
                _logger.Log(LogLevel.Information, $"Service '{id}' not initialized because it is disabled.");
                return;
            }

            try
            {
                var repositoryEntityUid = new RepositoryEntityUid {
                    Id = id, Version = configuration.Version
                };

                var serviceInstance = new ServiceInstance(_repositoryService, _pythonEngineService, _loggerFactory);
                serviceInstance.Initialize(repositoryEntityUid);

                if (configuration.Variables != null)
                {
                    foreach (var variable in configuration.Variables)
                    {
                        serviceInstance.SetVariable(variable.Key, variable.Value);
                    }
                }

                _logger.Log(LogLevel.Information, "Initializing service '{0}'.", id);
                serviceInstance.ExecuteFunction("initialize");

                lock (_serviceInstances)
                {
                    if (_serviceInstances.TryGetValue(id, out var existingServiceInstance))
                    {
                        _logger.Log(LogLevel.Information, "Stopping service '{0}'.", id);
                        existingServiceInstance.ExecuteFunction("stop");
                    }

                    _serviceInstances[id] = serviceInstance;

                    _logger.Log(LogLevel.Information, "Starting service '{0}'.", id);
                    serviceInstance.ExecuteFunction("start");
                }

                _logger.Log(LogLevel.Information, $"Service '{id}' started.");
            }
            catch (Exception exception)
            {
                _logger.Log(LogLevel.Error, exception, $"Error while initializing service '{id}'.");
            }
        }
예제 #2
0
        private ServiceInstance CreateServiceInstance(string id, ServiceConfiguration configuration)
        {
            var repositoryEntityUid    = new RepositoryEntityUid(id, configuration.Version);
            var repositoryEntitySource = _repositoryService.LoadEntity(repositoryEntityUid);

            var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost(_logger);

            scriptHost.Initialize(repositoryEntitySource.Script);

            var serviceInstance = new ServiceInstance(id, configuration, scriptHost);

            if (configuration.Variables != null)
            {
                foreach (var variable in configuration.Variables)
                {
                    serviceInstance.SetVariable(variable.Key, variable.Value);
                }
            }

            return(serviceInstance);
        }
        private ServiceInstance CreateServiceInstance(string id, ServiceConfiguration configuration)
        {
            var packageUid = new PackageUid(id, configuration.Version);
            var package    = _repositoryService.LoadPackage(packageUid);

            var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost(_logger);

            scriptHost.Compile(package.Script);

            var serviceInstance = new ServiceInstance(id, configuration, scriptHost);

            if (configuration.Variables != null)
            {
                foreach (var variable in configuration.Variables)
                {
                    serviceInstance.SetVariable(variable.Key, variable.Value);
                }
            }

            return(serviceInstance);
        }