Ejemplo n.º 1
0
        private void Loop()
        {
            const string unknownName = "unknown";
            var          executor    = new PlanExecutor(this.plansDir);

            while (!stop)
            {
                try
                {
                    foreach (var file in Directory.GetFiles(this.plansDir, "*.json.result")
                             .Where(file => !File.Exists(Path.Combine(this.plansDir, Path.GetFileNameWithoutExtension(file)))))
                    {
                        var id = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(file));
                        if (id.Equals(unknownName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            id = "";
                        }

                        var result = File.ReadAllText(file);
                        log.Info("Sending results for {0}", id);
                        messageSource.SendResult(new Message {
                            Body = result, Id = id
                        });
                        File.Delete(file);
                    }

                    var path = Directory.EnumerateFiles(this.plansDir, "*.json").FirstOrDefault();
                    if (path == null)
                    {
                        using (var message = messageSource.GetMessage())
                        {
                            if (message == null)
                            {
                                return;
                            }
                            var id = message.Id;
                            if (string.IsNullOrEmpty(id))
                            {
                                id = unknownName;
                            }

                            path = Path.Combine(this.plansDir, string.Format("{0}.json", id));
                            File.WriteAllText(path, message.Body);
                            log.Info("Received new execution plan {0}", id);
                        }
                    }
                    else
                    {
                        var id = Path.GetFileNameWithoutExtension(path);
                        log.Info("Executing exising plan {0}", id);
                    }

                    executor.Execute(path);
                    File.Delete(path);
                    delayFactor = 1;

                    if (stop)
                    {
                        break;
                    }
                    if (executor.RebootNeeded)
                    {
                        Reboot();
                    }
                }
                catch (Exception exception)
                {
                    WaitOnException(exception);
                }
            }
        }
Ejemplo n.º 2
0
        void Loop()
        {
            const string unknownName = "unknown";
            while (!stop)
            {
                try
                {
                    foreach (var file in Directory.GetFiles(this.plansDir, "*.json.result")
                        .Where(file => !File.Exists(Path.Combine(this.plansDir, Path.GetFileNameWithoutExtension(file)))))
                    {
                        var id = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(file)) ?? unknownName;
                        if (id.Equals(unknownName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            id = "";
                        }

                        var result = File.ReadAllText(file);
                        Log.Info("Sending results for {0}", id ?? unknownName);
                        rabbitMqClient.SendResult(new MqMessage { Body = result, Id =  id });
                        File.Delete(file);
                    }

                    var path = Directory.EnumerateFiles(this.plansDir, "*.json").FirstOrDefault();
                    if (path == null)
                    {
                        var message = rabbitMqClient.GetMessage();
                        var id = message.Id;
                        if(string.IsNullOrEmpty(id))
                        {
                            id = unknownName;
                        }

                        path = Path.Combine(this.plansDir, string.Format("{0}.json", id));
                        File.WriteAllText(path, message.Body);
                        Log.Info("Received new execution plan {0}", id);
                        message.Ack();
                    }
                    else
                    {
                        var id = Path.GetFileNameWithoutExtension(path);
                        Log.Info("Executing exising plan {0}", id);
                    }
                    var executor = new PlanExecutor(path);
                    executor.Execute();
                    File.Delete(path);
                    delayFactor = 1;

                    if (stop) break;
                    if (executor.RebootNeeded)
                    {
                        Reboot();
                    }
                }
                catch (Exception exception)
                {
                    WaitOnException(exception);
                }

            }
        }