Example #1
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 #2
0
        private static void ProcessQueue(string[] queues)
        {
            var stopWatch = new Stopwatch();

            Logger.Info("ProcessQueue started.");

            while (true)
            {
                var cmd = WaitForCommandFromQueue(queues);

                if (cmd == null)
                {
                    continue;
                }

                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);
                    continue;
                }

                var post = new Post { Content = cmd.Code, Classes = cmd.Classes };

                stopWatch.Start();

                var result = Executer.Execute(post);

                stopWatch.Stop();

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

                try
                {
                    var response = new WorkerResult
                                   {
                                       // code = cmd.Code,
                                       // classes = cmd.Classes,
                                       Time = DateTime.UtcNow,
                                       Duration = stopWatch.ElapsedMilliseconds,
                                       ExecutionResult = result
                                   };

                    var listeners = PublishToClient(cmd.ClientId, response.GetBytes());

                    Logger.Info("Work results published to {0} listeners.", listeners.Result);
                }
                catch (JsonSerializationException ex)
                {
                    Logger.ErrorException("An error occurred while attempting to serialize the JSON result.", ex);
                }

                stopWatch.Reset();
            }
        }