Exemplo n.º 1
0
        private ExecutionResult Execute(string className, string resultProperty)
        {
            var result = new ExecutionResult();
            var type = typeof(ByteCodeLoader);
            var formatter = new ObjectFormatter(maxLineLength: 5120);
            var domain = AppDomain.CurrentDomain;

            try
            {
                var totalProcessorTime = domain.MonitoringTotalProcessorTime;
                var totalAllocatedMemorySize = domain.MonitoringTotalAllocatedMemorySize;

                var loader = (ByteCodeLoader)domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
                var unformattedResult = loader.Run(className, resultProperty, _assemblyBytes);

                result.Result = formatter.FormatObject(unformattedResult.ReturnValue);
                result.ConsoleOutput = unformattedResult.ConsoleOutput;
                result.ProcessorTime = domain.MonitoringTotalProcessorTime - totalProcessorTime;
                result.TotalMemoryAllocated = domain.MonitoringTotalAllocatedMemorySize - totalAllocatedMemorySize;
            }
            catch (SerializationException ex)
            {
                result.Result = ex.Message;
            }
            catch (TargetInvocationException ex)
            {
                result.Result = ex.InnerException.ToString();
            }

            return result;
        }
Exemplo n.º 2
0
        private static string FormatResult(object input)
        {
            try
            {
                var formatter = new ObjectFormatter(maxLineLength: 350);
                var result = formatter.FormatObject(input);

                if (string.IsNullOrEmpty(result)) return "null";

                result = result.Replace(Environment.NewLine, " ").Replace("\n", " ").Replace("\r", " ");

                if (result.Length > 350) result = result.Substring(0, 350);

                return result;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
Exemplo n.º 3
0
        private static void ProcessQueue()
        {
            Logger.Debug("ProcessQueue task {0} started.", Task.CurrentId);

            var stopWatch = new Stopwatch();
            var formatter = new ObjectFormatter(maxLineLength: 5120);

            using (var connection = CreateOpenRedisConnection())
            using (var client = connection.GetClient()) {

                while (true)
                {
                    var message = client.BlockingDequeueItemFromList("queue:execute", null);

                    if (TokenSource.IsCancellationRequested)
                    {
                        Logger.Error("ProcessQueue task cancelled.");
                        break;
                    }

                    if (message != null)
                    {
                        Logger.Debug("Message received.");

                        var messageBytes = Convert.FromBase64String(message);

                        var command = ExecuteCommand.Deserialize(messageBytes);

                        Logger.Info("Executing: {0}", command.Code ?? string.Empty);

                        stopWatch.Start();
                        var result = Executer.Execute(command.Code);
                        stopWatch.Stop();

                        Logger.Info("Executed: {0}", command.Code ?? string.Empty);

                        var response = JsonConvert.SerializeObject(new {
                            code = command.Code,
                            result = formatter.FormatObject(result),
                            time = DateTime.UtcNow,
                            duration = stopWatch.ElapsedMilliseconds
                        });

                        var listeners = client.PublishMessage("workers:job-done:" + command.ClientId, response);

                        Logger.Debug("Response published to " + listeners + " listeners.");

                        stopWatch.Reset();
                    }
                }

                Logger.Debug("ProcessQueue task ending.");
            }
        }
Exemplo n.º 4
0
        private static void ProcessQueue(RedisConnection connection, string[] queues)
        {
            var stopWatch = new Stopwatch();
            var formatter = new ObjectFormatter(maxLineLength: 5120);

            Logger.Info("ProcessQueue started.");

            while (true)
            {
                var message = connection.Lists.BlockingRemoveFirst(0, queues, DefaultTimeout);

                if (message.Result == null)
                {
                    continue;
                }

                var command = ExecuteCommand.Deserialize(message.Result.Item2);

                var timeInQueue = DateTime.UtcNow - command.Submitted;

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

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

                stopWatch.Start();

                var result = Executer.Execute(command.Code, command.Classes);

                stopWatch.Stop();

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

                try
                {
                    var response = JsonConvert.SerializeObject(new
                                   {
                                       code = command.Code,
                                       classes = command.Classes,
                                       result = formatter.FormatObject(result),
                                       time = DateTime.UtcNow,
                                       duration = stopWatch.ElapsedMilliseconds
                                   });

                    var bytes = Encoding.UTF8.GetBytes(response);
                    var listeners = connection.Publish("workers:job-done:" + command.ClientId, bytes);

                    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();
            }
        }
Exemplo n.º 5
0
        private static void ProcessQueue()
        {
            Logger.Info("ProcessQueue task {0} started.", Task.CurrentId);

            var stopWatch = new Stopwatch();
            var formatter = new ObjectFormatter(maxLineLength: 5120);

            using (var connection = OpenConnection())
            {
                connection.Error += (sender, e) => Logger.ErrorException(e.Cause, e.Exception);

                connection.Wait(connection.Open());

                while (!TokenSource.IsCancellationRequested)
                {
                    var message = connection.Lists.BlockingRemoveFirst(0, new[] { "queue:execute" }, Int32.MaxValue);

                    var command = ExecuteCommand.Deserialize(message.Result.Item2);

                    stopWatch.Start();

                    var result = Executer.Execute(command.Code, command.Classes);

                    stopWatch.Stop();

                    var response = JsonConvert.SerializeObject(new
                                   {
                                       code = command.Code,
                                       classes = command.Classes,
                                       result = formatter.FormatObject(result),
                                       time = DateTime.UtcNow,
                                       duration = stopWatch.ElapsedMilliseconds
                                   });

                    var responseBytes = Encoding.UTF8.GetBytes(response);

                    connection.Wait(connection.Publish("workers:job-done:" + command.ClientId, responseBytes));

                    stopWatch.Reset();
                }

                Logger.Error("ProcessQueue task {0} cancelled.", Task.CurrentId);
            }
        }