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(); } }
private void OnValueProduced(ValueProduced valueProduced) { var openRequest = InFlightRequests.Values.SingleOrDefault(); if (openRequest == null) { return; } try { var transient = CreateTransient(); // executeResult data var executeResultData = valueProduced.IsLastValue ? new ExecuteResult( openRequest.ExecutionCount, transient: transient, data: valueProduced?.FormattedValues ?.ToDictionary(k => k.MimeType, v => v.Value)) : new DisplayData( transient: transient, data: valueProduced?.FormattedValues ?.ToDictionary(k => k.MimeType, v => v.Value)); if (!openRequest.Request.Silent) { // send on io var executeResultMessage = Message.Create( executeResultData, openRequest.Context.Request.Header); openRequest.Context.IoPubChannel.Send(executeResultMessage); } } catch (Exception e) { var errorContent = new Error( eName: "Unhandled Exception", eValue: $"{e.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); } } }
private static void OnValueProduced(ValueProduced valueProduced, ConcurrentDictionary <IKernelCommand, OpenRequest> openRequests, RenderingEngine renderingEngine) { openRequests.TryGetValue(valueProduced.Command, out var openRequest); if (openRequest == null) { return; } try { var rendering = renderingEngine.Render(valueProduced.Value); // executeResult data var executeResultData = new ExecuteResult( openRequest.ExecutionCount, transient: openRequest.Transient, data: new Dictionary <string, object> { { rendering.MimeType, rendering.Content } }); if (!openRequest.Request.Silent) { // send on io var executeResultMessage = Message.Create( executeResultData, openRequest.Context.Request.Header); openRequest.Context.IoPubChannel.Send(executeResultMessage); } } catch (Exception e) { var errorContent = new Error( eName: "Unhandled Exception", eValue: $"{e.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); } } }
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(); } }
private static void OnValueProduced( ValueProduced valueProduced, ConcurrentDictionary <IKernelCommand, InflightRequest> openRequests) { openRequests.TryGetValue(valueProduced.Command, out var openRequest); if (openRequest == null) { return; } try { // executeResult data var executeResultData = new ExecuteResult( openRequest.ExecutionCount, transient: openRequest.Transient, data: valueProduced?.FormattedValues ?.ToDictionary(k => k.MimeType ?? "text/plain", v => v.Value)); if (!openRequest.Request.Silent) { // send on io var executeResultMessage = Message.Create( executeResultData, openRequest.Context.Request.Header); openRequest.Context.IoPubChannel.Send(executeResultMessage); } } catch (Exception e) { var errorContent = new Error( eName: "Unhandled Exception", eValue: $"{e.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); } } }
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(); }
private static void OnCodeSubmissionEvaluatedFailed(CodeSubmissionEvaluationFailed codeSubmissionEvaluationFailed, ConcurrentDictionary <IKernelCommand, InflightRequest> openRequests) { openRequests.TryRemove(codeSubmissionEvaluationFailed.Command, out var openRequest); var errorContent = new Error( eName: "Unhandled Exception", eValue: $"{codeSubmissionEvaluationFailed.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(); }
public async Task <ICommandDeliveryResult> Handle( ICommandDelivery <JupyterRequestContext> delivery) { switch (delivery.Command.Request.Header.MessageType) { case MessageTypeValues.ExecuteRequest: var transient = new Dictionary <string, object> { { "display_id", Guid.NewGuid().ToString() } }; var jObject = (JObject)delivery.Command.Request.Content; var executeRequest = jObject.ToObject <ExecuteRequest>(); var code = executeRequest.Code; var workspace = new Workspace( files: new[] { new File("Program.cs", Scaffold()) }, buffers: new[] { new Buffer(new BufferId("Program.cs", "main"), code), }, workspaceType: "console"); var workspaceRequest = new WorkspaceRequest(workspace); var server = new WorkspaceServerMultiplexer(new PackageRegistry()); var result = await server.Run(workspaceRequest); var messageBuilder = delivery.Command.Builder; var ioPubChannel = delivery.Command.IoPubChannel; var serverChannel = delivery.Command.ServerChannel; if (!executeRequest.Silent) { _executionCount++; var executeInput = messageBuilder.CreateMessage( MessageTypeValues.ExecuteInput, new ExecuteInput { Code = code, ExecutionCount = _executionCount }, delivery.Command.Request.Header); ioPubChannel.Send(executeInput); } // execute result var output = string.Join("\n", result.Output); // executeResult data var executeResultData = new ExecuteResult() { Data = new JObject { { "text/html", output }, { "text/plain", output } }, Transient = transient, ExecutionCount = _executionCount }; var resultSucceeded = result.Succeeded && result.Exception == null; if (resultSucceeded) { // reply ok var executeReplyPayload = new ExecuteReplyOk { ExecutionCount = _executionCount }; // send to server var executeReply = messageBuilder.CreateMessage( MessageTypeValues.ExecuteReply, executeReplyPayload, delivery.Command.Request.Header); executeReply.Identifiers = delivery.Command.Request.Identifiers; serverChannel.Send(executeReply); } else { var errorContent = new Error { EName = string.IsNullOrWhiteSpace(result.Exception) ? "Compile Error" : "Unhandled Exception", EValue = output, Traceback = new List <string>() }; // reply Error var executeReplyPayload = new ExecuteReplyError(errorContent) { ExecutionCount = _executionCount }; // send to server var executeReply = messageBuilder.CreateMessage( MessageTypeValues.ExecuteReply, executeReplyPayload, delivery.Command.Request.Header); executeReply.Identifiers = delivery.Command.Request.Identifiers; serverChannel.Send(executeReply); if (!executeRequest.Silent) { // send on io var error = messageBuilder.CreateMessage( MessageTypeValues.Error, errorContent, delivery.Command.Request.Header); ioPubChannel.Send(error); // send on stderr var stdErr = new StdErrStream { Text = errorContent.EValue }; var stream = messageBuilder.CreateMessage( MessageTypeValues.Stream, stdErr, delivery.Command.Request.Header); ioPubChannel.Send(stream); } } if (!executeRequest.Silent && resultSucceeded) { // send on io var executeResultMessage = messageBuilder.CreateMessage( MessageTypeValues.ExecuteResult, executeResultData, delivery.Command.Request.Header); ioPubChannel.Send(executeResultMessage); } break; } return(delivery.Complete()); }
private async Task HandleExecuteRequest(ICommandDelivery <JupyterRequestContext> delivery) { var ioPubChannel = delivery.Command.IoPubChannel; var serverChannel = delivery.Command.ServerChannel; var transient = new Dictionary <string, object> { { "display_id", Guid.NewGuid().ToString() } }; var executeRequest = delivery.Command.GetRequestContent <ExecuteRequest>(); var code = executeRequest.Code; var workspace = CreateScaffoldWorkspace(code); var workspaceRequest = new WorkspaceRequest(workspace); var result = await _server.Run(workspaceRequest); if (!executeRequest.Silent) { _executionCount++; var executeInput = Message.Create( new ExecuteInput(code: code, executionCount: _executionCount), delivery.Command.Request.Header); ioPubChannel.Send(executeInput); } // execute result var output = string.Join("\n", result.Output); // executeResult data var executeResultData = new ExecuteResult( _executionCount, transient: transient, data: new Dictionary <string, object> { { "text/html", output }, { "text/plain", output } }); var resultSucceeded = result.Succeeded && result.Exception == null; if (resultSucceeded) { // reply ok var executeReplyPayload = new ExecuteReplyOk(executionCount: _executionCount); // send to server var executeReply = Message.CreateResponse( executeReplyPayload, delivery.Command.Request); serverChannel.Send(executeReply); } else { var errorContent = new Error( eName: string.IsNullOrWhiteSpace(result.Exception) ? "Compile Error" : "Unhandled Exception", eValue: output ); // reply Error var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount); // send to server var executeReply = Message.CreateResponse( executeReplyPayload, delivery.Command.Request); serverChannel.Send(executeReply); if (!executeRequest.Silent) { // send on io var error = Message.Create( errorContent, delivery.Command.Request.Header); ioPubChannel.Send(error); // send on stderr var stdErr = new StdErrStream(errorContent.EValue); var stream = Message.Create( stdErr, delivery.Command.Request.Header); ioPubChannel.Send(stream); } } if (!executeRequest.Silent && resultSucceeded) { // send on io var executeResultMessage = Message.Create( executeResultData, delivery.Command.Request.Header); ioPubChannel.Send(executeResultMessage); } }