/// <summary>Processes any jobs that have been added.</summary> /// <param name="jobManager">The job manager.</param> private void ProcessAddedJobs(JobManager jobManager) { JobsService.Job runningJobDescription = null; //If there is no YP job running, then add a new job. Once a YP job is running don't add any more jobs (regardless of type). if (runningJob == null || jobManager.IsJobCompleted(runningJob)) { // Remove completed jobs if nothing is running. Otherwise, completedjobs will // grow and grow. jobManager.ClearCompletedJobs(); runningJob = null; using (JobsService.JobsClient jobsClient = new JobsService.JobsClient()) { runningJobDescription = jobsClient.GetNextToRun(); } if (runningJobDescription != null) { if (RunnableJobs.ProcessYPJob.IsF4PJob(runningJobDescription.Name) == true) runningJob = new RunnableJobs.ProcessF4PJob(true) { JobName = runningJobDescription.Name }; else runningJob = new RunnableJobs.ProcessYPJob(true) { JobName = runningJobDescription.Name }; if (runningJob != null) jobManager.AddJob(runningJob); } else { // No jobs to run so wait a bit. Thread.Sleep(15 * 1000); // 15 sec. } } }
/// <summary>Handles the Load event of the Page control.</summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void Page_Load(object sender, EventArgs e) { GridView.DataSource = null; using (JobsService.JobsClient jobsService = new JobsService.JobsClient()) { JobsService.Job[] jobs = jobsService.GetMany(Convert.ToInt32(NumRowsTextBox.Text)); table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Status", typeof(string)); table.Columns.Add("XML", typeof(string)); table.Columns.Add("URL", typeof(string)); // Loop through all jobs foreach (JobsService.Job job in jobs) { if (ShowAllCheckBox.Checked || job.Status != JobsService.StatusEnum.Completed) { DataRow newRow = table.NewRow(); table.Rows.Add(newRow); newRow["Name"] = job.Name; newRow["Status"] = job.Status.ToString(); newRow["XML"] = "XML"; newRow["URL"] = job.URL; } } } GridView.DataSource = table; GridView.DataBind(); }
/// <summary>Uploads a job specified by the the yield prophet.</summary> /// <param name="fileName">The name of the file.</param> private void UploadFarm4Prophet(string fileName) { Farm4Prophet farm4Prophet = Farm4ProphetUtility.Farm4ProphetFromFile(fileName); using (JobsService.JobsClient jobsService = new JobsService.JobsClient()) { jobsService.AddFarm4Prophet(farm4Prophet); } }
/// <summary>Uploads a job specified by the the yield prophet.</summary> /// <param name="fileName">The name of the file.</param> private void UploadYieldProphet(string fileName) { YieldProphet yieldProphet = YieldProphetUtility.YieldProphetFromFile(fileName); DateTime nowDate = DateTime.Now; if (NowEditBox.Text != "") nowDate = DateTime.ParseExact(NowEditBox.Text, "d/M/yyyy", CultureInfo.InvariantCulture); foreach (Paddock paddock in yieldProphet.Paddock) paddock.NowDate = nowDate; using (JobsService.JobsClient jobsService = new JobsService.JobsClient()) { jobsService.Add(yieldProphet); } }
/// <summary> /// We're about to render the page - search for the soils and write names to the /// response. The iPad soil app uses this method. /// </summary> protected void Page_PreRender(object sender, EventArgs e) { Response.Clear(); if (Request.QueryString["Name"] != null) { using (JobsService.JobsClient jobsService = new JobsService.JobsClient()) { string jobName = Request.QueryString["Name"]; JobsService.Job job = jobsService.Get(jobName); if (Request.QueryString["Type"] != null) { string type = Request.QueryString["Type"]; if (type == "XML") { Response.ContentType = "text/xml"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.Output.Write(jobsService.GetJobXML(jobName)); } else { Response.ContentType = "text/plain"; Response.Output.Write(job.ErrorText); } Response.End(); } } } else { using (JobsService.JobsClient jobsService = new JobsService.JobsClient()) { string text = string.Empty; DataSet log = jobsService.GetLogMessages(); foreach (DataRow row in log.Tables[0].Rows) { text += Convert.ToDateTime(row["DATE"]).ToString("yyyy-MM-dd hh:mm:ss tt") + ": " + row["MESSAGE"] + "\r\n"; } Response.ContentType = "text/plain"; Response.Output.Write(text); Response.End(); } } }
/// <summary>Called when the user clicks an image or link on a particular row of the grid.</summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="GridViewCommandEventArgs"/> instance containing the event data.</param> protected void OnGridRowCommand(object sender, GridViewCommandEventArgs e) { // Retrieve the row index stored in the // CommandArgument property. int index = Convert.ToInt32(e.CommandArgument); if (e.CommandName == "Status") { string status = table.Rows[index]["Status"].ToString(); if (status == "Error") { string name = table.Rows[index]["Name"].ToString(); using (JobsService.JobsClient jobsService = new JobsService.JobsClient()) { JobsService.Job job = jobsService.Get(name); Response.Redirect("ShowJobDetail.aspx?Name=" + name + "&Type=Error"); } } } else if (e.CommandName == "XML") { string name = table.Rows[index]["Name"].ToString(); Response.Redirect("ShowJobDetail.aspx?Name=" + name + "&Type=XML"); } else if (e.CommandName == "ReRun") { using (JobsService.JobsClient jobsService = new JobsService.JobsClient()) { string name = table.Rows[index]["Name"].ToString(); jobsService.ReRun(name); Response.Redirect("Main.aspx"); } } else if (e.CommandName == "Delete") { using (JobsService.JobsClient jobsService = new JobsService.JobsClient()) { string name = table.Rows[index]["Name"].ToString(); jobsService.Delete(name); Response.Redirect("Main.aspx"); } } }
/// <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>Called to start the job.</summary> /// <param name="jobManager">Job manager</param> /// <param name="worker">Background worker</param> public void Run(JobManager jobManager, BackgroundWorker worker) { // Create a working directory. workingDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(workingDirectory); string jobXML; if (JobFileName == null) { using (JobsService.JobsClient jobsClient = new JobsService.JobsClient()) { jobXML = jobsClient.GetJobXML(JobName); } } else { if (Path.GetExtension(JobFileName) == ".zip") ZipUtilities.UnZipFiles(JobFileName, workingDirectory, null); else File.Copy(JobFileName, Path.Combine(workingDirectory, Path.GetFileName(JobFileName))); string[] xmlFileNames = Directory.GetFiles(workingDirectory, "*.xml"); if (xmlFileNames.Length == 0) throw new Exception("Cannot find a job xml file"); StreamReader reader = new StreamReader(xmlFileNames[0]); jobXML = reader.ReadToEnd(); reader.Close(); JobName = Path.GetFileNameWithoutExtension(xmlFileNames[0]); } // Create and run a job. string ErrorMessage = null; try { JobManager.IRunnable job = CreateRunnableJob(JobName, jobXML, workingDirectory, ApsimExecutable); if (runAPSIM) { jobManager.AddChildJob(this, job); while (!jobManager.IsJobCompleted(job)) Thread.Sleep(5 * 1000); // 5 sec } List<Exception> errors = jobManager.Errors(job); if (errors.Count > 0) ErrorMessage = errors[0].ToString(); } catch (Exception err) { ErrorMessage = err.Message + "\r\n" + err.StackTrace; } // Zip the temporary directory and send to archive. string zipFileName = Path.Combine(workingDirectory, JobName + ".zip"); ZipUtilities.ZipFiles(Directory.GetFiles(workingDirectory), zipFileName, null); // Tell the job system that job is complete. if (JobFileName == null) { string pwdFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "ftpuserpwd.txt"); if (!File.Exists(pwdFile)) ErrorMessage = "Cannot find file: " + pwdFile; else { string[] usernamepwd = File.ReadAllText(pwdFile).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); FTPClient.Upload(zipFileName, archiveLocation + "/" + JobName + ".zip", usernamepwd[0], usernamepwd[1]); } using (JobsService.JobsClient jobsClient = new JobsService.JobsClient()) { if (ErrorMessage != null) ErrorMessage = ErrorMessage.Replace("'", ""); jobsClient.SetCompleted(JobName, ErrorMessage); } } else { string destZipFileName = Path.ChangeExtension(JobFileName, ".out.zip"); File.Copy(zipFileName, destZipFileName, true); } // Get rid of our temporary directory. Directory.Delete(workingDirectory, true); if (ErrorMessage != null) throw new Exception(ErrorMessage); }