/// <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();
                }
            }
        }
예제 #2
0
        /// <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);
        }