Exemplo 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();
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
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);
        }
        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);
        }