/// <summary>
 /// isJobTerminated:  Checks if the job is terminated
 /// </summary>
 /// <param name="job"></param>
 /// <returns></returns>
 public bool isJobTerminated()
 {
     if (job != null)
     {
         return(job.IsTerminated());
     }
     return(false);
 }
        /// <summary>
        ///  DoConfigure starts up COM process server.  Occasionally readSetup fail, this
        ///  will attempt maxAttemptsReadSetup times to open the
        ///  simulation.  Also readSetup will hang in unmanaged code
        /// </summary>
        /// <param name="job"></param>
        /// <param name="process"></param>
        /// <param name="maxAttemptsReadSetup"></param>
        /// <returns></returns>
        private void DoConfigure(IJobConsumerContract job,
                                 IProcess process, int maxAttemptsReadSetup)
        {
            int    attempts    = 1;
            int    timeout     = 1000;
            string setupString = null;

            var configFilePath = Path.Combine(process.WorkingDirectory, configFileName);

            while (true)
            {
                Debug.WriteLine(String.Format("Attempt to Read {0}", configFilePath),
                                "SinterConsumer.DoConfigure");
                setupString = "";
                try
                {
                    StreamReader inFileStream = new StreamReader(configFilePath);
                    setupString = inFileStream.ReadToEnd();
                    inFileStream.Close();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message, "SinterConsumer.DoConfigure");
                    Debug.WriteLine(ex.StackTrace, "SinterConsumer.DoConfigure");
                    job.Error(String.Format("Failed to read configuration file: {0}", ex.StackTrace));
                    throw;
                }

                try
                {
                    InitializeSinter(job, process.WorkingDirectory, setupString);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message, "SinterConsumer.DoConfigure.InitializeSinter");
                    Debug.WriteLine(ex.StackTrace, "SinterConsumer.DoConfigure.InitializeSinter");
                    job.Error(String.Format("Failed to InitializeSinter: {0}", ex.StackTrace));
                    throw;
                }

                Debug.WriteLine(String.Format("readSetup {0}", configFilePath),
                                "SinterConsumer.DoConfigure");
                Debug.WriteLine(String.Format("model {0}", sim.setupFile.aspenFilename),
                                "SinterConsumer.DoConfigure");

                try
                {
                    lock (this)
                    {
                        sim.openSim();
                        isSetup = true;
                        if (isTerminated)
                        {
                            throw new TerminateException(
                                      "APWN was terminated, try readSetup again");
                        }
                    }
                }
                catch (Exception ex)
                {
                    //System.Exception: Cannot create ActiveX component
                    var msg = String.Format("Failed to open Simulation ({0}: {1}", attempts, ex.Message);
                    Debug.WriteLine(msg, "SinterConsumer");
                    Debug.WriteLine(ex.StackTrace, "SinterConsumer");
                    try
                    {
                        sim.closeSim();
                    }
                    catch (NullReferenceException e2)
                    {
                        Debug.WriteLine(e2.Message, "SinterConsumer");
                        Debug.WriteLine(ex.StackTrace, "SinterConsumer");
                    }
                    if (job.IsTerminated())
                    {
                        throw new TerminateException("Job was terminated by producer");
                    }
                    else if (attempts >= maxAttemptsReadSetup)
                    {
                        job.Error(msg);
                        throw;
                    }
                    job.Message(msg);
                    isTerminated = false;
                    attempts    += 1;
                    System.Threading.Thread.Sleep(timeout);
                    timeout *= 2;
                    continue;
                }
                job.Message("sinter read setup finished");

                // One of these commands hangs infrequently..
                // However this job will not be retired as above, just abandoned and left in setup.
                Debug.WriteLine("set simulation layout", "SinterConsumer.DoConfigure");
                try
                {
                    sim.Vis = visible; // operates on Aspen instance
                }
                catch (Exception ex)
                {
                    sim.closeSim();
                    var msg = String.Format("Failed to Layout Simulation ({0}: {1}", attempts, ex.Message);
                    Debug.WriteLine(msg, "SinterConsumer.DoConfigure");
                    Debug.WriteLine(ex.StackTrace, "SinterConsumer.DoConfigure");
                    if (attempts >= maxAttemptsReadSetup)
                    {
                        job.Error(msg);
                        throw;
                    }
                    job.Message(msg);
                    attempts += 1;
                    System.Threading.Thread.Sleep(timeout);
                    timeout *= 2;
                    continue;
                }

                Debug.WriteLine("set simulation reset", "SinterConsumer.DoConfigure");
                try
                {
                    sim.dialogSuppress = true;  // operates on Aspen instance
                    //sim.resetSim(); // operates on Aspen instance
                }
                catch (Exception ex)
                {
                    sim.closeSim();
                    var msg = String.Format("Failed to Reset Simulation ({0}: {1}", attempts, ex.Message);
                    Debug.WriteLine(msg, "SinterConsumer.DoConfigure");
                    Debug.WriteLine(ex.StackTrace, "SinterConsumer.DoConfigure");
                    if (attempts >= maxAttemptsReadSetup)
                    {
                        job.Error(msg);
                        throw;
                    }
                    job.Message(msg);
                    attempts += 1;
                    System.Threading.Thread.Sleep(timeout);
                    timeout *= 2;
                    continue;
                }
                break;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// DoFinalize sends outputs to the database, sets final run status and
        /// warnings and/or errors in the job and process.
        /// </summary>
        /// <param name="stest"></param>
        /// <param name="job"></param>
        /// <param name="process"></param>
        protected virtual void DoFinalize(sinter.ISimulation stest,
                                          IJobConsumerContract job,
                                          IProcess process)
        {
            int runStatus = (int)stest.runStatus;

            process.SetStatus(runStatus);
            IDictionary <string, Object> myDict = null;
            JObject outputDict;

            try
            {
                if (stest.runStatus == sinter.sinter_AppError.si_OKAY ||
                    stest.runStatus == sinter.sinter_AppError.si_SIMULATION_WARNING)
                {
                    var superDict = stest.getOutputs();
                    outputDict = (JObject)superDict["outputs"];
                    // HACK: Inefficient Just making it work w/o covariance issues
                    string data = outputDict.ToString(Newtonsoft.Json.Formatting.None);
                    myDict = JsonConvert.DeserializeObject <IDictionary <string, Object> >(data);
                }
                else
                {
                    Debug.WriteLine(String.Format("Sinter runstatus={0}, did not retrieve outputs.", stest.runStatus),
                                    "SinterConsumer.DoFinalize");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message, "SinterConsumer.DoFinalize");
                process.Output = new Dictionary <string, Object>()
                {
                    { "exception", ex.Message }
                };
                if (job.IsTerminated())
                {
                    throw new TerminateException("Job was terminated by producer");
                }
                job.Error(String.Format("Failed to retrieve the outputs: {0}   {1}", ex.Message, ex.StackTrace));
                return;
            }
            Debug.WriteLine("Outputs: " + myDict, this.GetType());
            process.Output = myDict;

            if (stest.runStatus == sinter.sinter_AppError.si_OKAY)
            {
                job.Success();
            }
            else if (stest.runStatus == sinter.sinter_AppError.si_SIMULATION_ERROR)
            {
                var msg = String.Join(",", stest.errorsBasic());
                Debug.WriteLine("Error: si_SIMULATION_ERROR", "SinterConsumerRun.DoFinalize");
                Debug.WriteLine(msg, "SinterConsumerRun.DoFinalize");

                // Truncate the msg if it's more than 4000 characters.
                // The last 4000 characters are more interesting for debugging.
                if (msg.Length > 4000)
                {
                    msg = msg.Substring(msg.Length - 4000);
                }

                job.Error(String.Format("Error: si_SIMULATION_ERROR: {0}", msg));
                if (sim != null)
                {
                    Debug.WriteLine("2) Simulation is not Null",
                                    "SinterConsumerRun.DoFinalize()");
                    sim.closeSim();
                    if (sim != null)
                    {
                        KillProcessAndChildren(sim.ProcessID);
                    }
                    sim = null;
                    //sim.terminate();
                }
            }
            else if (stest.runStatus == sinter.sinter_AppError.si_SIMULATION_WARNING)
            {
                job.Message(String.Join(",", stest.warningsBasic()));
                job.Success();
            }
            else if (stest.runStatus == sinter.sinter_AppError.si_COM_EXCEPTION)
            {
                Debug.WriteLine("Error: si_COM_EXCEPTION", "SinterConsumerRun.DoFinalize");
                job.Error("COM Exception, Server was Terminated");
            }
            else if (stest.runStatus == sinter.sinter_AppError.si_SIMULATION_NOT_RUN)
            {
                Debug.WriteLine("Error: si_SIMULATION_NOT_RUN", "SinterConsumerRun.DoFinalize");
                job.Error("Simulation was not Run");
            }
            else
            {
                Debug.WriteLine("Error: Unknown", "SinterConsumerRun.DoFinalize");
                job.Error(string.Format("Aspen Process Status unknown code {0}.", (int)stest.runStatus));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// DoRun sends inputs specified in job contract to aspen simulation and runs
        /// the simulation.
        /// </summary>
        /// <param name="job"></param>
        /// <param name="stest"></param>
        /// <param name="combinedDict"></param>
        protected void DoRun(IJobConsumerContract job, sinter.ISimulation stest, JObject defaultsDict, JObject inputDict)
        {
            Debug.WriteLine(String.Format("Send Inputs to Job {0}", job.Id),
                            "SinterConsumer.DoRun");

            try
            {
                Debug.WriteLine("Default: " + JsonConvert.SerializeObject(defaultsDict), "SinterConsumerRun.DoRun");
                Debug.WriteLine("Input: " + JsonConvert.SerializeObject(inputDict), "SinterConsumerRun.DoRun");

                if (defaultsDict != null)
                {
                    stest.sendDefaults(defaultsDict);
                }
                stest.sendInputs(inputDict);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(String.Format(
                                    "Error sending Inputs: {0}, traceback {1}",
                                    ex.Message, ex.StackTrace), "SinterConsumerRun.DoRun");
                if (job.IsTerminated())
                {
                    throw new TerminateException("Job was terminated by producer");
                }
                job.Error(String.Format(
                              "Error sending Inputs: {0}, traceback {1}",
                              ex.Message, ex.StackTrace));
                Debug.WriteLine(String.Format("Moved Job {0} to Error", job.Id),
                                "SinterConsumer.DoRun");
                throw;
            }
            job.Message("sinter inputs sent, running simulation");
            Debug.WriteLine(String.Format("Job {0}: sendInputsToSim ", job.Id),
                            "SinterConsumer.DoRun");
            try
            {
                running = true;
                lock (this)
                {
                    stest.sendInputsToSim();
                    Debug.WriteLine(String.Format("Job {0}: runSim ", job.Id),
                                    "SinterConsumer.DoRun");
                    stest.runSim();
                    Debug.WriteLine(String.Format("Job {0}: recvOutputsFromSim ", job.Id),
                                    "SinterConsumer.DoRun");
                    if (stest.runStatus == sinter.sinter_AppError.si_OKAY)
                    {
                        stest.recvOutputsFromSim();
                        job.Message("Real Run was complete and successful.");
                        Debug.WriteLine(String.Format("Real Run was complete and successful. Job Id: {0}", job.Id),
                                        "SinterConsumer.Run");
                    }
                    else if (stest.runStatus == sinter.sinter_AppError.si_SIMULATION_WARNING)
                    {
                        stest.recvOutputsFromSim();
                        job.Message("Real Run was completed with warning.");
                        Debug.WriteLine(String.Format("Real Run was completed with warning. Job Id: {0}", job.Id),
                                        "SinterConsumer.Run");
                    }
                    else
                    {
                        job.Message(String.Format("Real Run failed, runStatus={0}", stest.runStatus));
                        Debug.WriteLine(String.Format("Real Run was complete and failed. Job Id: {0}, runStatus={1}",
                                                      job.Id, stest.runStatus), "SinterConsumer.Run");
                    }
                }
                running = false;
                if (isTerminated)
                {
                    throw new TerminateException(
                              "Monitor thread Terminated Simulation");
                }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                Debug.WriteLine(String.Format("Moving Job {0} to Error", job.Id),
                                "SinterConsumerRun.DoRun");
                Debug.WriteLine(ex.Message, "SinterConsumerRun.DoRun");
                Debug.WriteLine(ex.StackTrace, "SinterConsumerRun.DoRun");

                if (this.terminateSimAndJob() == false)
                {
                    Debug.WriteLine("Failed to terminate the simulation",
                                    "SinterConsumerRun.DoRun()");
                    if (sim != null)
                    {
                        Debug.WriteLine("1) Simulation is not Null",
                                        "SinterConsumerRun.DoRun()");
                        sim.closeSim();
                        if (sim != null)
                        {
                            KillProcessAndChildren(sim.ProcessID);
                        }
                        sim = null;
                        //sim.terminate();
                    }
                }

                job.Error(String.Format(
                              "Error running simulation: {0}, traceback {1}",
                              ex.Message, ex.StackTrace));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(String.Format("Moving Job {0} to Error", job.Id),
                                "SinterConsumerRun.DoRun");
                Debug.WriteLine(ex.Message, "SinterConsumerRun.DoRun");
                Debug.WriteLine(ex.StackTrace, "SinterConsumerRun.DoRun");
                if (job.IsTerminated())
                {
                    throw new TerminateException("Job was terminated by producer");
                }
                job.Error(String.Format(
                              "Error running simulation: {0}, traceback {1}",
                              ex.Message, ex.StackTrace));
                throw;
            }
        }