Exemplo n.º 1
0
 public void TextFixtureSetUp()
 {
     _workerNode = new WorkerNode
     {
         Url = new Uri("http://localhost:9050/")
     };
 }
Exemplo n.º 2
0
        public IActionResult GetAIChessWorker()
        {
            List <WorkerNode> workerNodes = _context.WorkerNodes.ToList();

            WorkerNode freeworkerNode = null;
            int        minActiveTasks = int.MaxValue;


            foreach (WorkerNode element in workerNodes)
            {
                using (var client = new HttpClient(new HttpClientHandler {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                }))
                {
                    client.BaseAddress = new Uri(element.Uri);
                    HttpResponseMessage response = client.GetAsync("/Worker/ActiveThreads").Result;
                    int activethreads            = Convert.ToInt32(response.Content.ReadAsStringAsync().Result);

                    if (response.IsSuccessStatusCode && minActiveTasks > activethreads)
                    {
                        freeworkerNode = element;
                        minActiveTasks = activethreads;
                    }
                }
            }


            return(Ok(freeworkerNode.Uri + "/Worker/Start"));
        }
Exemplo n.º 3
0
        public void ShouldSendTheJobToAnotherNodeIfFirstReturnsConflict()
        {
            var jobQueueItem = new JobQueueItem
            {
                Name       = "Name Test",
                CreatedBy  = "Created By Test",
                Serialized = "Serialized Test",
                Type       = "Type Test"
            };

            var workerNode2 = new WorkerNode
            {
                Url = new Uri("http://localhost:9051/")
            };

            NodeRepository.AddWorkerNode(_workerNode);
            Thread.Sleep(TimeSpan.FromSeconds(1));
            NodeRepository.AddWorkerNode(workerNode2);
            ThisNodeIsBusy(workerNode2.Url);

            ManagerController.AddItemToJobQueue(jobQueueItem);

            while (true)
            {
                if (JobRepository.GetAllJobs().Count == 1)
                {
                    break;
                }
            }

            JobRepository.GetAllJobs().First().SentToWorkerNodeUri.Should().Be.EqualTo(_workerNode.Url.ToString());
        }
Exemplo n.º 4
0
        public IList <WorkerNode> GetAllNodes()
        {
            var          nodes         = new List <WorkerNode>();
            const string commandString = @"SELECT * FROM [Stardust].[WorkerNode]";

            using (var sqlConnection = new SqlConnection(_managerConfiguration.ConnectionString))
            {
                sqlConnection.Open();
                using (var workerNodeCommand = new SqlCommand(commandString, sqlConnection))
                {
                    using (var sqlDataReader = workerNodeCommand.ExecuteReader())
                    {
                        if (!sqlDataReader.HasRows)
                        {
                            return(nodes);
                        }
                        while (sqlDataReader.Read())
                        {
                            var node = new WorkerNode
                            {
                                Url       = new Uri(sqlDataReader.GetString(sqlDataReader.GetOrdinal("Url"))),
                                Id        = sqlDataReader.GetGuid(sqlDataReader.GetOrdinal("Id")),
                                Heartbeat = sqlDataReader.GetDateTime(sqlDataReader.GetOrdinal("Heartbeat")),
                                Alive     = sqlDataReader.GetBoolean(sqlDataReader.GetOrdinal("Alive"))
                            };
                            nodes.Add(node);
                        }
                    }
                }
            }
            return(nodes);
        }
Exemplo n.º 5
0
 public WorkerNodeHostedService(ILogger <WorkerNodeHostedService> logger
                                , WorkerNode workerNode
                                , string serviceName)
 {
     _serviceName = serviceName;
     _workerNode  = workerNode;
     _logger      = logger;
 }
Exemplo n.º 6
0
 public WorkerNodeHostedService(ILogger <WorkerNodeHostedService> logger
                                , WorkerNode workerNode
                                , IServiceInfo serviceInfo)
 {
     _serviceName = serviceInfo.ShortName;
     _workerNode  = workerNode;
     _logger      = logger;
 }
Exemplo n.º 7
0
        public void AddWorkerNode(Uri workerNodeUri)
        {
            var node = new WorkerNode
            {
                Url = workerNodeUri
            };

            _nodeRepository.AddWorkerNode(node);
        }
Exemplo n.º 8
0
        public void TextFixtureSetUp()
        {
            _workerNode = new WorkerNode
            {
                Url = new Uri("http://localhost:9050/")
            };
            _workerNode2 = new WorkerNode
            {
                Url = new Uri("http://localhost:9051/")
            };

            var configurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

            XmlConfigurator.ConfigureAndWatch(new FileInfo(configurationFile));
        }
Exemplo n.º 9
0
        public void AddWorkerNode(WorkerNode workerNode)
        {
            const string selectWorkerNodeCommand = "INSERT INTO [Stardust].[WorkerNode] (Id, Url, Heartbeat, Alive) VALUES(@Id, @Url, @Heartbeat, @Alive)";

            try
            {
                using (var connection = new SqlConnection(_connectionString))
                {
                    connection.OpenWithRetry(_retryPolicy);

                    using (var workerNodeCommand = new SqlCommand(selectWorkerNodeCommand, connection))
                    {
                        workerNodeCommand.Parameters.AddWithValue("@Id", workerNode.Id);
                        workerNodeCommand.Parameters.AddWithValue("@Url", workerNode.Url.ToString());
                        workerNodeCommand.Parameters.AddWithValue("@Heartbeat", workerNode.Heartbeat);
                        workerNodeCommand.Parameters.AddWithValue("@Alive", workerNode.Alive);

                        workerNodeCommand.ExecuteNonQueryWithRetry(_retryPolicy);
                    }
                }
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UQ_WorkerNodes_Url"))
                {
                    using (var connection = new SqlConnection(_connectionString))
                    {
                        connection.OpenWithRetry(_retryPolicy);
                        const string updateCommandText = @"UPDATE [Stardust].[WorkerNode] SET Heartbeat = @Heartbeat,
											Alive = @Alive
											WHERE Url = @Url"                                            ;

                        using (var command = new SqlCommand(updateCommandText, connection))
                        {
                            command.Parameters.Add("@Heartbeat", SqlDbType.DateTime).Value = DateTime.UtcNow;
                            command.Parameters.Add("@Alive", SqlDbType.Bit).Value          = true;
                            command.Parameters.Add("@Url", SqlDbType.NVarChar).Value       = workerNode.Url.ToString();

                            command.ExecuteNonQueryWithRetry(_retryPolicy);
                        }
                    }
                    return;
                }

                this.Log().ErrorWithLineNumber(exp.Message, exp);
                throw;
            }
        }
Exemplo n.º 10
0
 public void AddWorkerNode(WorkerNode workerNode)
 {
     _workerNodes.Add(workerNode);
 }
Exemplo n.º 11
0
        public void Load()
        {
            if (IsDisposed)
            {
                return;
            }

            if (IsLoaded)
            {
                throw new Exception("Foreman already loaded");
            }

            if (IsRunning)
            {
                return;
            }

            if (ConfigString == null && Config == null)
            {
                throw new ArgumentNullException("ConfigString");
            }

            if (ContractorSettings == null)
            {
                ContractorSettings = new ContractorSettings();
            }

            // load config file only if not already defined by Contractor
            if (Config == null)
            {
                Config = ParseAndLoadConfigString(Id, ConfigString, ContractorSettings);
            }

            int NodeCounter  = 0;
            int QueueCounter = 0;

            IsNodesLongRunning = Config.isNodesLongRunning;

            // register assembly

            asm = Assembly.LoadFile(Config.assemblyPath);

            // loading an app domain solves the referenced assemblies problem?

            /*
             * AssemblyName[] ReferencedAssemblies = asm.GetReferencedAssemblies();
             * foreach (var refAsm in ReferencedAssemblies)
             *  if (refAsm != null)
             *      Assembly.Load(refAsm);
             */

            // Register nodes
            if (Config.nodes == null || Config.nodes.Count == 0)
            {
                throw new ArgumentException("No nodes in config file");
            }

            nodes = new WorkerNode[Config.nodes.Count];
            workerNodeExeOrder = new Dictionary <int, List <WorkerNode> >();
            nodeNameToId       = new Dictionary <string, int>(Config.nodes.Count);
            foreach (var configNode in Config.nodes)
            {
                var node = new WorkerNode();
                node.Id              = NodeCounter;
                node.OrderId         = configNode.exeOrderId;
                node.Name            = configNode.name;
                node.WorkerClassName = configNode.className;

                var workerType = asm.GetTypes().First(x => x.FullName.Equals(configNode.className));

                if (workerType == null)
                {
                    throw new Exception("Type not found: " + configNode.className);
                }

                var worker = (WorkerBase)Activator.CreateInstance(workerType);

                if (worker == null)
                {
                    throw new Exception("Can't create instance from" + configNode.className);
                }

                node.Worker = worker;

                if (!workerNodeExeOrder.ContainsKey(node.OrderId))
                {
                    workerNodeExeOrder.Add(node.OrderId, new List <WorkerNode>()
                    {
                        node
                    });
                }
                else
                {
                    workerNodeExeOrder[node.OrderId].Add(node);
                }

                node.State         = WorkerNodeState.Idle;
                nodes[NodeCounter] = node;
                nodeNameToId.Add(node.Name, node.Id);

                NodeCounter++;
            }

            // Register queues
            if (Config.queues == null || Config.queues.Count == 0)
            {
                Config.queues = new List <FCFQueue>();
            }

            if (Config.queues.Count > 0 && !IsNodesLongRunning)
            {
                throw new Exception("Can't define queues in short running foremen");
            }

            queues        = new BlockingCollection <object> [Config.queues.Count];
            queueNameToId = new Dictionary <string, int>(Config.queues.Count);
            //queueIsToEl = new bool[config.queues.Count];
            //queueIsFromEl = new bool[config.queues.Count];
            foreach (var configQueue in Config.queues)
            {
                if (queueNameToId.ContainsKey(configQueue.name))
                {
                    string err = "The queue name '" + configQueue.name + "' is already registered";
                    throw new ArgumentException(err);
                }

                int queueId = QueueCounter;

                if (configQueue.bufferLimit == 0)
                {
                    queues[queueId] = new BlockingCollection <object>();
                }
                else
                {
                    queues[queueId] = new BlockingCollection <object>(configQueue.bufferLimit);
                }

                queueNameToId.Add(configQueue.name, queueId);

                QueueCounter++;
            }

            // Register connections
            if (Config.connections == null)
            {
                Config.connections = new List <FCFConnection>();
            }

            foreach (var configConnection in Config.connections)
            {
                string fromName = configConnection.from;
                string toName   = configConnection.to;

                int fromElId, toElId;

                TopologyElementType fromEl = GetTopologyTypeByName(fromName, out fromElId);
                TopologyElementType toEl   = GetTopologyTypeByName(toName, out toElId);

                if (fromEl == TopologyElementType.None && toEl == TopologyElementType.None)
                {
                    string err = "Connection from and to elements do not exist: '" + fromName + "' -> '" + toName + "'";
                    throw new Exception(err);
                }

                // node to node, node to queue and queue to node are supported
                // queue to queue is not supported
                if (fromEl == TopologyElementType.Queue && toEl == TopologyElementType.Queue)
                {
                    string err = "Can't connect a queue to a queue: '" + fromName + "' -> '" + toName + "'";
                    throw new Exception(err);
                }

                if (fromEl == TopologyElementType.Node && toEl == TopologyElementType.Queue)
                {
                    var node  = nodes[fromElId];
                    var queue = queues[toElId];

                    if (node.Output != null)
                    {
                        string err = "Can't set two output elements for same node: '" + fromName + "' -> '" + toName + "'";
                        throw new Exception();
                    }

                    node.Output      = queue;
                    node.IsConnected = true;
                    //queueIsToEl[toElId] = true;
                }

                if (fromEl == TopologyElementType.Queue && toEl == TopologyElementType.Node)
                {
                    var node  = nodes[toElId];
                    var queue = queues[fromElId];

                    if (node.Input != null)
                    {
                        string err = "Can't set two input elements for the same node: '" + fromName + "' -> '" + toName + "'";
                        throw new Exception(err);
                    }

                    node.Input       = queue;
                    node.IsConnected = true;
                    //queueIsFromEl[fromElId] = true;
                }

                if (fromEl == TopologyElementType.Node && toEl == TopologyElementType.Node)
                {
                    var node1 = nodes[fromElId];
                    node1.IsConnected = true;

                    var node2 = nodes[toElId];
                    node2.IsConnected = true;

                    if (!IsNodesLongRunning)
                    {
                        node1.NextNode = node2;
                    }
                }
            }

            // a single node may not be connected
            if (nodes.Length == 1)
            {
                nodes[0].IsConnected = true;
            }

            // iterate over all tree and check if there are any unconnected nodes
            foreach (var node in nodes)
            {
                if (!node.IsConnected)
                {
                    string err = "Node is not connected to topology tree: '" + node.Name + "'";
                    throw new Exception(err);
                }
            }

            /*
             *
             * // queues CAN be an edge
             *
             * if (queues != null)
             * {
             *  // check a queue is not an edge
             *  for (var i = 0; i < queues.Length; i++)
             *  {
             *      if (!queueIsFromEl[i] || !queueIsToEl[i])
             *      {
             *          string err = "A queue cannot be an edge, but must connect a node as input and another node as output";
             *          throw new Exception(err);
             *      }
             *  }
             * }
             */

            // several independent topologies can coexist in a single foreman

            // dispose of helpers
            //queueIsFromEl = null;
            //queueIsToEl = null;
            nodeNameToId = null;

            IsLoaded = true;
        }