예제 #1
0
        public async Task Handle(JupyterRequestContext context)
        {
            var executeRequest = GetJupyterRequest(context);

            context.RequestHandlerStatus.SetAsBusy();
            var executionCount = executeRequest.Silent ? _executionCount : Interlocked.Increment(ref _executionCount);

            var command   = new SubmitCode(executeRequest.Code, "csharp");
            var id        = Guid.NewGuid();
            var transient = new Dictionary <string, object> {
                { "display_id", id.ToString() }
            };
            var openRequest = new InflightRequest(context, executeRequest, executionCount, transient);

            InFlightRequests[command] = openRequest;

            try
            {
                var kernelResult = await Kernel.SendAsync(command);

                openRequest.AddDisposable(kernelResult.KernelEvents.Subscribe(OnKernelResultEvent));
            }
            catch (Exception e)
            {
                InFlightRequests.TryRemove(command, out _);

                var errorContent = new Error(
                    eName: "Unhandled Exception",
                    eValue: $"{e.Message}"
                    );

                if (!executeRequest.Silent)
                {
                    // send on io
                    var error = Message.Create(
                        errorContent,
                        context.Request.Header);
                    context.IoPubChannel.Send(error);

                    // send on stderr
                    var stdErr = new StdErrStream(errorContent.EValue);
                    var stream = Message.Create(
                        stdErr,
                        context.Request.Header);
                    context.IoPubChannel.Send(stream);
                }

                //  reply Error
                var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: executionCount);

                // send to server
                var executeReply = Message.CreateResponse(
                    executeReplyPayload,
                    context.Request);

                context.ServerChannel.Send(executeReply);
                context.RequestHandlerStatus.SetAsIdle();
            }
        }
예제 #2
0
        public async Task Handle(JupyterRequestContext context)
        {
            var executeRequest = GetJupyterRequest(context);

            context.RequestHandlerStatus.SetAsBusy();
            var executionCount = executeRequest.Silent ? _executionCount : Interlocked.Increment(ref _executionCount);

            var command = new SubmitCode(executeRequest.Code, "csharp");

            var openRequest = new InflightRequest(context, executeRequest, executionCount);

            InFlightRequests[command] = openRequest;

            try
            {
                await Kernel.SendAsync(command);
            }
            catch (Exception e)
            {
                InFlightRequests.TryRemove(command, out _);

                var errorContent = new Error(
                    eName: "Unhandled Exception",
                    eValue: $"{e.Message}"
                    );

                if (!executeRequest.Silent)
                {
                    // send on io
                    var error = Message.Create(
                        errorContent,
                        context.Request.Header);
                    context.IoPubChannel.Send(error);

                    // send on stderr
                    var stdErr = new StdErrStream(errorContent.EValue);
                    var stream = Message.Create(
                        stdErr,
                        context.Request.Header);
                    context.IoPubChannel.Send(stream);
                }

                //  reply Error
                var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: executionCount);

                // send to server
                var executeReply = Message.CreateResponse(
                    executeReplyPayload,
                    context.Request);

                context.ServerChannel.Send(executeReply);
                context.RequestHandlerStatus.SetAsIdle();
            }
        }
예제 #3
0
        private void OnCodeSubmissionEvaluated(CodeSubmissionEvaluated codeSubmissionEvaluated)
        {
            InFlightRequests.TryRemove(codeSubmissionEvaluated.Command, out var openRequest);

            // reply ok
            var executeReplyPayload = new ExecuteReplyOk(executionCount: openRequest.ExecutionCount);

            // send to server
            var executeReply = Message.CreateResponse(
                executeReplyPayload,
                openRequest.Context.Request);

            openRequest.Context.ServerChannel.Send(executeReply);
            openRequest.Context.RequestHandlerStatus.SetAsIdle();
            openRequest.Dispose();
        }
예제 #4
0
        private void OnValueProductionEvent(ValueProducedEventBase eventBase)
        {
            if (!InFlightRequests.TryGetValue(eventBase.GetRootCommand(), out var openRequest))
            {
                return;
            }

            var transient = CreateTransient(eventBase.ValueId);

            var formattedValues = eventBase
                                  .FormattedValues
                                  .ToDictionary(k => k.MimeType, v => v.Value);

            var value = eventBase.Value;

            CreateDefaultFormattedValueIfEmpty(formattedValues, value);

            DisplayData executeResultData;

            switch (eventBase)
            {
            case DisplayedValueProduced _:
                executeResultData = new DisplayData(
                    transient: transient,
                    data: formattedValues);
                break;

            case ReturnValueProduced _:
                executeResultData = new ExecuteResult(
                    openRequest.ExecutionCount,
                    transient: transient,
                    data: formattedValues);
                break;

            case DisplayedValueUpdated _:
                executeResultData = new UpdateDisplayData(
                    transient: transient,
                    data: formattedValues);
                break;

            default:
                throw new ArgumentException("Unsupported event type", nameof(eventBase));
            }

            SendDisplayData(executeResultData, openRequest);
        }
예제 #5
0
        private void OnExecutionInterrupted(CurrentCommandCancelled currentCommandCancelled)
        {
            if (InFlightRequests.TryRemove(currentCommandCancelled.Command, out var openRequest))
            {
                // reply
                var interruptReplyPayload = new InterruptReply();

                // send to server
                var interruptReply = Message.CreateResponse(
                    interruptReplyPayload,
                    openRequest.Context.Request);

                openRequest.Context.ServerChannel.Send(interruptReply);
                openRequest.Context.RequestHandlerStatus.SetAsIdle();
                openRequest.Dispose();
            }
        }
예제 #6
0
        private void OnCommandHandled(CommandHandled commandHandled)
        {
            if (!InFlightRequests.TryRemove(commandHandled.GetRootCommand(), out var openRequest))
            {
                return;
            }

            // reply ok
            var executeReplyPayload = new ExecuteReplyOk(executionCount: openRequest.ExecutionCount);

            // send to server
            var executeReply = Message.CreateResponse(
                executeReplyPayload,
                openRequest.Context.Request);

            openRequest.Context.ServerChannel.Send(executeReply);
            openRequest.Context.RequestHandlerStatus.SetAsIdle();
            openRequest.Dispose();
        }
        private void OnKernelEvent(IKernelEvent @event, bool isComplete)
        {
            if (InFlightRequests.TryRemove(@event.Command, out var openRequest))
            {
                var status = isComplete ? "complete" : "incomplete";
                var indent = isComplete ? string.Empty : "*";
                // reply
                var isCompleteReplyPayload = new IsCompleteReply(indent: indent, status: status);

                // send to server
                var executeReply = Message.CreateResponse(
                    isCompleteReplyPayload,
                    openRequest.Context.Request);

                openRequest.Context.ServerChannel.Send(executeReply);
                openRequest.Context.RequestHandlerStatus.SetAsIdle();
                openRequest.Dispose();
            }
        }
예제 #8
0
        private void OnCommandFailed(CommandFailed commandFailed)
        {
            if (!InFlightRequests.TryRemove(commandFailed.GetRootCommand(), out var openRequest))
            {
                return;
            }

            var errorContent = new Error(
                eName: "Unhandled Exception",
                eValue: commandFailed.Message
                );

            if (!openRequest.Request.Silent)
            {
                // send on io
                var error = Message.Create(
                    errorContent,
                    openRequest.Context.Request.Header);
                openRequest.Context.IoPubChannel.Send(error);

                // send on stderr
                var stdErr = new StdErrStream(errorContent.EValue);
                var stream = Message.Create(
                    stdErr,
                    openRequest.Context.Request.Header);
                openRequest.Context.IoPubChannel.Send(stream);
            }

            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: openRequest.ExecutionCount);

            // send to server
            var executeReply = Message.CreateResponse(
                executeReplyPayload,
                openRequest.Context.Request);

            openRequest.Context.ServerChannel.Send(executeReply);

            openRequest.Context.RequestHandlerStatus.SetAsIdle();
            openRequest.Dispose();
        }