public void Run() { while (PipeUtilities.GetObjectFromPipe(pipeRead) is IRunnable runnable) { job = runnable; Exception error = null; StorageViaSockets storage = new StorageViaSockets(); try { if (runnable is Simulation sim) { storage = new StorageViaSockets(sim.FileName); // Remove existing DataStore sim.Children.RemoveAll(model => model is Models.Storage.DataStore); // Add in a socket datastore to satisfy links. sim.Children.Add(storage); if (sim.Services != null) { sim.Services.RemoveAll(s => s is Models.Storage.IDataStore); sim.Services.Add(storage); } // Initialise the model so that Simulation.Run doesn't call OnCreated. // We don't need to recompile any manager scripts and a simulation // should be ready to run at this point following a binary // deserialisation. sim.ParentAllDescendants(); } else if (runnable is IModel model) { IDataStore oldStorage = model.FindInScope <IDataStore>(); if (oldStorage != null) { storage = new StorageViaSockets(oldStorage.FileName); } storage.Parent = model; storage.Children.AddRange(model.Children.OfType <DataStore>().SelectMany(d => d.Children).Select(m => Apsim.Clone(m))); model.Children.RemoveAll(m => m is DataStore); model.Children.Add(storage); model.ParentAllDescendants(); } // Initiate progress updates. lock (timerLock) timer.Start(); // Run the job. runnable.Run(new CancellationTokenSource()); // Stop progress updates. lock (timerLock) timer.Stop(); } catch (Exception err) { error = err; } // Signal end of job. lock (timerLock) { PipeUtilities.SendObjectToPipe(pipeWrite, new JobOutput { ErrorMessage = error, ReportData = storage.reportDataThatNeedsToBeWritten, DataTables = storage.dataTablesThatNeedToBeWritten }); pipeWrite.WaitForPipeDrain(); } } }
/// <summary>Main program</summary> static int Main(string[] args) { try { if (args == null || args.Length < 2) { throw new Exception("Usage: APSIMRunner.exe pipeWriteHandle pipeReadHandle"); } // Get read and write pipe handles // Note: Roles are now reversed from how the other process is passing the handles in string pipeWriteHandle = args[0]; string pipeReadHandle = args[1]; // Add hook for manager assembly resolve method. AppDomain.CurrentDomain.AssemblyResolve += Manager.ResolveManagerAssembliesEventHandler; // Create 2 anonymous pipes (read and write) for duplex communications // (each pipe is one-way) using (var pipeRead = new AnonymousPipeClientStream(PipeDirection.In, pipeReadHandle)) using (var pipeWrite = new AnonymousPipeClientStream(PipeDirection.Out, pipeWriteHandle)) { //while (args.Length > 0) // Thread.Sleep(200); while (PipeUtilities.GetObjectFromPipe(pipeRead) is Simulation sim) { Exception error = null; var storage = new StorageViaSockets(sim.FileName); try { if (sim != null) { // Remove existing DataStore sim.Children.RemoveAll(model => model is Models.Storage.DataStore); // Add in a socket datastore to satisfy links. sim.Children.Add(storage); // Run the simulation. sim.Run(new CancellationTokenSource()); } else { throw new Exception("Unknown job type"); } } catch (Exception err) { error = err; } // Signal end of job. PipeUtilities.SendObjectToPipe(pipeWrite, new JobOutput { ErrorMessage = error, ReportData = storage.reportDataThatNeedsToBeWritten, DataTables = storage.dataTablesThatNeedToBeWritten }); pipeWrite.WaitForPipeDrain(); } } } catch (Exception err) { Console.WriteLine(err.ToString()); return(1); } finally { AppDomain.CurrentDomain.AssemblyResolve -= Manager.ResolveManagerAssembliesEventHandler; } return(0); }
/// <summary>Main program</summary> static int Main(string[] args) { try { AppDomain.CurrentDomain.AssemblyResolve += Manager.ResolveManagerAssembliesEventHandler; // Send a command to socket server to get the job to run. object response = GetNextJob(); while (response != null) { JobRunnerMultiProcess.GetJobReturnData job = response as JobRunnerMultiProcess.GetJobReturnData; // Run the simulation. Exception error = null; string simulationName = null; RunSimulation simulationRunner = null; StorageViaSockets storage = new StorageViaSockets(job.key); try { IRunnable jobToRun = job.job; if (jobToRun is RunExternal) { jobToRun.Run(new CancellationTokenSource()); } else { simulationRunner = job.job as RunSimulation; // Replace datastore with a socket writer simulationRunner.Services = new object[] { storage }; // Run simulation simulationName = simulationRunner.simulationToRun.Name; simulationRunner.Run(new CancellationTokenSource()); } } catch (Exception err) { error = err; } // Signal we have completed writing data for this sim. storage.WriteAllData(); // Signal end of job. JobRunnerMultiProcess.EndJobArguments endJobArguments = new JobRunnerMultiProcess.EndJobArguments(); endJobArguments.key = job.key; if (error != null) { endJobArguments.errorMessage = error.ToString(); } endJobArguments.simulationName = simulationName; SocketServer.CommandObject endJobCommand = new SocketServer.CommandObject() { name = "EndJob", data = endJobArguments }; SocketServer.Send("127.0.0.1", 2222, endJobCommand); // Get next job. response = GetNextJob(); } } catch (Exception err) { Console.WriteLine(err.ToString()); return(1); } finally { AppDomain.CurrentDomain.AssemblyResolve -= Manager.ResolveManagerAssembliesEventHandler; } return(0); }