public static Connection Create(ISecurityService securityService, IContainer container, string rCommandLineArguments, bool isUserCreated, bool fetchHostLoad) { var path = $"https://localhost:{container.HostPorts.First()}"; var brokerConnectionInfo = BrokerConnectionInfo.Create(securityService, container.Name, path, rCommandLineArguments, fetchHostLoad); return(new Connection(brokerConnectionInfo, path, isUserCreated, container)); }
public async Task ReconnectAsync(CancellationToken cancellationToken, ReentrancyToken reentrancyToken) { using (_session._disposeToken.Link(ref cancellationToken)) { if (!VerifyTransactionState(nameof(ConnectToNewBrokerAsync))) { return; } var host = _session._host; // host may be null if previous attempts to start it have failed if (host != null) { // Detach RHost from RSession host.DetachCallback(); // Cancel all current requests (if any) await _session.CancelAllAsync(cancellationToken); host.Dispose(); await _session._hostRunTask; } var connectionInfo = new BrokerConnectionInfo(_session._startupInfo.Name, _session, _session._startupInfo.RHostCommandLineArguments); host = await _session.BrokerClient.ConnectAsync(connectionInfo, cancellationToken, reentrancyToken); await _session.StartHostAsyncBackground(host, _lockToken, cancellationToken); } }
public Task TestConnectionAsync(IConnectionInfo connection, CancellationToken cancellationToken = default(CancellationToken)) { var brokerConnectionInfo = (connection as IConnection)?.BrokerConnectionInfo ?? BrokerConnectionInfo.Create(_securityService, connection.Name, connection.Path, connection.RCommandLineArguments); return(_sessionProvider.TestBrokerConnectionAsync(connection.Name, brokerConnectionInfo, cancellationToken)); }
public void StartRHostMissing() { var brokerClient = new LocalBrokerClient(nameof(RSessionTest), BrokerConnectionInfo.Create(null, "C", @"C:\"), _services, new NullConsole(), Environment.SystemDirectory); var session = new RSession(0, _testMethod.FileSystemSafeName, brokerClient, new AsyncReaderWriterLock().CreateExclusiveReaderLock(), () => { }); Func <Task> start = () => session.StartHostAsync(new RHostStartupInfo(), null, 10000); start.ShouldThrow <ComponentBinaryMissingException>(); }
public static Connection Create(ISecurityService securityService, IContainerManager containers, IConnectionInfo connectionInfo, bool fetchHostLoad) { var brokerConnectionInfo = BrokerConnectionInfo.Create(securityService, connectionInfo.Name, connectionInfo.Path, connectionInfo.RCommandLineArguments, fetchHostLoad); var uri = brokerConnectionInfo.Uri; var container = brokerConnectionInfo.IsUrlBased && uri.IsLoopback ? containers.GetContainers().FirstOrDefault(c => c.HostPorts.Contains(uri.Port)) : null; return(new Connection(brokerConnectionInfo, connectionInfo, container)); }
public async Task StopBeforeInitialized_RHostMissing() { var brokerClient = new LocalBrokerClient(nameof(RSessionTest), BrokerConnectionInfo.Create(null, "C", @"C:\"), _services, new NullConsole(), Environment.SystemDirectory); var session = new RSession(0, _testMethod.FileSystemSafeName, brokerClient, new AsyncReaderWriterLock().CreateExclusiveReaderLock(), () => { }); Func <Task> start = () => session.StartHostAsync(new RHostStartupInfo(), null, 10000); var startTask = Task.Run(start).SilenceException <RHostBinaryMissingException>(); await session.StopHostAsync(); session.IsHostRunning.Should().BeFalse(); await startTask; }
static void Main(string[] args) { Console.CancelKeyPress += Console_CancelKeyPress; var programName = "Microsoft.R.Host.Client.Program"; using (var shell = new CoreShell(programName)) { var localConnector = new LocalBrokerClient(programName, BrokerConnectionInfo.Create(null, "local", args[0], null, false), shell.Services, new NullConsole()); var host = localConnector.ConnectAsync(new HostConnectionInfo(programName, new Program())).GetAwaiter().GetResult(); _evaluator = host; host.Run().GetAwaiter().GetResult(); } }
/// <summary> /// Creates R session /// </summary> /// <param name="name">Session name</param> /// <param name="url">Path to local R interpreter (folder with R.dll) or URL to the remote machine</param> /// <returns>R session</returns> public static IRHostSession Create(string name) { var e = new WindowsRInstallation().GetCompatibleEngines().FirstOrDefault(); if (e == null) { throw new InvalidOperationException("No R engines installed"); } var ci = BrokerConnectionInfo.Create(e.Name, e.InstallPath, e.Architecture, string.Empty); var bc = new LocalBrokerClient(name, ci, CoreShell.Create()); return(new RHostSession(new RSession(0, name, bc, new NullLock(), () => { }))); }
private static async Task TestBrokerConnectionWithRHost(IBrokerClient brokerClient, CancellationToken cancellationToken, ReentrancyToken reentrancyToken) { var callbacks = new NullRCallbacks(); var connectionInfo = new BrokerConnectionInfo(nameof(TestBrokerConnectionAsync), callbacks); var rhost = await brokerClient.ConnectAsync(connectionInfo, cancellationToken, reentrancyToken); try { var rhostRunTask = rhost.Run(cancellationToken); callbacks.SetReadConsoleInput("q()\n"); await rhostRunTask; } finally { rhost.Dispose(); } }
public async Task ConnectToNewBrokerAsync(CancellationToken cancellationToken, ReentrancyToken reentrancyToken) { using (_session._disposeToken.Link(ref cancellationToken)) { if (!VerifyTransactionState(nameof(ConnectToNewBrokerAsync))) { return; } var startupInfo = _session._startupInfo; // host requires _startupInfo, so proceed only if session was started at least once var connectionInfo = new BrokerConnectionInfo(startupInfo.Name, _session, startupInfo.RHostCommandLineArguments); _hostToSwitch = await _session.BrokerClient.ConnectAsync(connectionInfo, cancellationToken, reentrancyToken); } }
static void Main(string[] args) { Console.CancelKeyPress += Console_CancelKeyPress; var programName = "Microsoft.R.Host.Client.Program"; using (var logger = new Logger(programName, Path.GetTempPath(), new MaxLoggingPermissions())) { var services = new CoreServices(null, new MaxLoggingPermissions(), null, null, null, logger, null, null); var localConnector = new LocalBrokerClient(programName, BrokerConnectionInfo.Create(null, "local", args[0]), services, new NullConsole()); var host = localConnector.ConnectAsync(new HostConnectionInfo(programName, new Program())).GetAwaiter().GetResult(); _evaluator = host; host.Run().GetAwaiter().GetResult(); } }
public Task <bool> ConnectAsync(IRSessionProvider sessionProvider) { var securityService = _services.GetService <ISecurityService>(); if (securityService is SecurityServiceStub securityServiceStub) { securityServiceStub.GetUserNameHandler = s => UserName; securityServiceStub.GetUserCredentialsHandler = (authority, workspaceName) => Credentials.Create(UserName, _remoteBrokerFixture.Password); } var brokerConnectionInfo = BrokerConnectionInfo.Create(securityService, _testName, _remoteBrokerFixture.Address); return(sessionProvider.TrySwitchBrokerAsync(_testName, brokerConnectionInfo)); }
private async Task ConnectAsync(CancellationToken ct) { var provider = _services.GetService <IRInteractiveWorkflowProvider>(); _workflow = provider.GetOrCreate(); _ui = _services.GetService <IHostUIService>(); _ui.SetLogLevel(MessageType.Info); var e = GetREngine(); if (e == null) { return; } var log = _services.Log(); var info = BrokerConnectionInfo.Create("(local)", e.InstallPath, e.Architecture, string.Empty); var start = DateTime.Now; _ui.LogMessageAsync($"Starting R Process with {e.InstallPath}...", MessageType.Info).DoNotWait(); try { if (await _workflow.RSessions.TrySwitchBrokerAsync("(local)", info, ct)) { try { await _workflow.RSession.StartHostAsync(new RHostStartupInfo(), new RSessionCallback(), _services.UI(), Debugger.IsAttached? 1000000 : 20000, ct); } catch (Exception ex) { _ui.ShowMessageAsync($"Unable to start Microsoft.R.Host process. Exception: {ex.Message}", MessageType.Error).DoNotWait(); return; } // Start package building _ui.LogMessageAsync($"complete in {FormatElapsed(DateTime.Now - start)}", MessageType.Info).DoNotWait(); start = DateTime.Now; _ui.LogMessageAsync("Building IntelliSense index...", MessageType.Info).DoNotWait(); _packageIndex = _services.GetService <IPackageIndex>(); _packageIndex.BuildIndexAsync(ct).ContinueWith(t => { _ui.LogMessageAsync($"complete in {FormatElapsed(DateTime.Now - start)}", MessageType.Info).DoNotWait(); }, ct, TaskContinuationOptions.None, TaskScheduler.Default).DoNotWait(); } else { _ui.ShowMessageAsync($"Unable to connect to broker.", MessageType.Error).DoNotWait(); } } catch (Exception ex) { _ui.ShowMessageAsync($"Unable to connect to broker. Exception: {ex.Message}", MessageType.Error).DoNotWait(); } }
private async Task ConnectAsync(CancellationToken ct) { var provider = _services.GetService <IRInteractiveWorkflowProvider>(); _workflow = provider.GetOrCreate(); _ui = _services.GetService <IUIService>(); _ui.SetLogLevel(MessageType.Info); var e = GetREngine(); if (e == null) { return; } var log = _services.Log(); var info = BrokerConnectionInfo.Create(_services.Security(), "VSCR", e.InstallPath, string.Empty, false); var start = DateTime.Now; var message = $"Starting R Process with {e.InstallPath}..."; _ui.LogMessageAsync(message, MessageType.Info).DoNotWait(); log.Write(LogVerbosity.Normal, MessageCategory.General, $"Switching local broker to {e.InstallPath}"); if (await _workflow.RSessions.TrySwitchBrokerAsync("VSCR", info, ct)) { try { await _workflow.RSession.StartHostAsync(new RHostStartupInfo(), new RSessionCallback(), Debugger.IsAttached? 100000 : 20000, ct); } catch (Exception ex) { _ui.ShowMessageAsync($"Unable to start R process. Exception: {ex.Message}", MessageType.Error).DoNotWait(); return; } // Start package building _ui.LogMessageAsync($"complete in {FormatElapsed(DateTime.Now - start)}", MessageType.Info).DoNotWait(); start = DateTime.Now; _ui.LogMessageAsync("Building IntelliSense index...", MessageType.Info).DoNotWait(); _packageIndex = _services.GetService <IPackageIndex>(); _packageIndex.BuildIndexAsync(ct).ContinueWith(t => { _ui.LogMessageAsync($"complete in {FormatElapsed(DateTime.Now - start)}", MessageType.Info).DoNotWait(); }, ct, TaskContinuationOptions.None, TaskScheduler.Default).DoNotWait(); } else { _ui.ShowMessageAsync("Unable to start R process", MessageType.Error).DoNotWait(); } }
public async Task <bool> ConnectAsync(IRSessionProvider sessionProvider) { var securityService = _services.GetService <ISecurityService>(); if (securityService is SecurityServiceStub securityServiceStub) { securityServiceStub.ReadUserCredentialsHandler = s => (UserName, _remoteBrokerFixture.Password); securityServiceStub.GetUserCredentialsHandler = (authority, workspaceName) => Credentials.Create(UserName, _remoteBrokerFixture.Password); } await _remoteBrokerFixture.EnsureBrokerStartedAsync(_assemblyName, _services); var brokerConnectionInfo = BrokerConnectionInfo.Create(securityService, _testName, _remoteBrokerFixture.Address, null, false); return(await sessionProvider.TrySwitchBrokerAsync(_testName, brokerConnectionInfo)); }
/// <summary> /// Creates R session /// </summary> /// <param name="name">Session name</param> /// <param name="url">Path to local R interpreter (folder with R.dll) or URL to the remote machine</param> /// <returns>R session</returns> public static IRHostSession Create(string name, string url = null) { if (string.IsNullOrEmpty(url)) { var engine = new WindowsRInstallation().GetCompatibleEngines().FirstOrDefault(); if (engine == null) { throw new InvalidOperationException("No R engines installed"); } url = engine.InstallPath; } var ci = BrokerConnectionInfo.Create(name, url, null); var bc = new LocalBrokerClient(name, ci, new ServiceContainer(), new NullConsole(), null); return(new RHostSession(new RSession(0, name, bc, new NullLock(), () => { }))); }
public async Task TestBrokerConnectionAsync(string name, BrokerConnectionInfo connectionInfo, CancellationToken cancellationToken = default(CancellationToken)) { using (_disposeToken.Link(ref cancellationToken)) { await TaskUtilities.SwitchToBackgroundThread(); // Create random name to avoid collision with actual broker client name = name + Guid.NewGuid().ToString("N"); var brokerClient = CreateBrokerClient(name, connectionInfo, cancellationToken); if (brokerClient == null) { throw new ArgumentException(nameof(connectionInfo)); } using (brokerClient) { await TestBrokerConnectionWithRHost(brokerClient, cancellationToken); } } }
private IBrokerClient CreateBrokerClient(string name, BrokerConnectionInfo connectionInfo, CancellationToken cancellationToken) { if (!connectionInfo.IsValid) { connectionInfo = BrokerConnectionInfo.Create(connectionInfo.Name, new RInstallation().GetCompatibleEngines().FirstOrDefault()?.InstallPath); } if (!connectionInfo.IsValid) { return(null); } if (connectionInfo.IsRemote) { return(new RemoteBrokerClient(name, connectionInfo, _services, _console, cancellationToken)); } return(new LocalBrokerClient(name, connectionInfo, _services, _console)); }
private IBrokerClient CreateBrokerClient(string name, BrokerConnectionInfo connectionInfo, CancellationToken cancellationToken) { if (!connectionInfo.IsValid) { var installSvc = _services.GetService <IRInstallationService>(); var path = installSvc.GetCompatibleEngines().FirstOrDefault()?.InstallPath; connectionInfo = BrokerConnectionInfo.Create(_services.Security(), connectionInfo.Name, path); } if (!connectionInfo.IsValid) { return(null); } if (connectionInfo.IsRemote) { return(new RemoteBrokerClient(name, connectionInfo, _services, _console, cancellationToken)); } return(new LocalBrokerClient(name, connectionInfo, _services, _console)); }
private async Task StartHostAsyncBackground(RHostStartupInfo startupInfo, IRSessionCallback callback, IBinaryAsyncLockToken lockToken, int timeout, CancellationToken cancellationToken) { await TaskUtilities.SwitchToBackgroundThread(); _callback = callback; _startupInfo = startupInfo; RHost host; try { var connectionInfo = new BrokerConnectionInfo(startupInfo.Name, this, startupInfo.RHostCommandLineArguments, timeout); host = await BrokerClient.ConnectAsync(connectionInfo, cancellationToken); } catch (OperationCanceledException ex) { _initializationTcs.TrySetCanceled(ex); lockToken.Reset(); throw; } catch (Exception ex) { _initializationTcs.TrySetException(ex); lockToken.Reset(); throw; } await StartHostAsyncBackground(host, lockToken, cancellationToken); }
public async Task SwitchToInvalid() { using (var sessionProvider = new RSessionProvider(_services)) { var session = sessionProvider.GetOrCreate(nameof(SwitchToInvalid)); await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToInvalid)); await session.EnsureHostStartedAsync(new RHostStartupInfo(), null, 1000); var switch1Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToInvalid) + "1", BrokerConnectionInfo.Create(null, "JHFFR", @"\\JHF\F\R")); await Task.Yield(); var switch2Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToInvalid) + "2"); await ParallelTools.WhenAll(30000, switch1Task, switch2Task); switch1Task.Should().BeRanToCompletion(); switch1Task.Result.Should().BeFalse(); switch2Task.Should().BeRanToCompletion(); switch2Task.Result.Should().BeTrue(); } }
public static Connection Create(ISecurityService securityService, IConnectionInfo connectionInfo) { var brokerConnectionInfo = BrokerConnectionInfo.Create(securityService, connectionInfo.Name, connectionInfo.Path, connectionInfo.RCommandLineArguments); return(new Connection(brokerConnectionInfo, connectionInfo)); }
public Task <bool> TrySwitchBrokerAsync(string name, BrokerConnectionInfo connectionInfo, CancellationToken cancellationToken = default(CancellationToken)) => Task.FromResult(true);
public Task TestBrokerConnectionAsync(string name, BrokerConnectionInfo connectionInfo, CancellationToken cancellationToken = default(CancellationToken)) => Task.CompletedTask;
private Connection(BrokerConnectionInfo brokerConnectionInfo, string path, bool isUserCreated) : base(brokerConnectionInfo.Name, path, brokerConnectionInfo.RCommandLineArguments, isUserCreated) { BrokerConnectionInfo = brokerConnectionInfo; }
public async Task <bool> TrySwitchBrokerAsync(string name, BrokerConnectionInfo connectionInfo = default(BrokerConnectionInfo), CancellationToken cancellationToken = default(CancellationToken)) { using (_disposeToken.Link(ref cancellationToken)) { await TaskUtilities.SwitchToBackgroundThread(); var brokerClient = CreateBrokerClient(name, connectionInfo, cancellationToken); if (brokerClient == null) { return(false); } // Broker switching shouldn't be concurrent IAsyncReaderWriterLockToken lockToken; try { lockToken = await _connectArwl.WriterLockAsync(cancellationToken); } catch (OperationCanceledException) { brokerClient.Dispose(); return(false); } if (brokerClient.ConnectionInfo.Equals(_brokerProxy.ConnectionInfo)) { brokerClient.Dispose(); try { // Switching to the broker that is currently running and connected is always successful if (IsConnected) { return(true); } await ReconnectAsync(cancellationToken); } catch (Exception) { return(false); } finally { lockToken.Dispose(); } IsConnected = true; return(true); } // First switch broker proxy so that all new sessions are created for the new broker var oldBroker = _brokerProxy.Set(brokerClient); if (_updateHostLoadLoopTask == null) { _updateHostLoadLoopTask = UpdateHostLoadLoopAsync(); } try { BrokerChanging?.Invoke(this, EventArgs.Empty); await SwitchBrokerAsync(cancellationToken); oldBroker.Dispose(); } catch (Exception ex) { _brokerProxy.Set(oldBroker); if (_disposeToken.IsDisposed) { oldBroker.Dispose(); } brokerClient.Dispose(); BrokerChangeFailed?.Invoke(this, EventArgs.Empty); if (ex is OperationCanceledException || ex is ComponentBinaryMissingException) { // RHostDisconnectedException is derived from OperationCanceledException return(false); } throw; } finally { lockToken.Dispose(); } IsConnected = true; OnBrokerChanged(); return(true); } }
public Connection(string name, string path, string rCommandLineArguments, bool isUserCreated) : base(name, path, rCommandLineArguments, isUserCreated) { BrokerConnectionInfo = BrokerConnectionInfo.Create(name, path, rCommandLineArguments); }
private Connection(BrokerConnectionInfo brokerConnectionInfo, IConnectionInfo connectionInfo) : base(connectionInfo) { BrokerConnectionInfo = brokerConnectionInfo; }
private static IBrokerClient CreateLocalBrokerClient(IServiceContainer services, string name) => new LocalBrokerClient(name, BrokerConnectionInfo.Create(null, "Test", new RInstallation().GetCompatibleEngines().FirstOrDefault()?.InstallPath), services, new NullConsole());
public Connection(IConnectionInfo connectionInfo) : base(connectionInfo) { BrokerConnectionInfo = BrokerConnectionInfo.Create(connectionInfo.Name, Path, RCommandLineArguments); }