示例#1
0
        private Dictionary <string, IConnection> CreateConnectionList()
        {
            var connections  = GetConnectionsFromSettings();
            var localEngines = _installationService.GetCompatibleEngines().ToList();

            // Remove missing engines and add engines missing from saved connections
            // Set 'is used created' to false if path points to locally found interpreter
            foreach (var kvp in connections.Where(c => !c.Value.IsRemote).ToList())
            {
                var valid = IsValidLocalConnection(kvp.Value.Name, kvp.Value.Path);
                if (!valid)
                {
                    connections.Remove(kvp.Key);
                }
            }

            // For MRS and MRC always use generated name. These upgrade in place
            // so keeping old name with old version doesn't make sense.
            // TODO: handle this better in the future by storing version.
            foreach (var kvp in connections.Where(c => c.Value.Path.ContainsIgnoreCase("R_SERVER")).ToList())
            {
                connections.Remove(kvp.Key);
            }

            // Add newly installed engines
            foreach (var e in localEngines)
            {
                if (!connections.Values.Any(x => x.Path.PathEquals(e.InstallPath)))
                {
                    connections[e.Name] = Connection.Create(_securityService, e.Name, e.InstallPath, string.Empty, false, _settings.ShowHostLoadMeter);
                }
            }

            // Verify that most recently used connection is still valid
            var last = _settings.LastActiveConnection;

            if (last != null && !IsRemoteConnection(last.Path) && !IsValidLocalConnection(last.Name, last.Path))
            {
                // Installation was removed or otherwise disappeared
                _settings.LastActiveConnection = null;
            }

            if (_settings.LastActiveConnection == null && connections.Any())
            {
                // Perhaps first time launch with R preinstalled.
                var connection = PickBestLocalRConnection(connections.Values, localEngines);
                connection.LastUsed            = DateTime.Now;
                _settings.LastActiveConnection = new ConnectionInfo(connection)
                {
                    LastUsed = DateTime.Now
                };
            }

            return(connections);
        }
示例#2
0
        private IEnumerable <Interpreter> GetInterpreters()
        {
            if (_options.AutoDetect)
            {
                _logger.LogTrace(Resources.Trace_AutoDetectingR);

                var engines = _installationService.GetCompatibleEngines().AsList();
                if (engines.Any())
                {
                    var interpreterId = 0;
                    foreach (var e in engines)
                    {
                        var detected = new Interpreter(this, Invariant($"{interpreterId++}"), e.Name, e.InstallPath, e.BinPath, e.Version);
                        _logger.LogTrace(Resources.Trace_DetectedR, detected.Version, detected.Path);
                        yield return(detected);
                    }
                }
                else
                {
                    _logger.LogWarning(Resources.Error_NoRInterpreters);
                }
            }

            foreach (var kv in _options.Interpreters)
            {
                string             id      = kv.Key;
                InterpreterOptions options = kv.Value;

                if (!string.IsNullOrEmpty(options.BasePath) && _fs.DirectoryExists(options.BasePath))
                {
                    var interpInfo = _installationService.CreateInfo(string.Empty, options.BasePath);
                    if (interpInfo.VerifyInstallation())
                    {
                        yield return(new Interpreter(this, id, options.Name, interpInfo.InstallPath, interpInfo.BinPath, interpInfo.Version));

                        continue;
                    }
                }

                _logger.LogError(Resources.Error_FailedRInstallationData, options.Name ?? id, options.BasePath);
            }
        }
示例#3
0
        public async Task StartAsync(Action exited)
        {
            var rhostExe = Path.Combine(_rhostDirectory, RHostExe);

            if (!_fileSystem.FileExists(rhostExe))
            {
                throw new RHostBinaryMissingException();
            }

            var rhostBrokerExe = Path.Combine(_rhostBrokerDirectory, RHostBrokerExe);

            if (!_fileSystem.FileExists(rhostBrokerExe))
            {
                throw new RHostBrokerBinaryMissingException();
            }

            var rHome = _installations.GetCompatibleEngines().First().InstallPath;

            IProcess process = null;

            try {
                var psi = new ProcessStartInfo {
                    FileName        = rhostBrokerExe,
                    UseShellExecute = false,
                    CreateNoWindow  = false,
                    Arguments       =
                        $" --logging:logFolder \"{_logFolder.TrimTrailingSlash()}\"" +
                        $" --logging:logHostOutput true" +
                        $" --logging:logPackets true" +
                        $" --urls http://127.0.0.1:0" + // :0 means first available ephemeral port
                        $" --startup:name \"{_name}\"" +
                        $" --lifetime:parentProcessId {Process.GetCurrentProcess().Id}" +
                        $" --security:secret \"{Password}\"" +
                        $" --R:autoDetect false" +
                        $" --R:interpreters:test:name \"{_name}\"" +
                        $" --R:interpreters:test:basePath \"{rHome.TrimTrailingSlash()}\""
                };

                process         = StartBroker(psi);
                process.Exited += (sender, e) => exited();

                var port = ProcessUtils.GetPortByProcessId(process.Id).FirstOrDefault();
                while (port == 0)
                {
                    await Task.Delay(100);

                    port = ProcessUtils.GetPortByProcessId(process.Id).FirstOrDefault();
                }

                Address        = $"http://127.0.0.1:{port}";
                _brokerProcess = process;
            } finally {
                if (_brokerProcess == null)
                {
                    try {
                        process?.Kill();
                    } catch (Exception) {
                    } finally {
                        process?.Dispose();
                    }
                }
            }
        }