public void Test_EmptyJob()
        {
            var agentId = Guid.NewGuid();
            var agentFriendlyName = "TestAgent1"; 

            var jobDescription = new JobDescription 
            {
                Id = Guid.NewGuid(),
                FriendlyName = "P.PRO TEST JOB",
                CommandArray = new [] {
                    new JobCommandDescription {
                        PrepareCommand = "",
                        RunCommand = "",
                        CleanUpCommand = "", 
                    }
                }
            };

            var jobInstance = new JobInstance 
            { 
                AgentId = agentId,
                CurrentState = JobInstance.State.NotYetStarted,
                Id = Guid.NewGuid(),
                JobDescriptionId = jobDescription.Id
            };

            var restProxy = new RestProxy("http://localhost:26679/testing/");
            restProxy.Create(jobDescription).Wait();
            restProxy.Create(jobInstance).Wait();

            new Daemon(agentId, agentFriendlyName, restProxy).Run(1);

            Thread.Sleep(1000);

            var jobInstanceAfter = restProxy.Get<JobInstance>(jobInstance.Id).Result;

            Assert.AreEqual(jobInstance.JobDescriptionId, jobInstanceAfter.JobDescriptionId);
            Assert.AreEqual(jobInstance.ResultDestination, jobInstanceAfter.ResultDestination);
            Assert.AreEqual(jobInstance.AgentId, jobInstanceAfter.AgentId);
            Assert.AreEqual(JobInstance.State.Finished, jobInstanceAfter.CurrentState);
        }
示例#2
0
        static void Main(string[] args)
        {
            var serverUrlEnvVar = Environment.GetEnvironmentVariable(ENV_P_PRO_SERVER_URL);
            String serverUrl;
            if (serverUrlEnvVar == null)
            {
                Console.WriteLine("Server URL not set via the {0} environment variable, using default ({1}).", ENV_P_PRO_SERVER_URL, DEFAULT_SERVER_URL);
                serverUrl = DEFAULT_SERVER_URL;
            }
            else
            {
                serverUrl = serverUrlEnvVar;
            }

            var random = new Random();
            var jobInstanceBlackList = new HashSet<Guid>();

            using (IRestProxy restProxy = new RestProxy(serverUrl))
            {
                while (true)
                {
                    JobInstance[] unassignedJobInstance;

                    try
                    {
                        unassignedJobInstance = restProxy.Query<JobInstance>("AgentId".Eq(Guid.Empty.ToString())).Result.ToArray();
                    }
                    catch (RestProxyException e)
                    {
                        Console.Error.WriteLine("HTTP error while trying to query for unassigned jobs: " + e.Message);
                        Console.Error.WriteLine("Trying again in a few seconds...");
                        Thread.Sleep(TimeSpan.FromSeconds(5));
                        continue;
                    }

                    foreach (var jobInstance in unassignedJobInstance)
                    {
                        if (jobInstanceBlackList.Contains(jobInstance.Id))
                            continue;

                        JobDescription jobDescription;

                        try { jobDescription = restProxy.Get<JobDescription>(jobInstance.JobDescriptionId).Result; }
                        catch (RestProxyException e)
                        {
                            if (e.StatusCode == HttpStatusCode.NotFound)
                            {
                                restProxy.Create(new JobInstanceLogEntry()
                                {
                                    Id = Guid.NewGuid(),
                                    JobInstanceId = jobInstance.Id,
                                    LocalTimeStamp = DateTime.UtcNow,
                                    Text = ""
                                });
                                Console.Error.WriteLine("Encountered invalid job instance: " + jobInstance.Id);
                                Console.Error.WriteLine("=> JobDescription not found.");
                                jobInstanceBlackList.Add(jobInstance.Id);
                            }
                            else
                            {
                                Console.Error.WriteLine("HTTP error while trying to retrieve job description for job instance: " + e.Message);
                            }

                            continue;
                        }

                        Agent[] agents = null;
                        while (agents == null)
                        {
                            try
                            {
                                agents = restProxy.GetAll<Agent>()
                                    .Result
                                    .Where(a => AgentMatchesJobRequirements(a, jobDescription))
                                    .ToArray();
                            }
                            catch (RestProxyException e)
                            {
                                Console.Error.WriteLine("HTTP error while trying to retrieve agent list: " + e.Message);
                                Console.Error.WriteLine("Retrying in a moment...");

                                Thread.Sleep(TimeSpan.FromSeconds(10));
                            }
                        }

                        var selectedAgentId = agents[random.Next(0, agents.Length)].Id;
                        jobInstance.AgentId = selectedAgentId;

                        while(true) try 
                        { 
                            restProxy.Update(jobInstance);
                            break;
                        }
                        catch (RestProxyException e)
                        {
                            if (e.StatusCode == HttpStatusCode.NotFound)
                            {
                                Console.Error.WriteLine("Could not find job instance with id " + jobInstance.Id + " again. May have been deleted.");
                                break;
                            }
                            else
                            {
                                Console.Error.WriteLine("HTTP error while trying to update job instance: " + e.Message);
                                Console.Error.WriteLine("Retrying in a moment...");

                                Thread.Sleep(TimeSpan.FromSeconds(10));
                            }
                        }
                    } 

                    Thread.Sleep(TimeSpan.FromSeconds(5));
                }
            }
        }