Exemplo n.º 1
0
        [Fact] // NOTE: This test will fail when executed by NCrunch.
        public void CodeCanBeCompiledAndExecuted()
        {
            var classes = new Document("Classes", "public static void SayHello() { Console.WriteLine(\"Hello, world!\"); }");
            var content = new Document("Content", "SayHello(); return \"Done!\";");

            var command = new EvaluateCodeCommand
            {
                ClientId  = Guid.NewGuid().ToString("N"),
                Documents = new List <Document> {
                    content, classes
                },
                Name          = "Untitled",
                Submitted     = DateTime.UtcNow,
                TimeoutPeriod = TimeSpan.FromSeconds(30)
            };

            var sandbox  = new Sandbox();
            var compiler = new CSharpCompiler();

            var actual = compiler.Compile(command);

            Assert.NotNull(actual);

            var result = sandbox.Execute(actual, Timeout.InfiniteTimeSpan);

            Assert.NotNull(result);
            Assert.Equal("Hello, world!\r\n", result.ConsoleOutput);
            Assert.Equal("\"Done!\"", result.Result);
        }
Exemplo n.º 2
0
        private static void ProcessCommand(EvaluateCodeCommand cmd)
        {
            if (cmd == null)
            {
                return;
            }

            var timeInQueue = DateTime.UtcNow - cmd.Submitted;

            Logger.Info("Job received after {0:N3} seconds in queue.", timeInQueue.TotalSeconds);

            if (timeInQueue > cmd.TimeoutPeriod)
            {
                Logger.Warn("Job was in queue for longer than {0} seconds, skipping!", cmd.TimeoutPeriod.Seconds);
                return;
            }

            var startedOn = DateTime.UtcNow;
            var stopWatch = new Stopwatch();
            stopWatch.Start();

            var assembly = Compiler.Compile(cmd);

            ExecutionResult result;
            if (assembly == null)
            {
                result = new ExecutionResult
                {
                    Result = "[compiling of code failed]"
                };
            }
            else
            {
                using (var executor = new Sandbox())
                {
                    result = executor.Execute(assembly, cmd.TimeoutPeriod);
                }
            }

            stopWatch.Stop();
            var stoppedOn = DateTime.UtcNow;

            Logger.Info("Work completed in {0} milliseconds.", stopWatch.ElapsedMilliseconds);

            try
            {
                var response = new WorkerResult
                {
                    ExecutionId = cmd.ExecutionId,
                    ClientId = cmd.ClientId,
                    StartTime = startedOn,
                    StopTime = stoppedOn,
                    RunDuration = stopWatch.Elapsed,
                    ProcessorTime = result.ProcessorTime,
                    TotalMemoryAllocated = result.TotalMemoryAllocated,
                    ConsoleOutput = result.ConsoleOutput,
                    Result = result.Result
                };

                Bus.Instance.Publish(response);
            }
            catch (JsonSerializationException ex)
            {
                Logger.ErrorException("An error occurred while attempting to serialize the JSON result.", ex);
            }

            stopWatch.Reset();
        }