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

            Directory.CreateDirectory(cacheDir);
            // NOTE: Aspen implementations only
            // Find 'aspenfile'
            // 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);
            string aspenfilename = (String)jsonConfig["aspenfile"];

            SimpleFile aspenfile = input_files.Single(g => g.name == "aspenfile");

            cacheFile = Path.Combine(cacheDir, aspenfilename);
            filepath  = Path.Combine(job.Process.WorkingDirectory, aspenfilename);
            File.WriteAllBytes(cacheFile, aspenfile.content);
            File.Copy(cacheFile, filepath);

            foreach (SimpleFile sf in input_files)
            {
                if (sf.name == "configuration" || sf.name == "aspenfile")
                {
                    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);
                }
            }
        }