/// <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); }
/// <summary> /// Writes to log. /// </summary> /// <param name="errorMessage">The error message.</param> private void WriteToLog(string errorMessage) { try { using (JobsService.JobsClient jobsClient = new JobsService.JobsClient()) { jobsClient.AddLogMessage(errorMessage, true); } Thread.Sleep(1000); // 1 sec. } catch (Exception) { // Network must be down - wait 5 minutes Thread.Sleep(1000 * 60 * 5); } }
/// <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); } }