Пример #1
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            JobSequence   parentJob       = new JobSequence();
            JobParallel   simulationJobs  = new JobParallel();
            List <string> simulationNames = new List <string>();

            FindAllSimulationsToRun(model, simulationJobs, simulationNames);
            parentJob.Jobs.Add(simulationJobs);
            parentJob.Jobs.Add(new RunAllCompletedEvent(simulations));

            // IF we are going to run all simulations, we can delete all tables in the DataStore. This
            // will clean up order of columns in the tables and removed unused ones.
            // Otherwise just remove the unwanted simulations from the DataStore.
            DataStore store = Apsim.Child(simulations, typeof(DataStore)) as DataStore;

            if (model is Simulations)
            {
                store.DeleteAllTables(true);
            }
            else
            {
                store.RemoveUnwantedSimulations(simulations, simulationNames);
            }
            store.Disconnect();

            if (runTests)
            {
                foreach (Tests tests in Apsim.ChildrenRecursively(model, typeof(Tests)))
                {
                    parentJob.Jobs.Add(tests);
                }
            }
            jobManager.AddChildJob(this, parentJob);
        }
Пример #2
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            // Extract the path from the filespec. If non specified then assume
            // current working directory.
            string path = Path.GetDirectoryName(fileSpec);
            if (path == null | path == "")
                path = Directory.GetCurrentDirectory();

            List<string> files = Directory.GetFiles(
                path,
                Path.GetFileName(fileSpec),
                recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).ToList();

            // See above. FIXME!
            files.RemoveAll(s => s.Contains("UnitTests"));
            files.RemoveAll(s => s.Contains("UserInterface"));
            files.RemoveAll(s => s.Contains("ApsimNG"));

            // For each .apsimx file - read it in and create a job for each simulation it contains.
            string workingDirectory = Directory.GetCurrentDirectory();
            string binDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string apsimExe = Path.Combine(binDirectory, "Models.exe");
            foreach (string apsimxFileName in files)
            {
                string arguments = StringUtilities.DQuote(apsimxFileName);
                if (runTests)
                    arguments += " /RunTests";
                jobManager.AddChildJob(this, new RunExternal(apsimExe, arguments, workingDirectory));
            }
        }
Пример #3
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            // IF we are going to run all simulations, we can delete all tables in the DataStore. This
            // will clean up order of columns in the tables and removed unused ones.
            // Otherwise just remove the unwanted simulations from the DataStore.
            DataStore store = Apsim.Child(simulations, typeof(DataStore)) as DataStore;
            if (model is Simulations)
                store.DeleteAllTables();
            else
                store.RemoveUnwantedSimulations(simulations);
            store.Disconnect();

            JobSequence parentJob = new JobSequence();
            JobParallel simulationJobs = new JobParallel();
            FindAllSimulationsToRun(model, simulationJobs);
            parentJob.Jobs.Add(simulationJobs);
            parentJob.Jobs.Add(new RunAllCompletedEvent(simulations));

            if (runTests)
            {
                foreach (Tests tests in Apsim.ChildrenRecursively(model, typeof(Tests)))
                    parentJob.Jobs.Add(tests);
            }
            jobManager.AddChildJob(this, parentJob);
        }
Пример #4
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            Simulation clonedSim = Apsim.Clone(simulation) as Simulation;

            Simulations simulations = Apsim.Parent(simulation, typeof(Simulations)) as Simulations;
            simulation.FileName = simulations.FileName;

            Simulations.MakeSubstitutions(simulations, new List<Simulation> { clonedSim });

            Simulations.CallOnLoaded(clonedSim);
            jobManager.AddChildJob(this, clonedSim);
        }
Пример #5
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            Simulation clonedSim = Apsim.Clone(simulation) as Simulation;

            Simulations simulations = Apsim.Parent(simulation, typeof(Simulations)) as Simulations;

            simulation.FileName = simulations.FileName;

            Simulations.MakeSubstitutions(simulations, new List <Simulation> {
                clonedSim
            });

            Simulations.CallOnLoaded(clonedSim);
            jobManager.AddChildJob(this, clonedSim);
        }
Пример #6
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            List <List <FactorValue> > allCombinations = AllCombinations();
            Simulation  baseSimulation    = Apsim.Child(this, typeof(Simulation)) as Simulation;
            Simulations parentSimulations = Apsim.Parent(this, typeof(Simulations)) as Simulations;

            Stream serialisedBase = Apsim.SerialiseToStream(baseSimulation) as Stream;

            List <Simulation> simulations = new List <Simulation>();

            foreach (List <FactorValue> combination in allCombinations)
            {
                string newSimulationName = Name;
                foreach (FactorValue value in combination)
                {
                    newSimulationName += value.Name;
                }

                Simulation newSimulation = Apsim.DeserialiseFromStream(serialisedBase) as Simulation;
                newSimulation.Name     = newSimulationName;
                newSimulation.Parent   = null;
                newSimulation.FileName = parentSimulations.FileName;
                Apsim.ParentAllChildren(newSimulation);

                // Make substitutions.
                Simulations.MakeSubstitutions(parentSimulations, new List <Simulation> {
                    newSimulation
                });

                // Call OnLoaded in all models.
                Events events = new Events();
                events.AddModelEvents(newSimulation);
                events.CallEventHandler(newSimulation, "Loaded", null);

                foreach (FactorValue value in combination)
                {
                    value.ApplyToSimulation(newSimulation);
                }

                PushFactorsToReportModels(newSimulation, combination);
                StoreFactorsInDataStore(newSimulation, combination);
                jobManager.AddChildJob(this, newSimulation);
            }
        }
Пример #7
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            // Extract the path from the filespec. If non specified then assume
            // current working directory.
            string path = Path.GetDirectoryName(fileSpec);

            if (path == null | path == "")
            {
                path = Directory.GetCurrentDirectory();
            }

            List <string> files = Directory.GetFiles(
                path,
                Path.GetFileName(fileSpec),
                recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).ToList();

            // See above. FIXME!
            files.RemoveAll(s => s.Contains("UnitTests"));
            files.RemoveAll(s => s.Contains("UserInterface"));
            files.RemoveAll(s => s.Contains("ApsimNG"));

            // For each .apsimx file - read it in and create a job for each simulation it contains.
            string workingDirectory = Directory.GetCurrentDirectory();
            string binDirectory     = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string apsimExe         = Path.Combine(binDirectory, "Models.exe");

            foreach (string apsimxFileName in files)
            {
                string arguments = StringUtilities.DQuote(apsimxFileName);
                if (runTests)
                {
                    arguments += " /RunTests";
                }
                jobManager.AddChildJob(this, new RunExternal(apsimExe, arguments, workingDirectory));
            }
        }
Пример #8
0
        /// <summary>Called to start the job.</summary>
        /// <param name="jobManager">The job manager running this job.</param>
        /// <param name="workerThread">The thread this job is running on.</param>
        public void Run(JobManager jobManager, BackgroundWorker workerThread)
        {
            List<List<FactorValue>> allCombinations = AllCombinations();
            Simulation baseSimulation = Apsim.Child(this, typeof(Simulation)) as Simulation;
            Simulations parentSimulations = Apsim.Parent(this, typeof(Simulations)) as Simulations;

            Stream serialisedBase = Apsim.SerialiseToStream(baseSimulation) as Stream;

            List<Simulation> simulations = new List<Simulation>();
            foreach (List<FactorValue> combination in allCombinations)
            {
                string newSimulationName = Name;
                foreach (FactorValue value in combination)
                    newSimulationName += value.Name;

                Simulation newSimulation = Apsim.DeserialiseFromStream(serialisedBase) as Simulation;
                newSimulation.Name = newSimulationName;
                newSimulation.Parent = null;
                newSimulation.FileName = parentSimulations.FileName;
                Apsim.ParentAllChildren(newSimulation);

                // Make substitutions.
                Simulations.MakeSubstitutions(parentSimulations, new List<Simulation> { newSimulation });

                // Call OnLoaded in all models.
                Events events = new Events();
                events.AddModelEvents(newSimulation);
                events.CallEventHandler(newSimulation, "Loaded", null);

                foreach (FactorValue value in combination)
                    value.ApplyToSimulation(newSimulation);

                PushFactorsToReportModels(newSimulation, combination);
                jobManager.AddChildJob(this, newSimulation);
            }
        }
Пример #9
0
 /// <summary>Called to start the job.</summary>
 /// <param name="jobManager">The job manager running this job.</param>
 /// <param name="workerThread">The thread this job is running on.</param>
 public void Run(JobManager jobManager, BackgroundWorker workerThread)
 {
     // Add all jobs to the queue
     foreach (JobManager.IRunnable job in Jobs)
         jobManager.AddChildJob(this, job);
 }
Пример #10
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);
        }