Exemple #1
0
        private async Task HandleCancelCurrentCommand(
            CancelCurrentCommand cancelCurrentCommand,
            KernelInvocationContext context)
        {
            var reply = new CurrentCommandCancelled(cancelCurrentCommand);

            lock (_cancellationSourceLock)
            {
                _cancellationSource.Cancel();
                _cancellationSource = new CancellationTokenSource();
            }

            context.Publish(reply);
            context.Complete();
        }
Exemple #2
0
        private async Task HandleSubmitCode(
            SubmitCode submitCode,
            KernelInvocationContext context)
        {
            CancellationTokenSource cancellationSource;

            lock (_cancellationSourceLock)
            {
                cancellationSource = _cancellationSource;
            }
            var codeSubmissionReceived = new CodeSubmissionReceived(
                submitCode.Code,
                submitCode);

            context.Publish(codeSubmissionReceived);

            var code       = submitCode.Code;
            var isComplete = await IsCompleteSubmissionAsync(submitCode.Code);

            if (isComplete)
            {
                context.Publish(new CompleteCodeSubmissionReceived(submitCode));
            }
            else
            {
                context.Publish(new IncompleteCodeSubmissionReceived(submitCode));
            }

            if (submitCode.SubmissionType == SubmissionType.Diagnose)
            {
                return;
            }

            Exception exception = null;

            using var console = await ConsoleOutput.Capture();

            using (console.SubscribeToStandardOutput(std => PublishOutput(std, context, submitCode)))
                using (console.SubscribeToStandardError(std => PublishError(std, context, submitCode)))
                {
                    if (!cancellationSource.IsCancellationRequested)
                    {
                        try
                        {
                            if (ScriptState == null)
                            {
                                ScriptState = await CSharpScript.RunAsync(
                                    code,
                                    ScriptOptions,
                                    cancellationToken : cancellationSource.Token)
                                              .UntilCancelled(cancellationSource.Token);
                            }
                            else
                            {
                                ScriptState = await ScriptState.ContinueWithAsync(
                                    code,
                                    ScriptOptions,
                                    e =>
                                {
                                    exception = e;
                                    return(true);
                                },
                                    cancellationToken : cancellationSource.Token)
                                              .UntilCancelled(cancellationSource.Token);
                            }
                        }
                        catch (CompilationErrorException cpe)
                        {
                            exception = new CodeSubmissionCompilationErrorException(cpe);
                        }
                        catch (Exception e)
                        {
                            exception = e;
                        }
                    }
                }

            if (!cancellationSource.IsCancellationRequested)
            {
                if (exception != null)
                {
                    string message = null;

                    if (exception is CodeSubmissionCompilationErrorException compilationError)
                    {
                        message =
                            string.Join(Environment.NewLine,
                                        (compilationError.InnerException as CompilationErrorException)?.Diagnostics.Select(d => d.ToString()) ?? Enumerable.Empty <string>());
                    }

                    context.Publish(new CommandFailed(exception, submitCode, message));
                }
                else
                {
                    if (ScriptState != null && HasReturnValue)
                    {
                        var formattedValues = FormattedValue.FromObject(ScriptState.ReturnValue);
                        context.Publish(
                            new ReturnValueProduced(
                                ScriptState.ReturnValue,
                                submitCode,
                                formattedValues));
                    }

                    context.Complete();
                }
            }
            else
            {
                context.Publish(new CommandFailed(null, submitCode, "Command cancelled"));
            }
        }