예제 #1
0
        public void BuildAusfarmRuntimeEnvironment()
        {
            string binDirectory     = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string runtimeDirectory = Path.Combine(binDirectory, "AusFarm-1.4.12");

            // Remove old runtime directory if it exists.
            if (Directory.Exists(runtimeDirectory))
            {
                Directory.Delete(runtimeDirectory, true);
            }

            // Build the new runtime environment.
            Farm4Prophet       f4p         = GetDefaultF4PSimulationSpec();
            RuntimeEnvironment environment = new RuntimeEnvironment
            {
                AusfarmRevision = "AusFarm-1.4.12",
            };
            RunF4PJob job = new RunF4PJob(f4p, environment);

            // Make sure it is built correctly.
            Assert.IsTrue(Directory.Exists(runtimeDirectory));
            string ausfarmExe = Path.Combine(runtimeDirectory, "Ausfarm", "ausfarm.exe");

            Assert.IsTrue(File.Exists(ausfarmExe));

            // Remove newly created runtime directory
            Directory.Delete(runtimeDirectory, true);
        }
예제 #2
0
        public void RunAusfarmGetOutputs()
        {
            RuntimeEnvironment environment = new RuntimeEnvironment
            {
                AusfarmRevision = "AusFarm-1.4.12",
            };

            RunF4PJob  job    = new RunF4PJob(GetDefaultF4PSimulationSpec(), environment);
            IJobRunner runner = new JobRunnerAsync();

            runner.Run(job, wait: true);

            // Make sure we don't have an error.
            Assert.AreEqual(job.Errors.Count, 0);

            // Make sure we have a daily output table.
            //Assert.AreEqual(job.Outputs.Tables.Count, 3);
            //Assert.AreEqual(job.Outputs.Tables[0].TableName, "Summary");
            //
            //Assert.AreEqual(job.Outputs.Tables[1].TableName, "YieldProphetDaily");
            //Assert.AreEqual(job.Outputs.Tables[1].Rows.Count, 92);
            //double[] biomass = DataTableUtilities.GetColumnAsDoubles(job.Outputs.Tables[1], "biomass");
            //Assert.IsTrue(MathUtilities.Max(biomass) > 20.0); // make sure something is growing.
            //
            //// Make sure we have a depth table.
            //Assert.AreEqual(job.Outputs.Tables[2].TableName, "YieldProphetDepth");
            //Assert.AreEqual(job.Outputs.Tables[2].Rows.Count, 8);
            //double[] sw = DataTableUtilities.GetColumnAsDoubles(job.Outputs.Tables[2], "sw");
            //Assert.IsTrue(MathUtilities.Max(sw) > 0.0); // make sure there are sw values
        }
예제 #3
0
        /// <summary>
        /// Get the next job from the server.
        /// </summary>
        /// <returns></returns>
        private IJobManager GetJobFromServer()
        {
            IJobManager nextJob = null;

            while (nextJob == null && !cancel)
            {
                using (JobsService.JobsClient jobsClient = new JobsService.JobsClient())
                {
                    JobsService.Job runningJobDescription = jobsClient.GetNextToRun();

                    if (runningJobDescription != null)
                    {
                        nameOfCurrentJob = runningJobDescription.Name;
                        string jobXML = jobsClient.GetJobXML(nameOfCurrentJob);

                        if (IsF4PJob(runningJobDescription.Name) == true)
                        {
                            RuntimeEnvironment environment = new RuntimeEnvironment
                            {
                                AusfarmRevision = appSettings["AusfarmRevision"],
                            };
                            nextJob = new RunF4PJob(jobXML, environment);
                        }
                        else
                        {
                            RuntimeEnvironment environment = new RuntimeEnvironment
                            {
                                APSIMRevision  = appSettings["APSIMRevision"],
                                RuntimePackage = appSettings["RuntimePackage"],
                            };
                            nextJob = new RunYPJob(jobXML, environment);
                            if ((nextJob as RunYPJob).Errors.Count > 0)
                            {
                                UpdateServerForCompletedJob(nextJob);
                                nextJob = null;
                            }
                        }
                    }
                    else
                    {
                        // No jobs to run so wait a bit.
                        Thread.Sleep(5 * 1000); // 5 sec.
                    }
                }
            }

            return(nextJob);
        }
예제 #4
0
        /// <summary>
        /// The current job has completed - update server.
        /// </summary>
        /// <param name="jobManager"></param>
        private void UpdateServerForCompletedJob(IJobManager runningJob)
        {
            string errorMessage = null;

            try
            {
                string pwdFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "ftpuserpwd.txt");
                if (!File.Exists(pwdFile))
                {
                    throw new Exception("Cannot find file: " + pwdFile);
                }

                string[] usernamepwd = File.ReadAllText(pwdFile).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                string   zipFileName = Path.GetTempFileName();
                DataSet  outputs;
                using (var s = File.Create(zipFileName))
                {
                    if (runningJob is RunYPJob)
                    {
                        if ((runningJob as RunYPJob).AllFilesZipped != null)
                        {
                            (runningJob as RunYPJob).AllFilesZipped.Seek(0, SeekOrigin.Begin);
                            (runningJob as RunYPJob).AllFilesZipped.CopyTo(s);
                        }
                        outputs = (runningJob as RunYPJob).Outputs;
                        foreach (string err in (runningJob as RunYPJob).Errors)
                        {
                            errorMessage += err;
                        }
                    }
                    else
                    {
                        if ((runningJob as RunYPJob).AllFilesZipped != null)
                        {
                            (runningJob as RunF4PJob).AllFilesZipped.Seek(0, SeekOrigin.Begin);
                            (runningJob as RunF4PJob).AllFilesZipped.CopyTo(s);
                        }
                        outputs = (runningJob as RunF4PJob).Outputs;
                        foreach (string err in (runningJob as RunF4PJob).Errors)
                        {
                            errorMessage += err;
                        }
                    }
                }

                string archiveLocation = appSettings["ArchiveFolder"];
                if (archiveLocation.StartsWith("ftp://"))
                {
                    FTPClient.Upload(zipFileName, archiveLocation + "/" + nameOfCurrentJob + ".zip", usernamepwd[0], usernamepwd[1]);
                }
                else
                {
                    File.Copy(zipFileName, archiveLocation);
                }
                File.Delete(zipFileName);

                if (appSettings["CallStoreReport"] == "true")
                {
                    if (runningJob is RunYPJob)
                    {
                        // YieldProphet - StoreReport
                        // validation runs have a report name of the year e.g. 2015.
                        // Don't need to call StoreReport for them.
                        using (YPReporting.ReportingClient ypClient = new YPReporting.ReportingClient())
                        {
                            try
                            {
                                ypClient.StoreReport(nameOfCurrentJob, outputs);
                            }
                            catch (Exception err)
                            {
                                throw new Exception("Cannot call YP StoreReport web service method: " + err.Message);
                            }
                        }
                    }
                    else if (runningJob is RunF4PJob)
                    {
                        RunF4PJob f4pJob = runningJob as RunF4PJob;

                        DataSet dataSet = new DataSet();
                        foreach (DataTable table in f4pJob.Outputs.Tables)
                        {
                            // Don't send the cropping daily and monthly files
                            if (table.TableName.EndsWith("_daily.txt") == false &&
                                table.TableName.EndsWith("_monthly.txt") == false)
                            {
                                dataSet.Tables.Add(table);
                            }
                        }

                        // Call Farm4Prophet web service.
                        using (F4P.F4PClient f4pClient = new F4P.F4PClient())
                        {
                            try
                            {
                                f4pClient.StoreReport(nameOfCurrentJob, dataSet);
                            }
                            catch (Exception err)
                            {
                                throw new Exception("Cannot call F4P StoreReport web service method: " + err.Message);
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                errorMessage = err.ToString();
            }

            using (JobsService.JobsClient jobsClient = new JobsService.JobsClient())
            {
                if (errorMessage != null)
                {
                    errorMessage = errorMessage.Replace("'", "");
                }
                jobsClient.SetCompleted(nameOfCurrentJob, errorMessage);
            }
        }
예제 #5
0
        /// <summary>Runs the job (.xml file) specified on the command line.</summary>
        /// <returns>True if something was run.</returns>
        private static bool RunJobFromCommandLine(Dictionary <string, string> appSettings)
        {
            if (appSettings.ContainsKey("FileName"))
            {
                string jobFileName = appSettings["FileName"];

                if (File.Exists(jobFileName))
                {
                    var ypEnvironment = new APSIM.Cloud.Shared.RuntimeEnvironment
                    {
                        APSIMRevision  = appSettings["APSIMRevision"],
                        RuntimePackage = appSettings["RuntimePackage"],
                    };

                    if (appSettings.ContainsKey("UpdateFile"))
                    {
                        YieldProphetOld.UpdateFile(jobFileName);
                        return(true);
                    }
                    else if (appSettings.ContainsKey("ConvertToAPSIM"))
                    {
                        string   jobXML = File.ReadAllText(jobFileName);
                        RunYPJob job    = new RunYPJob(jobXML, ypEnvironment, createSims: false);
                        AllocConsole();
                        Console.WriteLine(job.WorkingDirectory);
                        Console.WriteLine();
                        if (job.Errors != null)
                        {
                            string msg = null;
                            foreach (string error in job.Errors)
                            {
                                msg += error + Environment.NewLine;
                            }
                            throw new Exception(msg);
                        }
                        return(true);
                    }
                    else
                    {
                        appSettings.TryGetValue("APSIMXExecutable", out string executable);

                        string jobXML  = File.ReadAllText(jobFileName);
                        string jobName = Path.GetFileNameWithoutExtension(jobFileName);

                        IYPJob job = null;
                        if (jobXML.Contains("Farm4Prophet"))
                        {
                            var environment = new APSIM.Cloud.Shared.RuntimeEnvironment
                            {
                                AusfarmRevision = appSettings["AusfarmRevision"],
                            };
                            job = new RunF4PJob(jobXML, environment);
                        }
                        else
                        {
                            job = new RunYPJob(jobXML, ypEnvironment)
                            {
                                ApsimXExecutable = executable
                            };
                        }
                        if (job.Errors == null || job.Errors.Count == 0)
                        {
                            IJobRunner runner = new JobRunnerAsync();
                            runner.Run(job as IJobManager, wait: true);
                        }

                        if (job.AllFilesZipped != null)
                        {
                            string destZipFileName = Path.ChangeExtension(jobFileName, ".out.zip");
                            using (Stream s = File.Create(destZipFileName))
                            {
                                job.AllFilesZipped.Seek(0, SeekOrigin.Begin);
                                job.AllFilesZipped.CopyTo(s);
                            }
                        }

                        if (job.Errors != null && job.Errors.Count > 0)
                        {
                            string msg = string.Empty;
                            foreach (string error in job.Errors)
                            {
                                msg += error + Environment.NewLine;
                            }
                            throw new Exception(msg);
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }