Esempio n. 1
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();
        }
Esempio n. 2
0
        /// <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);
        }
        /// <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);
        }
        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);
        }