示例#1
0
        public override async Task <Kernel> CreateKernelAsync(
            SignalRConnectionOptions options,
            KernelInvocationContext context)
        {
            var connection = new HubConnectionBuilder()
                             .WithUrl(options.HubUrl)
                             .Build();

            await connection.StartAsync();

            await connection.SendAsync("connect");

            var receiver    = new KernelCommandAndEventSignalRHubConnectionReceiver(connection);
            var sender      = new KernelCommandAndEventSignalRHubConnectionSender(connection);
            var proxyKernel = new ProxyKernel(options.KernelName, receiver, sender);

            var _ = proxyKernel.RunAsync();

            proxyKernel.RegisterForDisposal(receiver);
            proxyKernel.RegisterForDisposal(async() =>
            {
                await connection.DisposeAsync();
            });

            return(proxyKernel);
        }
示例#2
0
        public async Task <Kernel> ConnectKernelAsync(KernelInfo kernelInfo)
        {
            // QUESTION: (ConnectKernelAsync) tests?
            var hubConnection = new HubConnectionBuilder()
                                .WithUrl(HubUrl)
                                .Build();

            await hubConnection.StartAsync();

            await hubConnection.SendAsync("connect");

            var receiver    = new KernelCommandAndEventSignalRHubConnectionReceiver(hubConnection);
            var sender      = new KernelCommandAndEventSignalRHubConnectionSender(hubConnection);
            var proxyKernel = new ProxyKernel(kernelInfo.LocalName, receiver, sender);

            var _ = proxyKernel.StartAsync();

            proxyKernel.RegisterForDisposal(receiver);
            proxyKernel.RegisterForDisposal(async() =>
            {
                await hubConnection.DisposeAsync();
            });

            return(proxyKernel);
        }
示例#3
0
        private async Task <Kernel> CreateProxyKernel(Language language)
        {
            var psi = new ProcessStartInfo
            {
                FileName               = "dotnet-interactive",
                Arguments              = $"stdio --default-kernel {language.LanguageName()}",
                WorkingDirectory       = Directory.GetCurrentDirectory(),
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                UseShellExecute        = false,
            };

            var process = new Process {
                StartInfo = psi
            };
            TaskCompletionSource <bool> ready = new();

            process.Start();

            var receiver = new KernelCommandAndEventTextReceiver(process.StandardOutput);
            var sender   = new KernelCommandAndEventTextStreamSender(process.StandardInput);

            var kernel = new ProxyKernel("proxy", receiver, sender);

            kernel.RegisterForDisposal(() =>
            {
                process.Kill(true);
                process.Dispose();
            });

            var _ = kernel.RunAsync();

            var sub = kernel.KernelEvents.OfType <KernelReady>().Subscribe(_ =>
            {
                ready.SetResult(true);
            });

            await ready.Task;

            sub.Dispose();

            return(kernel);
        }
示例#4
0
    public async Task <Kernel> ConnectKernelAsync(KernelInfo kernelInfo)
    {
        if (_receiver is not null)
        {
            var kernel = new ProxyKernel(kernelInfo.LocalName, _receiver.CreateChildReceiver(), _sender);
            var _      = kernel.StartAsync();
            return(kernel);
        }
        else
        {
            // QUESTION: (ConnectKernelAsync) tests?
            var command   = Command[0];
            var arguments = string.Join(" ", Command.Skip(1));
            var psi       = new ProcessStartInfo
            {
                FileName               = command,
                Arguments              = arguments,
                WorkingDirectory       = WorkingDirectory.FullName,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                UseShellExecute        = false,
            };
            _process = new Process {
                StartInfo = psi
            };
            _process.EnableRaisingEvents = true;
            var stdErr = new StringBuilder();
            _process.ErrorDataReceived += (o, args) => { stdErr.Append(args.Data); };
            await Task.Yield();

            _process.Start();
            _process.BeginErrorReadLine();
            _receiver = new MultiplexingKernelCommandAndEventReceiver(
                new KernelCommandAndEventTextReceiver(_process.StandardOutput));
            _sender = new KernelCommandAndEventTextStreamSender(_process.StandardInput);
            var kernel = new ProxyKernel(kernelInfo.LocalName, _receiver, _sender);
            kernel.RegisterForDisposal(this);

            var r = _receiver.CreateChildReceiver();
            var _ = kernel.StartAsync();

            var checkReady = Task.Run(async() =>
            {
                await foreach (var eoc in r.CommandsAndEventsAsync(CancellationToken.None))
                {
                    if (eoc.Event is KernelReady)
                    {
                        return;
                    }
                }
            });

            var checkProcessError = Task.Run(async() =>
            {
                while (!checkReady.IsCompleted)
                {
                    await Task.Delay(200);
                    if (_process.HasExited)
                    {
                        if (_process.ExitCode != 0)
                        {
                            throw new CommandLineInvocationException(
                                new CommandLineResult(_process.ExitCode,
                                                      error: stdErr.ToString().Split(new[] { '\r', '\n' },
                                                                                     StringSplitOptions.RemoveEmptyEntries)));
                        }
                    }
                }
            });

            await Task.WhenAny(checkProcessError, checkReady);

            return(kernel);
        }
    }