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)); }
/* AppendJobs * * Each job request has a "Simulation" and "Input" * */ public List <int> AppendJobs(string sessionID, System.IO.Stream data) { Debug.WriteLine(String.Format("append jobs to session {0}", sessionID), this.GetType().Name); Object name = ""; Object inputs = ""; Object initialize = false; Object reset = true; StreamReader sr = new StreamReader(data); string json = sr.ReadToEnd(); List <Dictionary <string, Object> > jobsInputList = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Dictionary <string, Object> > >(json); List <Dictionary <String, Object> > jobDictList = new List <Dictionary <string, object> >(); List <string> simulationList = new List <string>(); List <bool> initializeList = new List <bool>(); List <bool> resetList = new List <bool>(); foreach (var inputData in jobsInputList) { if (!inputData.TryGetValue("Simulation", out name)) { Debug.WriteLine("Missing simulation name", this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Simulation is required" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } if (!inputData.TryGetValue("Input", out inputs)) { Debug.WriteLine("Missing input data", this.GetType().Name); throw new ArgumentException("No Input data name"); } if (!inputData.TryGetValue("Initialize", out initialize)) { initialize = false; } if (!inputData.TryGetValue("Reset", out reset)) { reset = true; } Newtonsoft.Json.Linq.JObject inputsD = null; try { inputsD = (Newtonsoft.Json.Linq.JObject)inputs; } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Input is required to be a dictionary" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } Dictionary <String, Object> dd = new Dictionary <string, object>(); foreach (var v in inputsD) { Debug.WriteLine("VALUE: " + v); dd[v.Key] = v.Value; } jobDictList.Add(dd); simulationList.Add(String.Format("{0}", name)); try { initializeList.Add((bool)initialize); } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key initialize is required to be a boolean" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } try { resetList.Add((bool)reset); } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Reset is required to be a boolean" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } } List <int> jobList = new List <int>(); // Formatting complete for (int i = 0; i < jobDictList.Count; i++) { ISimulationProducerContract contract = null; string simulationName = simulationList[i]; Dictionary <String, Object> dd = jobDictList[i]; try { contract = AspenSimulationContract.Get(simulationName); } catch (Exception ex) { string msg = String.Format("Failed to get Simulation {0}: {1}", name, ex.StackTrace.ToString()); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } IJobProducerContract jobContract; try { jobContract = contract.NewJob(new Guid(sessionID), initializeList[i], resetList[i]); } catch (Exception ex) { string msg = String.Format("Failed to create new job {0}: {1}", i, ex.StackTrace.ToString()); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } jobContract.Process.Input = dd; jobList.Add(jobContract.Id); } return(jobList); }
public Stream CreateJob(Stream data) { Debug.WriteLine("create new job", this.GetType().Name); StreamReader sr = new StreamReader(data); string json = sr.ReadToEnd(); Debug.WriteLine("JSON: " + json, this.GetType().Name); Dictionary <string, Object> inputData = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(json); Object name = ""; Guid sessionID; if (!inputData.TryGetValue("Session", out name)) { Debug.WriteLine("Missing Session", this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Session is required" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } try { sessionID = Guid.Parse((string)name); } catch (Exception) { Debug.WriteLine("Bad Session", this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Bad Session ID, must be GUID" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } if (!inputData.TryGetValue("Simulation", out name)) { Debug.WriteLine("Missing simulation name", this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Simulation is required" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } Object tmp = false; if (!inputData.TryGetValue("initialize", out tmp)) { tmp = false; } bool initialize; try { initialize = (bool)tmp; } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key initialize is required to be a boolean" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } if (!inputData.TryGetValue("reset", out tmp)) { tmp = true; } bool reset; try { reset = (bool)tmp; } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key reset is required to be a boolean" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } Debug.WriteLine("Retrieve Simulation Contract: " + name, this.GetType().Name); ISimulationProducerContract contract = null; try { contract = AspenSimulationContract.Get((string)name); } catch (Exception ex) { string msg = String.Format("Failed to get Simulation {0}: {1}", name, ex.StackTrace.ToString()); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } Object inputs; if (!inputData.TryGetValue("Inputs", out inputs)) { Debug.WriteLine("Missing input data", this.GetType().Name); throw new ArgumentException("No Input data name"); } Debug.WriteLine("Found Inputs", this.GetType().Name); Newtonsoft.Json.Linq.JObject inputsD = null; try { inputsD = (Newtonsoft.Json.Linq.JObject)inputs; } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Inputs is required to be a dictionary" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } Debug.WriteLine("Cast Inputs", this.GetType().Name); Dictionary <String, Object> dd = new Dictionary <string, object>(); foreach (var v in inputsD) { Debug.WriteLine("VALUE: " + v); dd[v.Key] = v.Value; } var jobContract = contract.NewJob(sessionID, initialize, reset); jobContract.Process.Input = dd; var d = DataMarshal.GetJobDict(jobContract.Id, true); json = Newtonsoft.Json.JsonConvert.SerializeObject(d); return(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json))); }
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 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); }
/// <summary> /// Copy Session to a new session with the desired job state. /// </summary> /// <param name="session"></param> /// <returns></returns> public Dictionary <string, string> Copy(Guid sessionId, string state) { ISimulationProducerContract contract = null; List <int> jobList = new List <int>(); Guid sessionIdNew = Guid.NewGuid(); string owner = Container.GetAppContext().UserName; using (var db = new ProducerContext()) { User user = db.Users.Single <User>(s => s.Name == owner); var session = new Session() { Id = sessionIdNew, User = user, Create = DateTime.UtcNow }; db.Sessions.Add(session); db.SaveChanges(); } using (var db = new ProducerContext()) { var sessionJobs = db.Jobs.Where(j => j.SessionId == sessionId).Where(j => j.State == state); Debug.WriteLine("Simulation Name: " + sessionJobs.First().Simulation.Name, "ProducerSessionContract.Copy"); contract = ProducerSimulationContract.Get(sessionJobs.First().Simulation.Name); foreach (var job in sessionJobs) { Debug.WriteLine("job: " + job.Count.ToString(), "ProducerSessionContract.Copy"); var input = Newtonsoft.Json.Linq.JObject.Parse(job.Process.Input); Dictionary <String, Object> dd = new Dictionary <string, object>(); foreach (var v in input) { Debug.WriteLine("VALUE: " + v); Debug.WriteLine("Key: " + v.Key + ", Value: " + v.Value); dd[v.Key] = v.Value; } Debug.WriteLine("Creating jobContract", "ProducerSessionContract.Copy"); IJobProducerContract jobContract; jobContract = contract.NewJob(sessionIdNew, job.Initialize, job.Reset, job.Visible); jobContract.Process.Input = dd; Debug.WriteLine("Adding jobContract Count to jobList", "ProducerSessionContract.Copy"); jobList.Add(jobContract.Id); } } var d = new Dictionary <string, string>(); d.Add("NewSessionGuid", sessionIdNew.ToString()); d.Add("NewJobIds", string.Join(",", jobList.ToArray())); return(d); }
/* * * /// <summary> * /// * /// </summary> * /// <param name="sessionID"></param> * /// <returns></returns> * public int KillSession(string sessionID) * { * ISessionProducerContract contract = new AspenSessionProducerContract(); * int total = 0; * * try * { * total += contract.Cancel(new Guid(sessionID)); * total += contract.Terminate(new Guid(sessionID)); * } * catch (AuthorizationError ex) * { * var detail = new ErrorDetail( * ErrorDetail.Codes.AUTHORIZATION_ERROR, * ex.Message * ); * throw new WebFaultException<ErrorDetail>(detail, System.Net.HttpStatusCode.Unauthorized); * } * return total; * } */ /// <summary> /// Append new Jobs to session, in 'Create' state. /// </summary> /// <param name="sessionID"></param> /// <param name="data">list of job requests [ {"Simulation":.., "Input":..., "Initialize":..., "Reset":..., } ]</param> /// <returns></returns> public List <int> AppendJobs(string sessionID, System.IO.Stream data) { Debug.WriteLine(String.Format("append jobs to session {0}", sessionID), this.GetType().Name); Object name = ""; Object inputs = ""; Object initialize = false; Object reset = true; Object visible = false; Object jobId = ""; Object consumerId = ""; StreamReader sr = new StreamReader(data); string json = sr.ReadToEnd(); List <Dictionary <string, Object> > jobsInputList = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Dictionary <string, Object> > >(json); List <Dictionary <String, Object> > jobDictList = new List <Dictionary <string, object> >(); List <string> simulationList = new List <string>(); List <bool> initializeList = new List <bool>(); List <bool> resetList = new List <bool>(); List <bool> visibleList = new List <bool>(); List <Guid> jobIdList = new List <Guid>(); List <Guid> consumerIdList = new List <Guid>(); foreach (var inputData in jobsInputList) { if (!inputData.TryGetValue("Simulation", out name)) { Debug.WriteLine("Missing simulation name", this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Simulation is required" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } if (!inputData.TryGetValue("Input", out inputs)) { Debug.WriteLine("Missing input data", this.GetType().Name); throw new ArgumentException("No Input data name"); } if (!inputData.TryGetValue("Initialize", out initialize)) { initialize = false; } if (!inputData.TryGetValue("Reset", out reset)) { reset = true; } if (!inputData.TryGetValue("Visible", out visible)) { visible = false; } if (inputData.TryGetValue("Guid", out jobId)) { try { jobIdList.Add(Guid.Parse((string)jobId)); } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key jobId is required to be a string/GUID" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } catch (FormatException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, String.Format("Key jobId {0} is required to be a formatted as a GUID", jobId) ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } } else { jobIdList.Add(Guid.Empty); } if (inputData.TryGetValue("ConsumerId", out consumerId)) { try { consumerIdList.Add(Guid.Parse((string)consumerId)); } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key ConsumerId is required to be a string/GUID" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } catch (FormatException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, String.Format("Key ConsumerId {0} is required to be a formatted as a GUID", consumerId) ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } } else { consumerIdList.Add(Guid.Empty); } Newtonsoft.Json.Linq.JObject inputsD = null; try { inputsD = (Newtonsoft.Json.Linq.JObject)inputs; } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Input is required to be a dictionary" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } Dictionary <String, Object> dd = new Dictionary <string, object>(); foreach (var v in inputsD) { //Debug.WriteLine("VALUE: " + v); dd[v.Key] = v.Value; } jobDictList.Add(dd); simulationList.Add(String.Format("{0}", name)); try { Debug.WriteLine("initialize String: " + initialize.ToString(), "SessionResource.AppendJobs"); Debug.WriteLine("initialize Type: " + initialize.GetType().ToString(), "SessionResource.AppendJobs"); initializeList.Add(Convert.ToBoolean(initialize)); } catch (InvalidCastException e) { Debug.WriteLine("Error: " + e.Message, "SessionResource.AppendJobs"); Debug.WriteLine("StackTrace: " + e.StackTrace, "SessionResource.AppendJobs"); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key initialize is required to be a boolean" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } try { resetList.Add(Convert.ToBoolean(reset)); } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Reset is required to be a boolean" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } try { visibleList.Add(Convert.ToBoolean(visible)); } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Visible is required to be a boolean" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } } List <int> jobList = new List <int>(); // Formatting complete for (int i = 0; i < jobDictList.Count; i++) { ISimulationProducerContract contract = null; string simulationName = simulationList[i]; Dictionary <String, Object> dd = jobDictList[i]; try { contract = ProducerSimulationContract.Get(simulationName); } catch (Exception ex) { string msg = String.Format("Failed to get Simulation {0}: {1}", name, ex.StackTrace.ToString()); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } if (contract == null) { string msg = String.Format("Failed to create new job {0}: No Simulation {1}", i, simulationName); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.NotFound); } IJobProducerContract jobContract; try { jobContract = contract.NewJob(new Guid(sessionID), initializeList[i], resetList[i], visibleList[i], jobIdList[i], consumerIdList[i]); jobContract.Process.Input = dd; } catch (Exception ex) { string msg = String.Format("Failed to create new job {0}: {1} {2}", i, ex, ex.StackTrace.ToString()); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } jobList.Add(jobContract.Id); } return(jobList); }
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); }