Exemplo n.º 1
0
        private async Task BuildIndexAsync(IBinaryAsyncLockToken lockToken) {
            try {
                if (!lockToken.IsSet) {
                    var startTotalTime = DateTime.Now;

                    await TaskUtilities.SwitchToBackgroundThread();
                    await _host.CreateSessionAsync();
                    Debug.WriteLine("R function host start: {0} ms", (DateTime.Now - startTotalTime).TotalMilliseconds);

                    var startTime = DateTime.Now;
                    // Fetch list of available packages from R session
                    await BuildInstalledPackagesIndexAsync();
                    Debug.WriteLine("R package names/description: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                    // Populate function index for preloaded packages first
                    startTime = DateTime.Now;
                    await BuildPreloadedPackagesFunctionListAsync();
                    Debug.WriteLine("R function index (preloaded): {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                    // Populate function index for all remaining packages
                    startTime = DateTime.Now;
                    await BuildRemainingPackagesFunctionListAsync();
                    Debug.WriteLine("R function index (remaining): {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                    await _functionIndex.BuildIndexAsync(this);
                    Debug.WriteLine("R function index total: {0} ms", (DateTime.Now - startTotalTime).TotalMilliseconds);
                }
            } catch (RHostDisconnectedException ex) {
                Debug.WriteLine(ex.Message);
                ScheduleIdleTimeRebuild();
            } finally {
                lockToken.Set();
            }
        }
Exemplo n.º 2
0
 private async Task RunHost(IBinaryAsyncLockToken lockToken, CancellationToken initializationCt)
 {
     try {
         ScheduleAfterHostStarted(_startupInfo);
         await _host.Run(initializationCt);
     } catch (OperationCanceledException oce) {
         _initializationTcs.TrySetCanceled(oce);
     } catch (MessageTransportException mte) {
         _initializationTcs.TrySetCanceled(new RHostDisconnectedException(string.Empty, mte));
     } catch (Exception ex) {
         _initializationTcs.TrySetException(ex);
     } finally {
         lockToken.Reset();
     }
 }
Exemplo n.º 3
0
        private async Task BuildIndexAsync(IBinaryAsyncLockToken lockToken, CancellationToken ct)
        {
            try {
                if (!lockToken.IsSet)
                {
                    await TaskUtilities.SwitchToBackgroundThread();

                    var stopwatch = new Stopwatch();
                    stopwatch.Start();

                    // Ensure session is started
                    await _host.StartSessionAsync(ct);

                    Debug.WriteLine("R function host start: {0} ms", stopwatch.ElapsedMilliseconds);

                    // Fetch list of package functions from R session
                    _disposableBag.ThrowIfDisposed();
                    stopwatch.Reset();
                    await LoadInstalledPackagesIndexAsync(ct);

                    Debug.WriteLine("Fetch list of package functions from R session: {0} ms", stopwatch.ElapsedMilliseconds);

                    // Try load missing functions from cache or explicitly
                    _disposableBag.ThrowIfDisposed();
                    stopwatch.Reset();
                    await LoadRemainingPackagesFunctions(ct);

                    Debug.WriteLine("Try load missing functions from cache or explicitly: {0} ms", stopwatch.ElapsedMilliseconds);

                    // Build index
                    _disposableBag.ThrowIfDisposed();
                    stopwatch.Reset();
                    await _functionIndex.BuildIndexAsync(this, ct);

                    Debug.WriteLine("R function index build: {0} ms", stopwatch.ElapsedMilliseconds);

                    stopwatch.Stop();
                }
            } catch (RHostDisconnectedException ex) {
                Debug.WriteLine(ex.Message);
                ScheduleIdleTimeRebuild();
            } catch (ObjectDisposedException) { }
            finally {
                lockToken.Set();
            }
        }
Exemplo n.º 4
0
        private async Task StartHostAsyncBackground(RHost host, IBinaryAsyncLockToken lockToken, CancellationToken cancellationToken = default(CancellationToken))
        {
            await TaskUtilities.SwitchToBackgroundThread();

            ResetInitializationTcs();
            ClearPendingRequests(new RHostDisconnectedException());

            Interlocked.Exchange(ref _host, host);
            var initializationCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            var hostRunTask       = RunHost(lockToken, initializationCts.Token);

            Interlocked.Exchange(ref _hostRunTask, hostRunTask)?.DoNotWait();

            await _initializationTcs.Task;

            initializationCts.Dispose();
            lockToken.Set();
        }
Exemplo n.º 5
0
        private async Task BuildIndexAsync(IBinaryAsyncLockToken lockToken)
        {
            try {
                if (!lockToken.IsSet)
                {
                    var startTotalTime = DateTime.Now;

                    await TaskUtilities.SwitchToBackgroundThread();

                    await _host.StartSessionAsync();

                    Debug.WriteLine("R function host start: {0} ms", (DateTime.Now - startTotalTime).TotalMilliseconds);

                    var startTime = DateTime.Now;
                    // Fetch list of available packages from R session
                    await BuildInstalledPackagesIndexAsync();

                    Debug.WriteLine("R package names/description: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                    // Populate function index for preloaded packages first
                    startTime = DateTime.Now;
                    await BuildPreloadedPackagesFunctionListAsync();

                    Debug.WriteLine("R function index (preloaded): {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                    // Populate function index for all remaining packages
                    startTime = DateTime.Now;
                    await BuildRemainingPackagesFunctionListAsync();

                    Debug.WriteLine("R function index (remaining): {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                    await _functionIndex.BuildIndexAsync(this);

                    Debug.WriteLine("R function index total: {0} ms", (DateTime.Now - startTotalTime).TotalMilliseconds);
                }
            } catch (RHostDisconnectedException ex) {
                Debug.WriteLine(ex.Message);
                ScheduleIdleTimeRebuild();
            } finally {
                lockToken.Set();
            }
        }
Exemplo n.º 6
0
        private async Task BuildIndexAsync(IBinaryAsyncLockToken lockToken)
        {
            try {
                if (!lockToken.IsSet)
                {
                    await TaskUtilities.SwitchToBackgroundThread();

                    var stopwatch = new Stopwatch();
                    stopwatch.Start();

                    // Ensure session is started
                    await _host.StartSessionAsync();

                    Debug.WriteLine("R function host start: {0} ms", stopwatch.ElapsedMilliseconds);

                    // Fetch list of package functions from R session
                    await LoadInstalledPackagesIndexAsync();

                    Debug.WriteLine("Fetch list of package functions from R session: {0} ms", stopwatch.ElapsedMilliseconds);

                    // Try load missing functions from cache or explicitly
                    await LoadRemainingPackagesFunctions();

                    Debug.WriteLine("Try load missing functions from cache or explicitly: {0} ms", stopwatch.ElapsedMilliseconds);

                    // Build index
                    await _functionIndex.BuildIndexAsync(this);

                    Debug.WriteLine("R function index total: {0} ms", stopwatch.ElapsedMilliseconds);

                    stopwatch.Stop();
                }
            } catch (Exception ex) when(!ex.IsCriticalException())
            {
                Debug.WriteLine(ex.Message);
                ScheduleIdleTimeRebuild();
            } finally {
                lockToken.Set();
            }
        }
Exemplo n.º 7
0
 public async Task AcquireLockAsync(CancellationToken cancellationToken)
 {
     // reset and acquire _initializationLock, but don't interrupt existing initialization
     _lockToken = await _session._initializationLock.ResetAsync(cancellationToken);
 }
Exemplo n.º 8
0
        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);
        }