Beispiel #1
0
        public Node(string configFilename = DistributionCommon.Constants.DistributionNode.Node.ConfigFilename)
        {
            string[] dependencies = { configFilename };
            if (new DistributionCommon.DependencyManager(dependencies).FindMissing().Count != 0)
            {
                throw new DistributionCommon.DistributionControlException("Configuration file not found.");
            }

            try
            {
                this.config = DistributionCommon.JSONFileReader.GetObject <Config>(configFilename);
            }
            catch (JsonException)
            {
                throw new DistributionCommon.DistributionControlException("Configuration file invalid.");
            }

            if (this.config != default(Config))
            {
                this.logger = new DistributionCommon.Logger(this.config.LogFilename, this.config.Verbose);
                this.logger.Log("Starting up node...");
                this.constructed = false;
                this.workers     = new Dictionary <int, DistributedWorker.Worker>();
                try
                {
                    this.logger.Log("Initializing listener...");
                    this.listener = new NetListener(this.config.Port, this.RequestSifter, this.logger.Log);
                    this.logger.Log("Listening on port:" + this.config.Port.ToString());
                    this.logger.Log("Startup complete");
                    var task = Task.Run(async() => { await this.listener.StartListener(); });
                    task.Wait();
                }
                catch (Exception e)
                {
                    if (!this.config.LiveErrors)
                    {
                        this.logger.Log(e.StackTrace, 3);
                        Environment.Exit(1);
                    }

                    throw;
                }
            }
            else
            {
                throw new DistributionCommon.DistributionControlException("Configuration file invalid.");
            }
        }
        public Controller(string configFilename = DistributionCommon.Constants.DistributionController.Controller.ConfigFilename)
        {
            {
                string[] dependencies = { configFilename };
                if (new DistributionCommon.DependencyManager(dependencies).FindMissing().Count != 0)
                {
                    throw new DistributionCommon.DistributionControlException("Configuration file not found.");
                }

                try
                {
                    this.config = DistributionCommon.JSONFileReader.GetObject <Config>(configFilename);
                }
                catch (Newtonsoft.Json.JsonException)
                {
                    throw new DistributionCommon.DistributionControlException("Configuration file invalid.");
                }

                if (this.config == default(Config))
                {
                    throw new DistributionCommon.DistributionControlException("Configuration file invalid.");
                }
            }

            {
                this.logger = new DistributionCommon.Logger(this.config.LogFilename, this.config.Verbose);
                this.logger.Log("Starting up controller...");
            }

            bool preloadPossible = true;

            {
                var dependencies = new List <string>();
                dependencies.Add(this.config.SchematicFilename);
                if (this.config.PreLoad)
                {
                    dependencies.Add(this.config.PreLoadFilename);
                }

                var missingFiles = new DistributionCommon.DependencyManager(dependencies.ToArray()).FindMissing();

                if (missingFiles.Contains(this.config.SchematicFilename))
                {
                    this.logger.Log("Schematic file not found.", 3);
                    throw new DistributionCommon.DistributionControlException("Schematic file not found.");
                }

                if (missingFiles.Contains(this.config.PreLoadFilename))
                {
                    this.logger.Log("Pre-load file not found.", 1);
                    preloadPossible = false;
                }
            }

            {
                this.schematic = DistributionCommon.JSONFileReader.GetObject <DistributionCommon.Schematic.Schematic>(this.config.SchematicFilename);
                this.nodes     = new Dictionary <int, Node>();
                foreach (var node in this.schematic.Nodes)
                {
                    this.AddNode(node.Value);
                }

                if (this.nodes.Count == 0)
                {
                    this.logger.Log("All nodes failed to initialize.", 3);
                    throw new DistributionCommon.DistributionControlException("All nodes failed to initialize.");
                }
            }

            {
                if (this.config.PreLoad && preloadPossible)
                {
                    this.logger.Log("Beginning job pre-load.");
                    this.jobs = DistributionCommon.JSONFileReader.GetObject <Dictionary <int, Job> >(this.config.PreLoadFilename);
                    var unsuccessful = new List <int>();
                    foreach (var job in this.jobs.Where(job => job.Value.NodeID != 0))
                    {
                        try
                        {
                            if (!this.AssignJobManual(job.Value, job.Value.NodeID))
                            {
                                this.logger.Log(string.Format("Failed to load job ID:{0}", job.Key), 1);
                                unsuccessful.Add(job.Key);
                            }
                        }
                        catch (KeyNotFoundException)
                        {
                            this.logger.Log(string.Format("Failed to load job ID:{0}", job.Key), 1);
                            unsuccessful.Add(job.Key);
                        }
                    }

                    foreach (int job in unsuccessful)
                    {
                        if (this.config.AutoAssignFailedPreLoadJobs)
                        {
                            this.jobs[job].Transfer(0);
                        }
                        else
                        {
                            this.jobs.Remove(job);
                        }
                    }

                    this.logger.Log("Beginning automatic assignment.");
                    var unassigned = this.jobs.Values.Where(job => job.NodeID == 0).Select(job => job.Blueprint.ID).Where(job => this.jobs[job].Balanced).ToList();
                    var remaining  = this.AssignJobsBalanced(unassigned);
                    var assigned   = unassigned.Where(job => !remaining.Any(rem => rem == job));
                    foreach (int job in assigned)
                    {
                        this.logger.Log("Loaded job ID:" + job);
                    }

                    foreach (int job in remaining)
                    {
                        this.logger.Log("Failed to load job ID:" + job, 1);
                    }

                    this.logger.Log("Job pre-load completed.");
                }
                else
                {
                    this.jobs = new Dictionary <int, Job>();
                }
            }

            {
                if (this.config.LoadBalancing)
                {
                    this.DistributionModification += this.BalanceAllJobs;
                }
            }

            this.DistributionModification += this.ModificationHandler;
            this.statusChanged             = false;
            this.logger.Log("Startup completed.");
        }