/// <summary> /// copyFromCache: copies simulation files from cache, return true if cache exists. /// </summary> /// <param name="job"></param> /// <returns></returns> private bool copyFromCache(IJobConsumerContract job) { string cacheDir = Path.Combine(AppUtility.GetAppContext().BaseWorkingDirectory, job.SimulationId.ToString()); //string shortConfigFileName = "sinter_configuration.txt"; //configFileName = Path.Combine(job.Process.WorkingDirectory, shortConfigFileName); if (Directory.Exists(cacheDir)) { // Shallow Copy cacheDir to Working Directory var source = new DirectoryInfo(cacheDir); var target = new DirectoryInfo(job.Process.WorkingDirectory); /*foreach (FileInfo finfo in source.GetFiles()) * { * Debug.WriteLine(String.Format("Found Cached file {0}: {1}", job.SimulationId, finfo.FullName), * "SinterConsumer.copyFromCache"); * finfo.CopyTo(Path.Combine(target.ToString(), finfo.Name), true); * }*/ Debug.WriteLine("Copying Cached files", "SinterConsumerRun.CopyFromCache"); DirectoryCopy(source.ToString(), target.ToString(), true); return(true); } return(false); }
/// <summary> /// copyFilesToDisk copies files from data contract to working directory. Assumes the sinter configuration file /// is named 'configuration' in the job description. Currently handles all the mapping from 'resource' names /// specified in the database schema to file names. /// </summary> /// <param name="job"></param> protected override void copyFilesToDisk(IJobConsumerContract job) { string cacheDir = Path.Combine(AppUtility.GetAppContext().BaseWorkingDirectory, job.SimulationId.ToString()); // NOTE: Aspen implementations only // Find 'aspenfile' // configuration file SimpleFile config = job.GetSimulationInputFiles().Single(f => f.name == "configuration"); string content = System.Text.Encoding.ASCII.GetString(config.content); var cacheFile = Path.Combine(cacheDir, configFileName); var filepath = Path.Combine(job.Process.WorkingDirectory, configFileName); File.WriteAllBytes(cacheFile, config.content); File.Copy(cacheFile, filepath); Dictionary <String, Object> jsonConfig = JsonConvert.DeserializeObject <Dictionary <String, Object> >(content); string modelfilename = (String)jsonConfig["model"]; SimpleFile model = job.GetSimulationInputFiles().Single(g => g.name == "model"); cacheFile = Path.Combine(cacheDir, modelfilename); filepath = Path.Combine(job.Process.WorkingDirectory, modelfilename); File.WriteAllBytes(cacheFile, model.content); File.Copy(cacheFile, filepath); }
/// <summary> /// DoRun sends inputs specified in job contract to aspen simulation and runs /// the simulation. /// </summary> /// <param name="job"></param> /// <param name="stest"></param> /// <param name="combinedDict"></param> protected void DoRun(IJobConsumerContract job, sinter.ISimulation stest, JObject defaultsDict, JObject inputDict) { Debug.WriteLine(String.Format("Send Inputs to Job {0}", job.Id), "SinterConsumer.DoRun"); try { if (defaultsDict != null) { stest.sendDefaults(defaultsDict); } stest.sendInputs(inputDict); } catch (Exception ex) { job.Error(String.Format( "Error sending Inputs to Aspen: {0}, traceback {1}", ex.Message, ex.StackTrace)); Debug.WriteLine(String.Format("Moved Job {0} to Error", job.Id), "SinterConsumer.DoRun"); throw; } job.Message("sinter inputs sent, running simulation"); Debug.WriteLine(String.Format("Job {0}: sendInputsToSim ", job.Id), "SinterConsumer.DoRun"); try { running = true; lock (this) { stest.sendInputsToSim(); Debug.WriteLine(String.Format("Job {0}: runSim ", job.Id), "SinterConsumer.DoRun"); stest.runSim(); Debug.WriteLine(String.Format("Job {0}: recvOutputsFromSim ", job.Id), "SinterConsumer.DoRun"); stest.recvOutputsFromSim(); } running = false; if (isTerminated) { throw new TerminateException( "Monitor thread Terminated Simulation"); } } catch (Exception ex) { Debug.WriteLine(String.Format("Moving Job {0} to Error", job.Id), "SinterConsumerRun.DoRun"); Debug.WriteLine(ex.Message, "SinterConsumerRun.DoRun"); Debug.WriteLine(ex.StackTrace, "SinterConsumerRun.DoRun"); job.Error(String.Format( "Error running simulation: {0}, traceback {1}", ex.Message, ex.StackTrace)); throw; } }
/// <summary> /// DoInitialize initializes the Aspen Engine if specified in the job contract. /// </summary> /// <param name="job"></param> /// <param name="stest"></param> /// <param name="defaultsDict"></param> private void DoInitialize(IJobConsumerContract job, sinter.ISimulation stest, JObject defaultsDict) { if (!job.Initialize()) { return; } Debug.WriteLine(String.Format("Start warm-up run on job {0}", job.Id), "SinterConsumer.Run"); try { stest.sendInputs(defaultsDict); } catch (Exception ex) { job.Error(String.Format( "Error sending Defaults to Aspen for warm-up run: {0}, traceback {1}", ex.Message, ex.StackTrace)); Debug.WriteLine(String.Format("Moved Job {0} to Error", job.Id), "SinterConsumer.Run"); throw; } job.Message("Warm-up inputs sent, running simulation"); Debug.WriteLine(String.Format("Calling run Simulation for warm-up on Job {0}", job.Id), "SinterConsumer.Run"); try { running = true; lock (this) { stest.sendInputsToSim(); stest.runSim(); stest.recvOutputsFromSim(); } running = false; if (isTerminated) { throw new TerminateException( "Monitor Terminated Initializing Simulation"); } } catch (Exception ex) { Debug.WriteLine(String.Format("Moving Job {0} to Error", job.Id), "SinterConsumer.Run"); job.Error(String.Format( "Error running warm-up simulation: {0}, traceback {1}", ex.Message, ex.StackTrace)); throw; } job.Message("Warm-up complete and successful. Reseting Sim for real run."); Debug.WriteLine(String.Format("Warm-up complete and successful. Reseting Sim for real run {0}", job.Id), "SinterConsumer.Run"); }
protected override void InitializeSinter(IJobConsumerContract job, String workingDir, String config) { if (IsSupported(job) == false) { throw new ArgumentException(String.Format("gPROMS: Found wrong Application {0}", job.ApplicationName)); } sim = new sinter.PSE.sinter_simGPROMS(); sim.workingDir = workingDir; sim.setupFile.parseFile(config); }
public void TestACMDynamic() { IDictionary <string, object> output; IConsumerRun run = (IConsumerRun) new InMemorySinterConsumer(); Assert.IsFalse(run.IsEngineRunning); Assert.IsFalse(run.IsSimulationInitializing); Assert.IsFalse(run.IsSimulationOpened); // set job before calling Run ((InMemorySinterConsumer)run).job = new InMemoryJobACM_VdV_Dynamic(); Newtonsoft.Json.Linq.JObject inputsD = JObject.Parse("{\"Ca_Feed\": [10.0,11.0,12.0,13.0],\"RunMode\": \"Dynamic\", \"TimeSeries\":[0.01,0.02,0.03,0.04]}"); //Newtonsoft.Json.Linq.JObject inputsD = JObject.Parse("{\"Ca_Feed\": [10.0,10.0,10.0,10.0],\"RunMode\": \"Dynamic\", \"TimeSeries\":[0.01,0.02,0.03,0.04]}"); //Newtonsoft.Json.Linq.JObject inputsD = JObject.Parse("{\"UQ_ADS_db\":1.08}"); //Newtonsoft.Json.Linq.JObject inputsD = JObject.Parse("{\"Ca_Feed\":[10.0,10.0,10.0,10.0,10.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,10.5,10.5,10.5,10.5,10.5,10.5,10.5,10.5,10.5,10.5,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,10.5,10.5,10.5,10.5,10.5,10.5,10.5,10.5,10.5,10.5,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.0,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,9.5,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0,11.0],\"F_Feed\":[3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3600.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3780.0000000000005,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3240.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3420.0,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005,3960.0000000000005],\"Volume\":[100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0],\"RunMode\":\"Dynamic\",\"TimeSeries\":[0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.1,0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.19,0.2,0.21,0.22,0.23,0.24,0.25,0.26,0.27,0.28,0.29,0.3,0.31,0.32,0.33,0.34,0.35000000000000003,0.36,0.37,0.38,0.39,0.4,0.41000000000000003,0.42,0.43,0.44,0.45,0.46,0.47000000000000003,0.48,0.49,0.5,0.51,0.52,0.53,0.54,0.55,0.56,0.57000000000000006,0.58,0.59,0.6,0.61,0.62,0.63,0.64,0.65,0.66,0.67,0.68,0.69000000000000006,0.70000000000000007,0.71,0.72,0.73,0.74,0.75,0.76,0.77,0.78,0.79,0.8,0.81,0.82000000000000006,0.83000000000000007,0.84,0.85,0.86,0.87,0.88,0.89,0.9,0.91,0.92,0.93,0.94000000000000006,0.95000000000000007,0.96,0.97,0.98,0.99,1.0,1.01,1.02,1.03,1.04,1.05,1.06,1.07,1.08,1.09,1.1,1.11,1.12,1.1300000000000001,1.1400000000000001,1.1500000000000001,1.16,1.17,1.18,1.19,1.2,1.21,1.22,1.23,1.24,1.25,1.26,1.27,1.28,1.29,1.3,1.31,1.32,1.33,1.34,1.35,1.36,1.37,1.3800000000000001,1.3900000000000001,1.4000000000000001,1.41,1.42,1.43,1.44,1.45,1.46,1.47,1.48,1.49,1.5,1.51,1.52,1.53,1.54,1.55,1.56,1.57,1.58,1.59,1.6,1.61,1.62,1.6300000000000001,1.6400000000000001,1.6500000000000001,1.6600000000000001,1.67,1.68,1.69,1.7,1.71,1.72,1.73,1.74,1.75,1.76,1.77,1.78,1.79,1.8,1.81,1.82,1.83,1.84,1.85,1.86,1.87,1.8800000000000001,1.8900000000000001,1.9000000000000001,1.9100000000000001,1.92,1.93,1.94,1.95,1.96,1.97,1.98,1.99,2.0,2.0100000000000002,2.02,2.0300000000000002,2.04,2.05,2.06,2.07,2.08,2.09,2.1,2.11,2.12,2.13,2.14,2.15,2.16,2.17,2.18,2.19,2.2,2.21,2.22,2.23,2.24,2.25],\"printlevel\":0,\"homotopy\":0}"); Dictionary <String, Object> dd = new Dictionary <string, object>(); foreach (var v in inputsD) { //Debug.WriteLine("VALUE: " + v); dd[v.Key] = v.Value; } ((InMemorySinterConsumer)run).job.Process.Input = dd; try { Assert.IsTrue(run.Run()); Assert.IsTrue(run.IsSimulationOpened); IJobConsumerContract job = ((InMemorySinterConsumer)run).job; //Assert.IsTrue(job.IsSuccess()); output = job.Process.Output; Assert.IsNotNull(output); foreach (KeyValuePair <string, object> kv in output) { Debug.WriteLine(String.Format("{0} : {1}", kv.Key, kv.Value)); } } finally { Debug.WriteLine("Attempt Cleanup"); run.CleanUp(); } Object d; output.TryGetValue("status", out d); JObject statusD = (JObject)d; JToken status = statusD.GetValue("value"); Assert.AreEqual <int>(status.Value <int>(), 0); }
public void SinterGPROMSTest() { IDictionary <string, object> output; IConsumerRun run = (IConsumerRun) new InMemorySinterConsumerGProms(); Assert.IsFalse(run.IsEngineRunning); Assert.IsFalse(run.IsSimulationInitializing); Assert.IsFalse(run.IsSimulationOpened); // set job before calling Run ((InMemorySinterConsumerGProms)run).job = new InMemoryJobGProms_BufferTank_FO(); //Newtonsoft.Json.Linq.JObject inputsD = JObject.Parse("{\"inputs\": { \"ProcessName\": \"SimulateTank_sinter\", \"password\": \"BufferTank_FO\", \"AlphaFO\": 0.8,\"SingleReal\": 0.0,\"SingleInt\": 11,\"ArrayInt\": [12,12],\"FlowInFO\": 14.0,\"Mass\": [1.0,2.0,3.0,4.0,5.0],\"HeightFO\": 7.5,\"singleSelector\": \"apple\",\"arraySelector\": [\"red\",\"red\",\"red\"]}}"); Newtonsoft.Json.Linq.JObject inputsD = new JObject(); Dictionary <String, Object> dd = new Dictionary <string, object>(); foreach (var v in inputsD) { //Debug.WriteLine("VALUE: " + v); dd[v.Key] = v.Value; } ((InMemorySinterConsumerGProms)run).job.Process.Input = dd; try { Assert.IsTrue(run.Run()); //Assert.IsTrue(run.IsSimulationOpened); IJobConsumerContract job = ((InMemorySinterConsumerGProms)run).job; Assert.IsTrue(job.IsSuccess()); output = job.Process.Output; Assert.IsNotNull(output); foreach (KeyValuePair <string, object> kv in output) { Debug.WriteLine(String.Format("{0} : {1}", kv.Key, kv.Value)); } } finally { Debug.WriteLine("Attempt Cleanup"); run.CleanUp(); } Object d; output.TryGetValue("status", out d); JObject statusD = (JObject)d; JToken status = statusD.GetValue("value"); Assert.AreEqual <int>(status.Value <int>(), 0); }
public void TestACM_BFBv6_3_new_Defaults() { IDictionary <string, object> output; IConsumerRun run = (IConsumerRun) new InMemorySinterConsumer(); IConsumerMonitor monitor = (IConsumerMonitor) new InMemorySinterConsumerAspenPlusMonitor(); monitor.setConsumerRun(run); Assert.IsFalse(run.IsEngineRunning); Assert.IsFalse(run.IsSimulationInitializing); Assert.IsFalse(run.IsSimulationOpened); // set job before calling Run ((InMemorySinterConsumer)run).job = new InMemoryJobACM_BFBv6_3(); try { //Assert.IsTrue(run.Run()); var taskRun = new Task <Boolean>(() => run.Run()); var taskMonitor = new Task <int>(() => monitor.Monitor(true)); Debug.WriteLine("Start taskRun", this.GetType().Name); taskRun.Start(); Thread.Sleep(50000); Assert.IsTrue(run.IsSimulationOpened); Debug.WriteLine("Start taskMonitor", this.GetType().Name); taskMonitor.Start(); Thread.Sleep(12000); Assert.IsTrue(run.IsSimulationOpened); IJobConsumerContract job = ((InMemorySinterConsumer)run).job; //Assert.IsTrue(job.IsSuccess()); output = job.Process.Output; //Assert.IsNotNull(output); /*foreach (KeyValuePair<string, object> kv in output) * { * Debug.WriteLine(String.Format("{0} : {1}", kv.Key, kv.Value)); * }*/ } finally { Debug.WriteLine("Attempt Cleanup"); run.CleanUp(); } }
public void TestACM() { IDictionary <string, object> output; IConsumerRun run = (IConsumerRun) new InMemorySinterConsumer(); Assert.IsFalse(run.IsEngineRunning); Assert.IsFalse(run.IsSimulationInitializing); Assert.IsFalse(run.IsSimulationOpened); // set job before calling Run ((InMemorySinterConsumer)run).job = new InMemoryJobACM(); Newtonsoft.Json.Linq.JObject inputsD = JObject.Parse("{\"UQ_ADS_db\":1.08}"); Dictionary <String, Object> dd = new Dictionary <string, object>(); foreach (var v in inputsD) { Debug.WriteLine("VALUE: " + v); dd[v.Key] = v.Value; } ((InMemorySinterConsumer)run).job.Process.Input = dd; try { Assert.IsTrue(run.Run()); Assert.IsTrue(run.IsSimulationOpened); IJobConsumerContract job = ((InMemorySinterConsumer)run).job; Assert.IsTrue(job.IsSuccess()); output = job.Process.Output; Assert.IsNotNull(output); foreach (KeyValuePair <string, object> kv in output) { Debug.WriteLine(String.Format("{0} : {1}", kv.Key, kv.Value)); } } finally { Debug.WriteLine("Attempt Cleanup"); run.CleanUp(); } Object d; output.TryGetValue("status", out d); JObject statusD = (JObject)d; JToken status = statusD.GetValue("value"); Assert.AreEqual <int>(status.Value <int>(), 0); }
/// <summary> /// InitializeSinter: MUST be called before openSim (working dir and setupFile must be done). /// /// </summary> /// <param name="job"></param> /// <param name="workingDir"></param> /// <param name="config"></param> protected override void InitializeSinter(IJobConsumerContract job, string workingDir, string config) { applicationName = job.ApplicationName; if (job.ApplicationName == "GProms") { sim = new sinter_simGPROMS(); sim.setupFile = new sinter_JsonSetupFile(); } else { throw new Exception(String.Format("Unsupported Application Type: {0}", job.ApplicationName)); } sim.workingDir = workingDir; sim.setupFile.parseFile(config); }
/// <summary> /// InitializeSinter: MUST be called before openSim (working dir and setupFile must be done). /// /// </summary> /// <param name="job"></param> /// <param name="workingDir"></param> /// <param name="config"></param> protected override void InitializeSinter(IJobConsumerContract job, string workingDir, string config) { applicationName = job.ApplicationName; if (job.ApplicationName == "ACM") { sim = new sinter_SimACM(); sim.setupFile = new sinter_JsonSetupFile(); } else if (job.ApplicationName == "AspenPlus") { sim = new sinter_SimAspen(); sim.setupFile = new sinter_JsonSetupFile(); } sim.workingDir = workingDir; sim.setupFile.parseFile(config); }
/// <summary> /// terminateSimAndJob: Terminate both job and simulation /// </summary> /// <param name="job"></param> /// <returns></returns> public bool terminateSimAndJob() { bool success = false; if (sim != null) { if (sim.ProcessID > 0) { KillProcessAndChildren(sim.ProcessID); success = sim.terminate(); } job = null; isTerminated = success; Debug.WriteLine("Job termination result : {0}", success); Debug.WriteLine(String.Format("Simulation Terminate ( sinter:{0}, processID:{1} ) {2}", sim.GetType().Name, sim.ProcessID, success), GetType().Name); } return(success); }
public void TestACM_SinterTermination() { IDictionary <string, object> output; IConsumerRun run = (IConsumerRun) new InMemorySinterConsumer(); Assert.IsFalse(run.IsEngineRunning); Assert.IsFalse(run.IsSimulationInitializing); Assert.IsFalse(run.IsSimulationOpened); // set job before calling Run ((InMemorySinterConsumer)run).job = new InMemoryJobACM(); try { Assert.IsTrue(run.Run()); Assert.IsTrue(run.IsSimulationOpened); IJobConsumerContract job = ((InMemorySinterConsumer)run).job; Assert.IsTrue(job.IsSuccess()); output = job.Process.Output; Assert.IsNotNull(output); foreach (KeyValuePair <string, object> kv in output) { Debug.WriteLine(String.Format("{0} : {1}", kv.Key, kv.Value)); } } finally { Debug.WriteLine("Attempt Cleanup"); run.CleanUp(); } Object d; output.TryGetValue("status", out d); JObject statusD = (JObject)d; JToken status = statusD.GetValue("value"); Assert.AreEqual <int>(status.Value <int>(), 0); }
protected override bool IsSupported(IJobConsumerContract job) { return(job.ApplicationName == "ACM" || job.ApplicationName == "AspenPlus"); }
protected override bool IsSupported(IJobConsumerContract job) { return(job.ApplicationName == "GProms"); }
/// <summary> /// RunNoReset doesn't close underlying COM object, doesn't change Sinter working directory. /// Grabs new inputs and sends them via sinter, and runs again. /// </summary> /// <param name="job"></param> /// <returns></returns> protected override bool RunNoReset(IJobConsumerContract job) { if (job.SimulationId != last_simulation_name) { return(false); } if ((sim.GetType() == typeof(sinter_simGPROMS))) { // hack to see whether Aspen has already been initialized // should implement an interface "closed" try { bool test = ((sinter_simGPROMS)sim).Vis; } catch (NullReferenceException) { return(false); } } if (last_simulation_success == false) { return(false); } IProcess process = null; Debug.WriteLine(String.Format("Reusing Sinter({0}) : Job {1}", job.ApplicationName, job.Id), GetType().Name); try { process = job.Setup(); } catch (Exception ex) { Debug.WriteLine(ex.Message, GetType().Name); throw; } job.Message("Setup: Reusing Sinter GProms"); Debug.WriteLine(String.Format("Move Job {0} to state Running", job.Id), GetType().Name); job.Running(); IDictionary <String, Object> inputDict = null; String data = null; JObject defaults = null; JObject inputs = null; // NOTE: Quick Fix. Serializing Dicts into string json // and back to JObject because of API change to sinter. // Better to refactor interfaces, etc to hand back strings try { inputDict = process.Input; data = JsonConvert.SerializeObject(inputDict); inputs = JObject.Parse(data); } catch (Exception ex) { sim.closeSim(); job.Error("Failed to Create Inputs to Simulation (Bad Simulation defaults or Job inputs)"); job.Message(ex.StackTrace.ToString()); throw; } Debug.WriteLine("Run", GetType().Name); try { DoRun(job, sim, defaults, inputs); } catch (System.Data.SqlClient.SqlException ex) { Debug.WriteLine("SqlException: " + ex.Message, GetType().Name); Debug.WriteLine(ex.StackTrace, GetType().Name); sim.closeSim(); sim = null; throw; } catch (Exception ex) { Debug.WriteLine("DoRun Exception Close", GetType().Name); //if (CheckTerminate() == false) if (isTerminated == false) { job.Error("Exception: " + ex.Message); sim.closeSim(); } sim = null; throw; } Debug.WriteLine("Finalize", GetType().Name); try { DoFinalize(sim, job, process); } catch (System.Data.SqlClient.SqlException ex) { Debug.WriteLine("SqlException: " + ex.Message, GetType().Name); Debug.WriteLine(ex.StackTrace, GetType().Name); sim.closeSim(); sim = null; throw; } catch (DbEntityValidationException dbEx) { Debug.WriteLine("DbEntityValidationException: " + dbEx.Message, GetType().Name); Debug.WriteLine(dbEx.StackTrace, GetType().Name); var msg = String.Empty; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { msg += String.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); Debug.WriteLine(msg, GetType().Name); Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); msg += ". \n"; } } job.Error("Exception: " + msg); sim.closeSim(); sim = null; throw; } catch (Exception ex) { Debug.WriteLine("DoFinalize Exception Close", GetType().Name); job.Error("Exception: " + ex.Message); sim.closeSim(); sim = null; throw; } return(true); }
/*protected override void copyFilesToDisk(Data.Contract.Behaviors.IJobConsumerContract job) * { * Debug.WriteLine("Copying files to disk", GetType().Name); * string cacheDir = Path.Combine(AppUtility.GetAppContext().BaseWorkingDirectory, job.SimulationId.ToString()); * Directory.CreateDirectory(cacheDir); * * // NOTE: Excel implementations only * // Find 'spreadsheet' * // configuration file * SimpleFile config = job.GetSimulationInputFiles().Single(f => f.name == "configuration"); * string content = System.Text.Encoding.ASCII.GetString(config.content); * * var cacheFile = Path.Combine(cacheDir, configFileName); * var filepath = Path.Combine(job.Process.WorkingDirectory, configFileName); * Debug.WriteLine("filePath: " + filepath, GetType().Name); * Debug.WriteLine("cacheFile: " + cacheFile, GetType().Name); * File.WriteAllBytes(cacheFile, config.content); * File.Copy(cacheFile, filepath); * * Dictionary<String, Object> jsonConfig = JsonConvert.DeserializeObject<Dictionary<String, Object>>(content); * object file_version; * string modelfilename; * // Handling the new format of SinterConfig file * if (jsonConfig.TryGetValue("filetype-version", out file_version) && (double)file_version >= 0.3) * { * Debug.WriteLine("file_version: " + ((double)file_version).ToString(), "ExcelSinterConsumer.copyFilesToDisk"); * var modelfileObject = JObject.FromObject(jsonConfig["model"]); * modelfilename = (String)modelfileObject["file"]; * } * else * { * Debug.WriteLine("Key 'filetype-version' wasn't specified or < 0.3", "ExcelSinterConsumer.copyFilesToDisk"); * modelfilename = (String)jsonConfig["spreadsheet"]; * } * * SimpleFile model = job.GetSimulationInputFiles().Single(g => g.name == "spreadsheet"); * cacheFile = Path.Combine(cacheDir, modelfilename); * filepath = Path.Combine(job.Process.WorkingDirectory, modelfilename); * File.WriteAllBytes(cacheFile, model.content); * File.Copy(cacheFile, filepath); * }*/ protected override void copyFilesToDisk(IJobConsumerContract job) { string cacheDir = Path.Combine(AppUtility.GetAppContext().BaseWorkingDirectory, job.SimulationId.ToString()); Directory.CreateDirectory(cacheDir); // NOTE: Excel implementations only // Find 'spreadsheet' // configuration file var input_files = job.GetSimulationInputFiles(); SimpleFile config = input_files.Single(f => f.name == "configuration"); string content = System.Text.Encoding.ASCII.GetString(config.content); var cacheFile = Path.Combine(cacheDir, configFileName); var filepath = Path.Combine(job.Process.WorkingDirectory, configFileName); File.WriteAllBytes(cacheFile, config.content); File.Copy(cacheFile, filepath); Dictionary <String, Object> jsonConfig = JsonConvert.DeserializeObject <Dictionary <String, Object> >(content); object file_version; string excelfilename; // Handling the new format of SinterConfig file if (jsonConfig.TryGetValue("filetype-version", out file_version) && (double)file_version >= 0.3) { Debug.WriteLine("file_version: " + ((double)file_version).ToString(), "ExcelSinterConsumer.copyFilesToDisk"); var excelfileObject = JObject.FromObject(jsonConfig["model"]); excelfilename = (String)excelfileObject["file"]; } else { Debug.WriteLine("Key 'filetype-version' wasn't specified or < 0.3", "ExcelSinterConsumer.copyFilesToDisk"); excelfilename = (String)jsonConfig["spreadsheet"]; } SimpleFile excelfile = input_files.Single(g => g.name == "spreadsheet"); cacheFile = Path.Combine(cacheDir, excelfilename); filepath = Path.Combine(job.Process.WorkingDirectory, excelfilename); File.WriteAllBytes(cacheFile, excelfile.content); File.Copy(cacheFile, filepath); foreach (SimpleFile sf in input_files) { if (sf.name == "configuration" || sf.name == "spreadsheet") { continue; } cacheFile = Path.Combine(cacheDir, sf.name); filepath = Path.Combine(job.Process.WorkingDirectory, sf.name); try { File.WriteAllBytes(cacheFile, sf.content); } catch (DirectoryNotFoundException) { string[] dirs = cacheFile.Split('/'); string[] d = dirs.Take <string>(dirs.Length - 1).ToArray <string>(); string dirpath = Path.Combine(d); Debug.WriteLine(String.Format("copyFilesToDisk: create cache directory {0}", dirpath), GetType().Name); Directory.CreateDirectory(dirpath); File.WriteAllBytes(cacheFile, sf.content); } try { File.Copy(cacheFile, filepath); } catch (DirectoryNotFoundException) { string[] dirs = filepath.Split('/'); string[] d = dirs.Take <string>(dirs.Length - 1).ToArray <string>(); string dirpath = Path.Combine(d); Debug.WriteLine(String.Format("copyFilesToDisk: create directory {0}", dirpath), GetType().Name); Directory.CreateDirectory(dirpath); File.Copy(cacheFile, filepath); } } }
/// <summary> /// DoSetup setup up the working directory, returns an IProcess interface. /// </summary> /// <param name="job"></param> /// <param name="configFileName"></param> /// <returns></returns> private IProcess DoSetup(IJobConsumerContract job) { Debug.WriteLine("DoSetup Called on " + job.Id, "SinterConsumer"); // NOTE: expect OptimisticConcurrencyException // to occur ONLY here when running multiple consumers IProcess process = null; try { process = job.Setup(); } catch (Exception ex) { Debug.WriteLine(ex.Message, "SinterConsumer.DoSetup"); throw; } //Sets the dir in the function to MyDocs, but doesn't actually chdir try { SetupWorkingDir(process); } catch (Exception ex) { Debug.WriteLine("Failed to setup working directory", "SinterConsumer.Run"); Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); job.Error(String.Format("Failed to setup working directory: {0}", ex.StackTrace)); throw; } bool isCached = false; try { isCached = copyFromCache(job); } catch (Exception ex) { Debug.WriteLine("copyFromCache Failed to copy files to working directory", "SinterConsumer.Run"); Debug.WriteLine(ex.StackTrace, "SinterConsumer.Run"); job.Error(String.Format("copyFromCache Failed to copy files to working directory: {0}", ex.StackTrace)); throw; } if (isCached == false) { try { copyFilesToDisk(job); } catch (Exception ex) { Debug.WriteLine("copyFilesToDisk Failed to copy files to working directory", "SinterConsumer.Run"); Debug.WriteLine(ex.StackTrace, "SinterConsumer.Run"); job.Error(String.Format("copyFilesToDisk Failed to copy files to working directory: {0}", ex.StackTrace)); throw; } } job.Message("working directory setup finished"); return(process); }
override protected IJobConsumerContract GetNextJob() { job = new InMemoryBadJob(); return(job); }
/// <summary> /// Run pops a job descriptor off the queue and manages the underlying AspenSinter /// </summary> /// <returns></returns> public bool Run() { // RESET ALL instance variables except sim bool prevJobIsTerminated = isTerminated; isSetup = false; isTerminated = false; running = false; var job = GetNextJob(); this.job = job; if (job == null) { Debug.WriteLine(DateTime.Now.ToString() + " - No submitted jobs in queue", "SinterConsumer.Run"); return(false); } visible = job.Visible; IProcess process = null; if (job.Reset == false && sim != null && prevJobIsTerminated == false && RunNoReset(job)) { Debug.WriteLine(String.Format("{0} - Found JOB(noreset): {1}", DateTime.Now.ToString(), job.Id), "SinterConsumer.Run"); return(true); } Debug.WriteLine(String.Format("{0} - Found JOB(reset): {1}", DateTime.Now.ToString(), job.Id), "SinterConsumer.Run"); CleanUp(); sim = null; Debug.WriteLine(String.Format("Setup Sinter Process {0}", job.Id), "SinterConsumer.Run"); process = DoSetup(job); var maxAttemptsReadSetup = 5; DoConfigure(job, process, maxAttemptsReadSetup); Debug.WriteLine(String.Format("Move Job {0} to state Running", job.Id), "SinterConsumer.Run"); job.Running(); IDictionary <String, Object> inputDict = null; String data = null; JObject defaults = null; JObject inputs = null; // NOTE: Quick Fix. Serializing Dicts into string json // and back to JObject because of API change to sinter. // Better to refactor interfaces, etc to hand back strings try { inputDict = process.Input; data = JsonConvert.SerializeObject(inputDict); inputs = JObject.Parse(data); } catch (System.Data.SqlClient.SqlException ex) { Debug.WriteLine("SqlException: " + ex.Message, "SinterConsumer.Run"); Debug.WriteLine(ex.StackTrace, "SinterConsumer.Run"); sim.closeSim(); sim = null; throw; } catch (Exception ex) { sim.closeSim(); job.Error("Failed to Create Inputs to Simulation (Bad Simulation defaults or Job inputs)"); job.Message(ex.StackTrace.ToString()); throw; } Debug.WriteLine("Initialize", "SinterConsumer.Run"); try { DoInitialize(job, sim, defaults); } catch (System.Data.SqlClient.SqlException ex) { Debug.WriteLine("SqlException: " + ex.Message, "SinterConsumer.Run"); Debug.WriteLine(ex.StackTrace, "SinterConsumer.Run"); sim.closeSim(); sim = null; throw; } catch (Exception ex) { Debug.WriteLine("DoInitialize Exception Close: " + ex.Message, "SinterConsumer.Run"); sim.closeSim(); sim = null; throw; } Debug.WriteLine("Run", "SinterConsumer.Run"); try { DoRun(job, sim, defaults, inputs); } catch (System.Data.SqlClient.SqlException ex) { Debug.WriteLine("SqlException: " + ex.Message, "SinterConsumer.Run"); Debug.WriteLine(ex.StackTrace, "SinterConsumer.Run"); sim.closeSim(); sim = null; throw; } catch (Exception ex) { Debug.WriteLine("DoRun Exception Close", "SinterConsumer.Run"); job.Error("Exception: " + ex.Message); sim.closeSim(); sim = null; throw; } Debug.WriteLine("Finalize", "SinterConsumer.Run"); try { DoFinalize(sim, job, process); last_simulation_name = job.SimulationId; last_simulation_success = job.IsSuccess(); } catch (System.Data.SqlClient.SqlException ex) { Debug.WriteLine("SqlException: " + ex.Message, "SinterConsumer.Run"); Debug.WriteLine(ex.StackTrace, "SinterConsumer.Run"); sim.closeSim(); sim = null; throw; } catch (Exception ex) { Debug.WriteLine("DoFinalize Exception Close", "SinterConsumerRun.Run"); Debug.WriteLine(ex.Message, "SinterConsumerRun.Run"); Debug.WriteLine(ex.StackTrace, "SinterConsumerRun.Run"); job.Error("Exception: " + ex.Message); sim.closeSim(); sim = null; throw; } return(true); }
abstract protected bool RunNoReset(IJobConsumerContract job);
/// <summary> /// DoFinalize sends outputs to the database, sets final run status and /// warnings and/or errors in the job and process. /// </summary> /// <param name="stest"></param> /// <param name="job"></param> /// <param name="process"></param> protected void DoFinalize(sinter.ISimulation stest, IJobConsumerContract job, IProcess process) { int runStatus = (int)stest.runStatus; process.SetStatus(runStatus); IDictionary <string, Object> myDict = null; JObject outputDict; try { var superDict = stest.getOutputs(); outputDict = (JObject)superDict["outputs"]; // HACK: Inefficient Just making it work w/o covariance issues string data = outputDict.ToString(Newtonsoft.Json.Formatting.None); myDict = JsonConvert.DeserializeObject <IDictionary <string, Object> >(data); } catch (Exception ex) { Debug.WriteLine(ex.Message, "SinterConsumer.DoFinalize"); process.Output = new Dictionary <string, Object>() { { "exception", ex.Message } }; job.Error(String.Format("Failed to retrieve the outputs: {0} {1}", ex.Message, ex.StackTrace)); return; } Debug.WriteLine("Outputs: " + myDict, this.GetType()); process.Output = myDict; if (stest.runStatus == sinter.sinter_AppError.si_OKAY) { job.Success(); } else if (stest.runStatus == sinter.sinter_AppError.si_SIMULATION_ERROR) { Debug.WriteLine("Error: si_SIMULATION_ERROR", "SinterConsumerRun.DoFinalize"); sim.closeSim(); sim = null; job.Error(String.Join(",", stest.errorsBasic())); } //else if (stest.runStatus == sinter.sinter_AppError.si_SIMULATION_WARNING) //{ // job.Error(String.Join(",", stest.warningsBasic())); //} else if (stest.runStatus == sinter.sinter_AppError.si_COM_EXCEPTION) { Debug.WriteLine("Error: si_COM_EXCEPTION", "SinterConsumerRun.DoFinalize"); job.Error("COM Exception, Server was Terminated"); } else if (stest.runStatus == sinter.sinter_AppError.si_SIMULATION_NOT_RUN) { Debug.WriteLine("Error: si_SIMULATION_NOT_RUN", "SinterConsumerRun.DoFinalize"); job.Error("Simulation was not Run"); } else { Debug.WriteLine("Error: Unknown", "SinterConsumerRun.DoFinalize"); job.Error(string.Format("Aspen Process Status unknown code {0}.", (int)stest.runStatus)); } }
public IJobConsumerContract GetNext(IConsumerRun run) { apps = run.SupportedApplications; IJobConsumerContract contract = null; if (apps.Length == 0) { throw new JobQueueException("Invalid JobQueue configuration: no applications specified"); } IConsumerContext appContext = AppUtility.GetConsumerContext(); string simulationName = appContext.BindSimulationName; using (ProducerContext db = new ProducerContext()) { Job job = null; foreach (var appName in apps) { Debug.WriteLine("Get Job for Application: " + appName, "DBJobQueue.GetNext"); if (String.IsNullOrEmpty(simulationName)) { job = db.Jobs.OrderByDescending(j => j.Submit). Where(j => j.State == "submit" && j.ConsumerId == run.ConsumerId && j.Simulation.ApplicationName == appName).FirstOrDefault(); if (job == null) { job = db.Jobs.OrderByDescending(j => j.Submit). Where(j => j.State == "submit" && j.ConsumerId == null && j.Simulation.ApplicationName == appName).FirstOrDefault(); } } else { job = db.Jobs.OrderByDescending(j => j.Submit). Where(j => j.State == "submit" && j.ConsumerId == run.ConsumerId && j.Simulation.Name == simulationName && j.Simulation.ApplicationName == appName).FirstOrDefault(); if (job == null) { job = db.Jobs.OrderByDescending(j => j.Submit). Where(j => j.State == "submit" && j.ConsumerId == null && j.Simulation.Name == simulationName && j.Simulation.ApplicationName == appName).FirstOrDefault(); } } var consumer = db.Consumers.Single(c => c.Id == run.ConsumerId); if (job != null) { job.State = "locked"; job.Consumer = consumer; db.SaveChanges(); contract = new ConsumerJobContract(job.Count, run.ConsumerId); Debug.WriteLine(String.Format("Found Job({0}): {1},{2}", job.Id, job.Simulation.Name, job.Simulation.ApplicationName), "DBJobQueue.GetNext"); break; } } } return(contract); }
abstract protected void copyFilesToDisk(IJobConsumerContract job);
public void TestACM_BFBCap_Inputs() { IDictionary <string, object> output; IConsumerRun run = (IConsumerRun) new InMemorySinterConsumer(); Assert.IsFalse(run.IsEngineRunning); Assert.IsFalse(run.IsSimulationInitializing); Assert.IsFalse(run.IsSimulationOpened); ((InMemorySinterConsumer)run).job = new InMemoryJobACM_BFBCap(); ((InMemorySinterConsumer)run).job.Process.Input["UQ_nv"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["rgndx"] = 0.0225; ((InMemorySinterConsumer)run).job.Process.Input["BFBadsT.Lb"] = 4.2; ((InMemorySinterConsumer)run).job.Process.Input["UQ_A3"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["UQ_A2"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["UQ_A1"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["GHXfg.A_exch"] = 5749.66; ((InMemorySinterConsumer)run).job.Process.Input["BFBrgnB.Lb"] = 4.2; ((InMemorySinterConsumer)run).job.Process.Input["UQ_E3"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["UQ_E2"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["UQ_E1"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["UQ_fg_flow"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["rgnDt"] = 12.0; ((InMemorySinterConsumer)run).job.Process.Input["CW_SHXlean.A_exch"] = 400.0; ((InMemorySinterConsumer)run).job.Process.Input["BFBadsM.Lb"] = 4.2; ((InMemorySinterConsumer)run).job.Process.Input["adsN"] = 15.0; ((InMemorySinterConsumer)run).job.Process.Input["rgnN"] = 15.0; ((InMemorySinterConsumer)run).job.Process.Input["adsdx"] = 0.0275; ((InMemorySinterConsumer)run).job.Process.Input["LR_SHXlean.A_exch"] = 7000.0; ((InMemorySinterConsumer)run).job.Process.Input["BFBrgnT.Lb"] = 4.2; ((InMemorySinterConsumer)run).job.Process.Input["adsDt"] = 15.0; ((InMemorySinterConsumer)run).job.Process.Input["rgnlhx"] = 0.08; ((InMemorySinterConsumer)run).job.Process.Input["adslhx"] = 0.4; ((InMemorySinterConsumer)run).job.Process.Input["UQ_dS1"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["UQ_dS2"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["UQ_dS3"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["BFBadsB.Lb"] = 4.2; ((InMemorySinterConsumer)run).job.Process.Input["LR_SHXrich.A_exch"] = 5000.0; ((InMemorySinterConsumer)run).job.Process.Input["UQ_dH1"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["UQ_dH3"] = 1.0; ((InMemorySinterConsumer)run).job.Process.Input["UQ_dH2"] = 1.0; try { Assert.IsTrue(run.Run()); Assert.IsTrue(run.IsSimulationOpened); IJobConsumerContract job = ((InMemorySinterConsumer)run).job; Assert.IsTrue(job.IsSuccess()); output = job.Process.Output; Assert.IsNotNull(output); foreach (KeyValuePair <string, object> kv in output) { Debug.WriteLine(String.Format("{0} : {1}", kv.Key, kv.Value)); } } finally { Debug.WriteLine("Attempt Cleanup"); run.CleanUp(); } Object d; output.TryGetValue("status", out d); JObject statusD = (JObject)d; JToken status = statusD.GetValue("value"); Assert.AreEqual <int>(status.Value <int>(), 0); }
/// <summary> /// Returns true if application of job is supported /// </summary> /// <param name="job"></param> /// <returns></returns> abstract protected bool IsSupported(IJobConsumerContract job);
public void TestAspenPlusThreading() { IDictionary <string, object> output; IConsumerRun run = (IConsumerRun) new InMemorySinterConsumerAspenPlus(); IConsumerMonitor monitor = (IConsumerMonitor) new InMemorySinterConsumerAspenPlusMonitor(); CancellationTokenSource tokenSource = new CancellationTokenSource(); monitor.setConsumerRun(run); Assert.IsFalse(run.IsEngineRunning); Assert.IsFalse(run.IsSimulationInitializing); Assert.IsFalse(run.IsSimulationOpened); try { var taskRun = new Task <Boolean>(() => run.Run(), tokenSource.Token); var taskMonitor = new Task <int>(() => monitor.Monitor(false)); Debug.WriteLine("Start taskRun", this.GetType().Name); taskRun.Start(); //System.Threading.Thread.Sleep(2000); Debug.WriteLine("Start taskMonitor", this.GetType().Name); taskMonitor.Start(); Debug.WriteLine("run.IsSimulationOpened", this.GetType().Name); while (run.IsSimulationOpened == false) { Debug.WriteLine("Waiting to Open", this.GetType().Name); if (taskRun.IsFaulted) { Debug.WriteLine("Message: " + taskRun.Exception.Message); foreach (Exception e in taskRun.Exception.InnerExceptions) { Debug.WriteLine("Inner: " + e.Message); } throw new Exception(String.Format("{0}", taskRun.Exception.Message)); } System.Threading.Thread.Sleep(500); } Debug.WriteLine("Simulation Opened!!!!!", this.GetType().Name); Assert.IsTrue(run.IsSimulationOpened); int sleepInterval = 1000; //Assert.IsTrue(run.Run()); bool finish = false; //finish = taskRun.Wait(sleepInterval); //while (taskMonitor.Wait(sleepInterval) == false) { } if (taskMonitor.IsCompleted) { Debug.WriteLine("TASK MONITOR IS COMPLETED!!!!!!", this.GetType().Name); try { tokenSource.Cancel(); while (taskRun.IsCanceled == false && tokenSource.Token.IsCancellationRequested == false) { Debug.WriteLine("Task is not cancelled yet!!!!!"); } Debug.WriteLine(tokenSource.Token.IsCancellationRequested, "Task is cancelled"); //*/ } catch (Exception e) { Debug.WriteLine("Task is not cancelled!!!!"); Debug.WriteLine(e.Message); Debug.WriteLine(e.StackTrace); } } //Assert.IsTrue(finish); if (finish) { Debug.WriteLine("TASK IS FINISHED!!!!!"); } else { Debug.WriteLine("TASK IS NOT FINISHED!!!!!"); } System.Threading.Thread.Sleep(6000); IJobConsumerContract job = ((InMemorySinterConsumerAspenPlus)run).job; //Assert.IsFalse(taskRun.IsCompleted); //Assert.IsTrue(job.IsSuccess()); //output = job.Process.Output; //Assert.IsNotNull(output); //foreach (KeyValuePair<string, object> kv in output) //{ // Debug.WriteLine(String.Format("{0} : {1}", kv.Key, kv.Value)); //} } finally { Debug.WriteLine("Attempt Cleanup"); run.CleanUp(); } //Object d; //output.TryGetValue("status", out d); //JObject statusD = (JObject)d; //JToken status = statusD.GetValue("value"); //Assert.AreEqual<int>(status.Value<int>(), 0); }
static void Run() { IConsumerRegistrationContract cc = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract(); IConsumerRun run = Turbine.Consumer.AppUtility.GetConsumerRunContract(); IJobQueue queue = cc.Register(run); IJobConsumerContract contract = queue.GetNext(run); jobs_available = (contract != null); if (jobs_available == false) { return; } try { contract.Setup(); } catch (System.Data.Entity.Core.OptimisticConcurrencyException ex) { var msg = String.Format( "Setup Failed({0}): OptimisticConcurrencyException, {1}", contract.Id, ex.Message); Debug.WriteLine(msg, label); throw; } catch (Turbine.Lite.Web.Resources.Contracts.InvalidStateChangeException ex) { var msg = String.Format( "Setup Failed({0}): InvalidStateChangeException, {1}", contract.Id, ex.Message); Debug.WriteLine(msg, label); throw; } Debug.WriteLine(String.Format("{0} Working Directory: {1}", DateTime.Now.ToString(), contract.Process.WorkingDirectory), label); // Execute Job try { contract.Running(); } catch (Exception ex) { contract.Error(String.Format( "threadid:{0}, machine:{1}, exception:{2}", Thread.CurrentThread.ManagedThreadId, System.Environment.MachineName, ex.Message) ); throw; } IProcess process = contract.Process; var inputDict = process.Input; var outputDict = new Dictionary <string, Object>(); foreach (var key in inputDict.Keys.ToArray <string>()) { if (inputDict[key] == null) { continue; } outputDict[key] = String.Format("{0} OUTPUT", inputDict[key]); process.AddStdout(String.Format("Add Output {0}\n", key)); } process.AddStdout("Fake Job Completed"); process.Output = outputDict; contract.Success(); }
abstract protected void InitializeSinter(IJobConsumerContract job, string workingDir, string config);
/// <summary> /// DoConfigure starts up COM process server. Occasionally readSetup fail, this /// will attempt maxAttemptsReadSetup times to open the /// simulation. Also readSetup will hang in unmanaged code /// </summary> /// <param name="job"></param> /// <param name="process"></param> /// <param name="maxAttemptsReadSetup"></param> /// <returns></returns> private void DoConfigure(IJobConsumerContract job, IProcess process, int maxAttemptsReadSetup) { int attempts = 1; int timeout = 1000; string setupString = null; var configFilePath = Path.Combine(process.WorkingDirectory, configFileName); while (true) { Debug.WriteLine(String.Format("Attempt to Read {0}", configFilePath), "SinterConsumer.DoConfigure"); setupString = ""; try { StreamReader inFileStream = new StreamReader(configFilePath); setupString = inFileStream.ReadToEnd(); inFileStream.Close(); } catch (Exception ex) { Debug.WriteLine(ex.Message, "SinterConsumer.DoConfigure"); Debug.WriteLine(ex.StackTrace, "SinterConsumer.DoConfigure"); job.Error(String.Format("Failed to read configuration file: {0}", ex.StackTrace)); throw; } try { InitializeSinter(job, process.WorkingDirectory, setupString); } catch (Exception ex) { Debug.WriteLine(ex.Message, "SinterConsumer.DoConfigure.InitializeSinter"); Debug.WriteLine(ex.StackTrace, "SinterConsumer.DoConfigure.InitializeSinter"); job.Error(String.Format("Failed to InitializeSinter: {0}", ex.StackTrace)); throw; } Debug.WriteLine(String.Format("readSetup {0}", configFilePath), "SinterConsumer.DoConfigure"); Debug.WriteLine(String.Format("model {0}", sim.setupFile.aspenFilename), "SinterConsumer.DoConfigure"); try { lock (this) { sim.openSim(); isSetup = true; if (isTerminated) { throw new TerminateException( "APWN was terminated, try readSetup again"); } } } catch (Exception ex) { //System.Exception: Cannot create ActiveX component var msg = String.Format("Failed to open Simulation ({0}: {1}", attempts, ex.Message); Debug.WriteLine(msg, "SinterConsumer"); Debug.WriteLine(ex.StackTrace, "SinterConsumer"); try { sim.closeSim(); } catch (NullReferenceException e2) { Debug.WriteLine(e2.Message, "SinterConsumer"); Debug.WriteLine(ex.StackTrace, "SinterConsumer"); } if (job.IsTerminated()) { throw new TerminateException("Job was terminated by producer"); } else if (attempts >= maxAttemptsReadSetup) { job.Error(msg); throw; } job.Message(msg); isTerminated = false; attempts += 1; System.Threading.Thread.Sleep(timeout); timeout *= 2; continue; } job.Message("sinter read setup finished"); // One of these commands hangs infrequently.. // However this job will not be retired as above, just abandoned and left in setup. Debug.WriteLine("set simulation layout", "SinterConsumer.DoConfigure"); try { sim.Vis = visible; // operates on Aspen instance } catch (Exception ex) { sim.closeSim(); var msg = String.Format("Failed to Layout Simulation ({0}: {1}", attempts, ex.Message); Debug.WriteLine(msg, "SinterConsumer.DoConfigure"); Debug.WriteLine(ex.StackTrace, "SinterConsumer.DoConfigure"); if (attempts >= maxAttemptsReadSetup) { job.Error(msg); throw; } job.Message(msg); attempts += 1; System.Threading.Thread.Sleep(timeout); timeout *= 2; continue; } Debug.WriteLine("set simulation reset", "SinterConsumer.DoConfigure"); try { sim.dialogSuppress = true; // operates on Aspen instance //sim.resetSim(); // operates on Aspen instance } catch (Exception ex) { sim.closeSim(); var msg = String.Format("Failed to Reset Simulation ({0}: {1}", attempts, ex.Message); Debug.WriteLine(msg, "SinterConsumer.DoConfigure"); Debug.WriteLine(ex.StackTrace, "SinterConsumer.DoConfigure"); if (attempts >= maxAttemptsReadSetup) { job.Error(msg); throw; } job.Message(msg); attempts += 1; System.Threading.Thread.Sleep(timeout); timeout *= 2; continue; } break; } }