public void TestRunNoJobs() { IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract(); Assert.IsNull(contract.Queue); contract.Register(); Turbine.Consumer.Contract.SinterConsumer consumer = new Turbine.Consumer.Excel.ExcelSinterConsumer(); consumer.Run(); }
public void TestDBJobQueue() { ISimulationProducerContract simulation_contract = Utility.CreateExcelSimulation("exceltest.xlsm", "exceltest-sinter.json"); var producerJob = simulation_contract.NewJob(Guid.NewGuid(), false, false); producerJob.Submit(); IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract(); //Assert.IsNull(contract.Queue); contract.Register(); IJobQueue queue = new Turbine.Consumer.Data.Contract.DBJobQueue(new string[] { "excel" }); var consumerJob = queue.GetNext(); Assert.IsTrue(consumerJob.ApplicationName == "excel", String.Format("Wrong Application {0}", consumerJob.ApplicationName)); }
public void TestConsumerRegistrationContract() { // NHibernate IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract(); contract.Register(); Guid guid = Turbine.Consumer.AppUtility.GetConsumerContext().Id; // EF Turbine.Data.Serialize.Consumer obj = Turbine.Data.Marshal.DataMarshal.GetConsumer(guid); Assert.AreEqual <string>(guid.ToString(), obj.Id); Assert.AreEqual <string>("up", obj.status); contract.UnRegister(); obj = Turbine.Data.Marshal.DataMarshal.GetConsumer(guid); Assert.AreEqual <string>("down", obj.status); }
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(); }
void ScheduledRun() { //contract = new Turbine.Consumer.Data.Contract.ConsumerRegistrationContract(); contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract(); consumerRun = Turbine.Consumer.AppUtility.GetConsumerRunContract(); consumerMonitor = Turbine.Consumer.AppUtility.GetConsumerMonitorContract(); consumerMonitor.setConsumerRun(consumerRun); try { contract.Register(consumerRun); } catch (System.Data.UpdateException ex) { // TODO: Ask to be shutdown Debug.WriteLine("Failed Register UpdateException: " + ex, "AspenSinter"); Debug.WriteLine("InnerException: " + ex.InnerException, "AspenSinter"); Environment.Exit(1); } catch (Exception ex) { // TODO: Ask to be shutdown Debug.WriteLine("Failed to Register: " + ex.Message, "AspenSinter"); Debug.WriteLine(ex.StackTrace); Environment.Exit(1); } // Run KeepAlive task var ktask = new Task <bool>(() => KeepAlive()); ktask.Start(); try { _ScheduledRun(); } catch (System.Data.UpdateException ex) { // TODO: Ask to be shutdown Debug.WriteLine("Failed ScheduledRun UpdateException: " + ex, "AspenSinter"); Debug.WriteLine("InnerException: " + ex.InnerException); throw; } catch (Exception ex) { Debug.WriteLine("Failed ScheduledRun: " + ex.Message, "AspenSinter"); Debug.WriteLine(ex.StackTrace); contract.Error(); Debug.WriteLine("SERVICE GOING TO EXIT", "AspenSinter"); Environment.Exit(1); } try { contract.UnRegister(); } catch (System.Data.UpdateException ex) { // TODO: Ask to be shutdown Debug.WriteLine("Failed UnRegister UpdateException: " + ex.Message, "AspenSinter"); Debug.WriteLine("InnerException: " + ex.InnerException, "AspenSinter"); } catch (Exception ex) { // TODO: Ask to be shutdown Debug.WriteLine("Failed UnRegister: " + ex.Message, "AspenSinter"); Debug.WriteLine(ex.StackTrace, "AspenSinter"); } if (shutdown) { Debug.WriteLine("CONTRACT SHUTDOWN", "AspenSinter"); shutdownProcess = System.Diagnostics.Process.Start("shutdown", "/s /t 10"); } else if (!stop) { Debug.WriteLine("SERVICE GOING TO EXIT", "AspenSinter"); Environment.Exit(1); } }
public void TestSessionProducerConsumerContracts() { // producers ISessionProducerContract producer = new AspenSessionProducerContract(); Guid session_id = producer.Create(); Assert.IsNotNull(session_id); ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("testX", "AspenPlus"); var bytes = File.ReadAllBytes("mea-sinter.txt"); Assert.IsNotNull(bytes); Assert.IsTrue(bytes.Length > 0); System.Threading.Thread.Sleep(100); simulation_contract.UpdateInput("configuration", bytes, "plain/text"); bytes = File.ReadAllBytes("mea.bkp"); Assert.IsNotNull(bytes); Assert.IsTrue(bytes.Length > 0); System.Threading.Thread.Sleep(100); simulation_contract.UpdateInput("aspenfile", bytes, "plain/text"); IJobProducerContract job_producer_contract = simulation_contract.NewJob(session_id, false, true); job_producer_contract = simulation_contract.NewJob(session_id, false, true); IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract(); contract.Register(); IJobConsumerContract job = contract.Queue.GetNext(); Assert.IsNull(job); job_producer_contract.Submit(); job = contract.Queue.GetNext(); Assert.IsNotNull(job); Assert.AreEqual(job.Id, job_producer_contract.Id); job.Setup(); SimpleFile backup = null; SimpleFile config = null; foreach (var f in job.GetSimulationInputFiles()) { if (f.name == "configuration") { config = f; } else { backup = f; } } Assert.AreEqual(config.name, "configuration"); String filename = SinterHelperFunctions .getBackupFilename(System.Text.Encoding.ASCII.GetString(config.content)); Assert.AreEqual(backup.name, "aspenfile"); // NEED TO SET INPUT Before Setting to Run //j try { job.Running(); Assert.Fail("Job.Process.Input Must be set before setting job to run"); } catch (InvalidStateChangeException) {} job.Process.Input = new Dictionary <string, Object>(); job.Running(); job.Success(); job = contract.Queue.GetNext(); Assert.IsNull(job); }
public void TestJobProducerConsumerContracts() { // producers ISessionProducerContract producer = new AspenSessionProducerContract(); Guid session_id = producer.Create(); Assert.IsNotNull(session_id); ISimulationProducerContract simulation_contract; simulation_contract = AspenSimulationContract.Create("test", "AspenPlus"); Assert.IsTrue(File.Exists("mea-sinter.txt")); Assert.IsTrue(File.Exists("mea.bkp")); var configContent = File.ReadAllBytes("mea-sinter.txt"); var aspenfileContent = File.ReadAllBytes("mea.bkp"); Assert.IsTrue(configContent.Length > 0); Assert.IsTrue(aspenfileContent.Length > 0); System.Threading.Thread.Sleep(100); simulation_contract.UpdateInput("configuration", configContent, "plain/text"); System.Threading.Thread.Sleep(1000); simulation_contract.UpdateInput("aspenfile", aspenfileContent, "plain/text"); IJobProducerContract job_producer_contract = simulation_contract.NewJob(session_id, false, true); // consumers //IJobQueue queue = AppUtility.GetJobQueue(); // NHibernate IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract(); contract.Register(); IJobConsumerContract job = contract.Queue.GetNext(); Assert.IsNull(job); job_producer_contract.Submit(); job = contract.Queue.GetNext(); Assert.IsNotNull(job); job.Setup(); IEnumerable <SimpleFile> files = job.GetSimulationInputFiles(); Assert.AreEqual <int>(files.Count <SimpleFile>(), 2); foreach (var f in files) { Assert.IsNotNull(f); Assert.IsNotNull(f.name); Debug.WriteLine("File: " + f.name, this.GetType().Name); Assert.IsNotNull(f.content); Assert.IsTrue(f.content.Length > 0); if (f.name == "configuration") { Assert.AreEqual <int>(f.content.Length, configContent.Length); } else if (f.name == "aspenfile") { Assert.AreEqual <int>(f.content.Length, aspenfileContent.Length); } else { Assert.Fail(String.Format("Unknown File {0}", f.name)); } } //byte[] backup = job.GetSimulationBackup(); //byte[] config = job.GetSimulationConfiguration(); //Assert(, //IJobConsumerContract consumer = AppUtility.GetJobConsumerContract(id); }
public void TestJobProducerConsumerContractsSetupFailUnRegisterConsumer() { // Create test Simulation TestSimulationProducerContract(); ISimulationProducerContract simulation_contract = AspenSimulationContract.Get("test"); ISessionProducerContract producer = new AspenSessionProducerContract(); Guid session_id = producer.Create(); IJobProducerContract job_producer_contract = simulation_contract.NewJob(session_id, false, true); // consumers /* * IJobQueue queue = AppUtility.GetJobQueue(); * IJobConsumerContract job = queue.GetNext(); * Assert.IsNull(job); * job_producer_contract.Submit(); * job = queue.GetNext(); * Assert.IsNotNull(job); */ // NHibernate IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract(); Assert.IsNull(contract.Queue); contract.Register(); Assert.IsNotNull(contract.Queue); Assert.IsNull(contract.Queue.GetNext()); // nothing on queue job_producer_contract.Submit(); IJobConsumerContract job = contract.Queue.GetNext(); Assert.IsNotNull(job); contract.UnRegister(); job.Setup(); SimpleFile backup = null; SimpleFile config = null; foreach (var f in job.GetSimulationInputFiles()) { if (f.name == "configuration") { config = f; } else { backup = f; } } Assert.AreEqual(config.name, "configuration"); String filename = SinterHelperFunctions .getBackupFilename(System.Text.Encoding.ASCII.GetString(config.content)); Assert.AreEqual(backup.name, ""); //Assert(, //IJobConsumerContract consumer = AppUtility.GetJobConsumerContract(id); }
public static void Run() { Debug.WriteLine("SinterCosnumerConsole : Running"); int timeSleepInterval = 1000; //int iterations = 60 * 30; //int setupIterations = 60 * 10; //int postInitIterations = 60 + setupIterations; bool finish = false; String dir = AppUtility.GetAppContext().BaseWorkingDirectory; IConsumerContext consumerCtx = AppUtility.GetConsumerContext(); // Register as a consumer, else can't use JobContract cc = AppUtility.GetConsumerRegistrationContract(); consumerRun = AppUtility.GetConsumerRunContract(); consumerMonitor = AppUtility.GetConsumerMonitorContract(); consumerMonitor.setConsumerRun(consumerRun); cc.Register(consumerRun); //consumer = AppUtility.GetConsumerRunContract(); // Run KeepAlive task var ktask = new Task <bool>(() => KeepAlive()); ktask.Start(); bool stop = false; bool cancelKeyPressed = false; var obj = new Object(); System.Console.CancelKeyPress += delegate { lock (obj) { stop = true; cancelKeyPressed = true; //consumer.CleanUp(); Debug.WriteLine("Exit Unregister Consumer ", "SinterConsumerConsole"); try { cc.UnRegister(); } catch (Exception ex) { Debug.WriteLine("Exception caught from UnRegister: " + ex.Message, "SinterConsumerConsole"); Debug.WriteLine(ex.StackTrace, "SinterConsumerConsole"); } // lock will block if "SinterConsumer.DoConfigure --> sim.openSim()" is being called Debug.WriteLine("lock consumer", "SinterConsumerConsole"); int wait_seconds = 30; if (Monitor.TryEnter(consumerRun, new TimeSpan(0, 0, wait_seconds))) { consumerMonitor.Monitor(cancelKeyPressed); Debug.WriteLine("Obtained ConsumerRun Lock: Monitor Terminate", "SinterConsumerConsole"); } else { consumerMonitor.Monitor(cancelKeyPressed); Debug.WriteLine("Failed to Obtain ConsumerRun Lock: Monitor Terminate", "SinterConsumerConsole"); } Debug.WriteLine("Consumer Exit", "SinterConsumerConsole"); } }; Task <int> taskMonitor = null; bool taskMonitorFinished = true; while (stop == false) { if (!Directory.Exists(dir)) { Debug.WriteLine(String.Format("Base Directory does not exist: {0}", dir), "SinterConsumerConsole"); OnExit(ERROR_PATH_NOT_FOUND); } try { var acl = Directory.GetAccessControl(dir); } catch (UnauthorizedAccessException) { Debug.WriteLine(String.Format("Base Directory is not writable: {0}", dir), "SinterConsumerConsole"); OnExit(ERROR_ACCESS_DENIED); } Debug.WriteLine("> New Task", "SinterConsumerConsole"); var taskRun = new Task <Boolean>(() => consumerRun.Run()); if (taskMonitorFinished) { Debug.WriteLine("> New Monitor Task", "SinterConsumerConsole.Run"); taskMonitorFinished = false; taskMonitor = new Task <int>(() => consumerMonitor.Monitor(false)); taskMonitor.Start(); } finish = false; int i = 0; bool hasInitialized = false; bool hasRun = false; taskRun.Start(); while (stop == false) { if (taskMonitor.IsCompleted) { Debug.WriteLine("taskMonitor.IsCompleted", "SinterConsumerConsole.Run"); taskMonitorFinished = true; System.Threading.Thread.Sleep(2000); break; } if (cc.GetStatus() == "down") { Debug.WriteLine("ConsumerContract: Consumer is Down", "SinterConsumerConsole.Run"); stop = true; Debug.WriteLine("Exit Unregister Consumer ", "SinterConsumerConsole.Run"); try { cc.UnRegister(); } catch (Exception ex) { Debug.WriteLine("Exception caught from UnRegister: " + ex.Message, "SinterConsumerConsole.Run"); Debug.WriteLine(ex.StackTrace, "SinterConsumerConsole.Run"); } // lock will block if "SinterConsumer.DoConfigure --> sim.openSim()" is being called Debug.WriteLine("lock consumer", "SinterConsumerConsole.Run"); int wait_seconds = 30; if (Monitor.TryEnter(consumerRun, new TimeSpan(0, 0, wait_seconds))) { consumerMonitor.Monitor(true); Debug.WriteLine("Obtained ConsumerRun Lock: Monitor Terminate", "SinterConsumerConsole.Run"); } else { consumerMonitor.Monitor(true); Debug.WriteLine("Failed to Obtain ConsumerRun Lock: Monitor Terminate", "SinterConsumerConsole.Run"); } break; } i++; Debug.WriteLine("top"); try { finish = taskRun.Wait(timeSleepInterval); } catch (Exception ex) { Debug.WriteLine("Exception caught from Run: " + ex.Message, "SinterConsumerConsole"); Debug.WriteLine(ex.StackTrace, "SinterConsumerConsole"); Debug.WriteLine("InnerException: " + ex.InnerException, "SinterConsumerConsole"); System.Threading.Thread.Sleep(2000); /*// check if job was terminated, InnerException would be InvalidStateChangeException * if (ex.InnerException is InvalidStateChangeException) * { * consumer.CheckTerminate(); * } */ break; } //Debug.WriteLine(">> Waiting!!!! " + i + " " + iterations + " " + setupIterations + " " + postInitIterations, "SinterConsumerConsole"); Debug.WriteLine(">> Waiting " + i, "SinterConsumerConsole"); if (finish) { if (taskRun.IsFaulted) { Debug.WriteLine("Faulted: " + taskRun.Exception, "SinterConsumerConsole"); } else if (taskRun.IsCompleted) { Debug.WriteLine("Completed", "SinterConsumerConsole"); } else { throw new Exception("Task Unexpected status?"); } if (!taskRun.Result) { // Nothing on Job Queue System.Threading.Thread.Sleep(4000); } break; } // DETECT HANGS if (consumerMonitor.IsSimulationInitializing) { hasInitialized = true; Debug.WriteLine("> Simulation Initializing: " + setupIterations); if (i >= setupIterations) { // Allows SinterConsumer to attempt to reopen the simulation // IF attempts max out in SinterConsumer, throw Exception // and task.IsFaulted and job will move to error Debug.WriteLine("HANG SETUP, TRY AGAIN", "SinterConsumerConsole"); consumerMonitor.MonitorTerminate(); Debug.WriteLine("Killed Aspen Process", "SinterConsumerConsole"); i = 0; continue; } } else if (consumerMonitor.IsEngineRunning) { hasRun = true; Debug.WriteLine("> Engine Running: " + iterations); if (i >= iterations) { // Assume Simulation is done Initializing Debug.WriteLine("HANG RUNNING EXIT", "SinterConsumerConsole"); //consumer.MonitorTerminateAPWN(); try { //consumer.MonitorTerminateAspenEngine(); //consumer.MonitorTerminateRunning(); consumerMonitor.MonitorTerminate(); } catch (TerminateException ex) { Debug.WriteLine(ex.Message, "SinterConsumerConsole"); } } } else if (hasInitialized && !hasRun) { // Stuck if (i >= postInitIterations) { // Assume Simulation is done Initializing Debug.WriteLine("HANG POST INIT, TRY AGAIN", "SinterConsumerConsole"); consumerMonitor.MonitorTerminate(); Debug.WriteLine("Killed Aspen instances", "SinterConsumerConsole"); i = 0; continue; } } else if (i % 60 == 0) { Debug.WriteLine(String.Format("Waiting {0}", i), "SinterConsumerConsole"); } } // end of while Debug.WriteLine("Stop called", "SinterConsumerConsole"); } // end of while lock (obj) { Debug.WriteLine("Stop and cleanup", "SinterConsumerConsole"); consumerMonitor.MonitorTerminate(); } //consumer.CleanUp(); //OnExit(ERROR_SUCCESS); }
public void TestExcelSpreadsheet1() { var app = ApplicationProducerContract.Get("Excel"); Assert.IsNotNull(app); ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("test", "Excel"); Assert.IsNotNull(simulation_contract); byte[] data; using (var fstream = File.Open(@"exceltest.xlsm", FileMode.Open)) { using (var ms = new MemoryStream()) { fstream.CopyTo(ms); data = ms.ToArray(); } } Assert.IsTrue(data.Length > 0); System.Threading.Thread.Sleep(10); simulation_contract.UpdateInput("spreadsheet", data, "plain/text"); data = null; using (var fstream = File.Open("exceltest-sinter.json", FileMode.Open)) { using (var ms = new MemoryStream()) { fstream.CopyTo(ms); data = ms.ToArray(); } } Assert.IsTrue(data.Length > 0); System.Threading.Thread.Sleep(10); simulation_contract.UpdateInput("configuration", data, "plain/text"); System.Threading.Thread.Sleep(10); var guid = Guid.NewGuid(); var job_producer_contract = simulation_contract.NewJob(guid, false, false); job_producer_contract.Process.Input = new Dictionary <string, Object>() { }; job_producer_contract.Submit(); System.Threading.Thread.Sleep(10); IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract(); Assert.IsNull(contract.Queue); contract.Register(); Turbine.Consumer.Contract.SinterConsumer consumer = new Turbine.Consumer.Excel.ExcelSinterConsumer(); //Utility.CheckBaseDirectory(); consumer.Run(); int jobID = job_producer_contract.Id; Debug.WriteLine("Job GUID" + guid.ToString(), GetType().Name); Dictionary <string, Object> json = null; using (Turbine.Data.TurbineModelContainer container = new Turbine.Data.TurbineModelContainer()) { Turbine.Data.Job entity = container.Jobs.Single(s => s.Id == job_producer_contract.Id); json = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(entity.Process.Output); } string output = Newtonsoft.Json.JsonConvert.SerializeObject(json); Debug.WriteLine("OUTPUT: " + output, GetType().Name); }