Example #1
0
        private ExecutionResult Execute(object assemblyInput)
        {
            var assembly = (ICodeAssembly)assemblyInput;
            var result = new ExecutionResult();
            var type = typeof(ByteCodeLoader);
            var formatter = ObjectFormatter.Instance;
            var formattingOptions = new ObjectFormattingOptions(maxOutputLength: 5120);

            var className = assembly.EntryPointClassName;
            var methodName = assembly.EntryPointMethodName;
            var resultProperty = "Result";
            var assemblyBytes = assembly.CompiledAssembly;

            try
            {
                var handle = Activator.CreateInstanceFrom(domain, type.Assembly.ManifestModule.FullyQualifiedName, type.FullName);
                var loader = (ByteCodeLoader)handle.Unwrap();
                var unformattedResult = loader.Run(className, methodName, resultProperty, assemblyBytes);

                result.Result = (unformattedResult != null) ? formatter.FormatObject(unformattedResult.ReturnValue, formattingOptions) : "null";
                result.ConsoleOutput = (unformattedResult != null) ? unformattedResult.ConsoleOutput : string.Empty;
                result.ProcessorTime = domain.MonitoringTotalProcessorTime;
                result.TotalMemoryAllocated = domain.MonitoringTotalAllocatedMemorySize;
            }
            catch (SerializationException ex)
            {
                result.Result = ex.Message;
            }
            catch (TargetInvocationException ex)
            {
                result.Result = ex.InnerException.ToString();
            }

            return result;
        }
Example #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();
        }
Example #3
0
        private ExecutionResult Execute(string className, string resultProperty)
        {
            var result = new ExecutionResult();
            var type = typeof(ByteCodeLoader);
            var formatter = new ObjectFormatter(maxLineLength: 5120);

            try
            {
                var loader = (ByteCodeLoader)domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);

                var unformattedResult = loader.Run(className, resultProperty, assemblyBytes);

                result.Result = (unformattedResult != null) ? formatter.FormatObject(unformattedResult.ReturnValue) : "null";
                result.ConsoleOutput = (unformattedResult != null) ? unformattedResult.ConsoleOutput : string.Empty;
                result.ProcessorTime = domain.MonitoringTotalProcessorTime;
                result.TotalMemoryAllocated = domain.MonitoringTotalAllocatedMemorySize;
            }
            catch (SerializationException ex)
            {
                result.Result = ex.Message;
            }
            catch (TargetInvocationException ex)
            {
                result.Result = ex.InnerException.ToString();
            }

            return result;
        }
Example #4
0
        private static void ProcessCommand(EvaluateCodeCommand cmd)
        {
            if (cmd == null)
            {
                return;
            }

            var now = DateTimeOffset.UtcNow;
            var timeInQueue = now - cmd.Submitted;

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

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

            var startedOn = DateTimeOffset.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, TimeSpan.FromSeconds(5));
                }
            }

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

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

            var response = new WorkerResult
            {
                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);

            stopWatch.Reset();
        }