Exemplo n.º 1
0
        /// <summary>
        /// Pushes job to database if clients are available and online
        /// </summary>
        /// <param name="maxTries">Maximum tries for fallback behavior (lock removal)</param>
        /// <returns></returns>
        public Boolean InfuseRemote(int maxTries)
        {
            if (parameters.Count != 3)
            {
                LogAdd("Invalid parameter count");
                return(false);
            }

            if (!mdm.Connect())
            {
                LogAdd("Error connecting to database. Fallback to local infusion");
                return(InfuseLocal(maxTries));
            }
            //Program.TestDbClient(mdm);
            //Program.TestDbJob(mdm);

            if (mdm.checkDuplicate(parameters.ElementAt(0)))
            {
                LogAdd("Duplicate found in db");
                LogAdd("Path: " + parameters.ElementAt(0));
                LogAdd("Job not added to queue");
                return(false);
            }

            List <Client> availableClients = mdm.GetAvailableClientsOnlineOnly();

            if (availableClients.Count == 0)
            {
                LogAdd("No available Clients. Fallback to local infusion");
                return(InfuseLocal(maxTries));
            }

            int    highestPrio = -1;
            Client eligible    = null;

            foreach (Client client in availableClients)
            {
                if (client.Priority < 0)
                {
                    continue;
                }
                //Always set eligible client to highest priority with free jobs
                if (client.Priority > highestPrio)
                {
                    if ((mdm.GetClientJobCount(client) < client.MaximumJobs) || client.MaximumJobs == -1)
                    {
                        eligible    = client;
                        highestPrio = client.Priority;
                    }
                }
                //If priority is the same, only assign if current client's number of jobs is smaller than currently set eligible client
                else if (client.Priority == highestPrio)
                {
                    int count = mdm.GetClientJobCount(client);
                    if ((count < client.MaximumJobs) || client.MaximumJobs == -1)
                    {
                        if (eligible != null)
                        {
                            if (count < mdm.GetClientJobCount(eligible))
                            {
                                eligible = client;
                            }
                        }
                    }
                }
            }
            if (eligible != null)
            {
                return(PushJobToDatabase(eligible, maxTries));
            }
            List <Client> query = availableClients.Where(e => e.Name.Equals(Environment.MachineName)).ToList();
            Client        local = null;

            if (query.Count == 1)
            {
                local = query.First();
            }
            if (local != null)
            {
                LogAdd("No Eligible Client found. Pushing to local machine instead");
                return(PushJobToDatabase(local, maxTries));
            }
            else
            {
                LogAdd("No Eligible Client found. Fallback to local infusion.");
                return(InfuseLocal(maxTries));
            }
        }