Example #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);
        }
Example #2
0
            public Task <Kernel> ConnectKernelAsync(KernelInfo kernelInfo)
            {
                var proxy = new ProxyKernel(kernelInfo.LocalName, _defaultReceiver.CreateChildReceiver(), _defaultSender);
                var _     = proxy.StartAsync();

                return(Task.FromResult((Kernel)proxy));
            }
    public override async Task <Kernel> ConnectKernelAsync(KernelInfo kernelInfo)
    {
        ProxyKernel?proxyKernel;

        if (_receiver is not null)
        {
            proxyKernel = new ProxyKernel(kernelInfo.LocalName, _receiver.CreateChildReceiver(), _sender);
        }
        else
        {
            _clientStream = new NamedPipeClientStream(
                ".",
                PipeName,
                PipeDirection.InOut,
                PipeOptions.Asynchronous, TokenImpersonationLevel.Impersonation);

            await _clientStream.ConnectAsync();

            _clientStream.ReadMode = PipeTransmissionMode.Message;

            _receiver = new MultiplexingKernelCommandAndEventReceiver(new KernelCommandAndEventPipeStreamReceiver(_clientStream));
            _sender   = new KernelCommandAndEventPipeStreamSender(_clientStream);


            proxyKernel = new ProxyKernel(kernelInfo.LocalName, _receiver, _sender);
        }

        var _ = proxyKernel.StartAsync();

        return(proxyKernel);;
    }
Example #4
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);
        }
Example #5
0
        public void cannot_be_started_multiple_times()
        {
            using var kernel = new ProxyKernel("proxy", new BlockingCommandAndEventReceiver(),
                                               new RecordingKernelCommandAndEventSender());

            kernel.StartAsync();

            var restart = new Action(() => kernel.StartAsync());

            restart.Should().Throw <InvalidOperationException>()
            .Which
            .Message
            .Should()
            .Be("ProxyKernel proxy is already started.");
        }
        public void When_targeting_a_local_kernel_after_targeting_a_proxy_kernel_splitting_resumes()
        {
            var proxyKernel = new ProxyKernel(
                "proxyKernel",
                new NullKernelCommandAndEventReceiver(),
                new NullKernelCommandAndEventSender());

            var parser = CreateSubmissionParser(additionalKernels: new Kernel[] { proxyKernel });

            var proxyCode = @"Console.WriteLine(""Hello from Proxy"");

#!time
var a = 12;

#!time
var b = 22;";

            var submission = $@"#!{proxyKernel.Name}
{proxyCode}
#!csharp
var d = 12;
#!time
Console.WriteLine(d);
";

            var tree = parser.Parse(submission);

            var nodes = tree.GetRoot()
                        .ChildNodes.ToList();

            var codeNodes = nodes.Where(n => n.GetType() == typeof(LanguageNode)).Select(n => n.Text).ToList();


            codeNodes.Should().BeEquivalentTo(
                $@"{proxyCode}
",
                @"var d = 12;
",
                @"Console.WriteLine(d);
");

            nodes.Should().ContainSingle <DirectiveNode>(n => n.Text.StartsWith("#!time"));
        }
Example #7
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);
        }
        public void Submissions_targeting_proxy_kernels_are_not_split_prior_to_sending()
        {
            var proxyKernel = new ProxyKernel(
                "proxyKernel",
                new NullKernelCommandAndEventReceiver(),
                new NullKernelCommandAndEventSender());

            var parser = CreateSubmissionParser(additionalKernels: new Kernel[] { proxyKernel });

            var code = @"Console.WriteLine(""Hello from Proxy"");

#!time
var a = 12;

#!time
var b = 22;";

            var submission = $@"#!{proxyKernel.Name}
{code}";

            var tree = parser.Parse(submission);

            var nodes = tree.GetRoot()
                        .ChildNodes.ToList();

            nodes
            .First()
            .As <ProxyKernelNameDirectiveNode>()
            .Should()
            .NotBeNull();

            nodes
            .Should()
            .ContainSingle <LanguageNode>(n => n.GetType() == typeof(LanguageNode))
            .Which
            .Text
            .Should()
            .Be(code);
        }
    public override 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,
                StandardOutputEncoding = Encoding.UTF8
            };
            _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);

            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);
        }
    }