/* Pause:  Concurrency errors ignore, but must handle.  Pause in descending order by Id,
         *   to avoid competition with job consumers.
         *
         */
        public int Pause(Guid session)
        {
            int num = 0;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Session obj = container.Sessions.First <Session>(s => s.Id == session);
                foreach (var job in obj.Jobs.Where(s => s.State == "submit").OrderByDescending(t => t.Id))
                {
                    IJobProducerContract contract = new AspenJobProducerContract(job.Id);
                    try
                    {
                        job.State = "pause";
                        container.SaveChanges();
                        num += 1;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(String.Format("Failed to pause session {0} job {1} ", session, job.Id),
                                        this.GetType());
                    }
                }
            }
            return(num);
        }
        /* Cancel:
         *
         * Ignore concurrency exception
         *
         */
        public int Cancel(Guid session)
        {
            int num = 0;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Session obj = container.Sessions.First <Session>(s => s.Id == session);
                foreach (var job in obj.Jobs.Where(s => s.State == "create" || s.State == "submit"))
                {
                    IJobProducerContract contract = new AspenJobProducerContract(job.Id);
                    try
                    {
                        contract.Cancel();
                        num += 1;
                    }
                    catch (AuthorizationError ex)
                    {
                        Debug.WriteLine(String.Format("AuthorizationError({0}): Failed to cancel session {1} job {2} ", ex.Message, session, job.Id),
                                        this.GetType());
                        throw;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(String.Format("Failed to cancel session {0} job {1} ", session, job.Id),
                                        this.GetType());
                    }
                }
            }
            return(num);
        }
        public int Terminate(Guid session)
        {
            int num = 0;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Session obj = container.Sessions.First <Session>(s => s.Id == session);
                foreach (var job in obj.Jobs.Where(s => s.State == "setup" || s.State == "running"))
                {
                    IJobProducerContract contract = new AspenJobProducerContract(job.Id);
                    try
                    {
                        contract.Terminate();
                        num += 1;
                    }
                    catch (InvalidStateChangeException ex)
                    {
                        Debug.WriteLine(String.Format("InvalidStateChange: Failed to terminate session {0} job {1} ", session, job.Id),
                                        this.GetType());
                        Debug.WriteLine(ex.Message);
                    }
                    catch (AuthorizationError ex)
                    {
                        Debug.WriteLine(String.Format("AuthorizationError({0}): Failed to terminate session {1} job {2} ", ex.Message, session, job.Id),
                                        this.GetType());
                        throw;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(String.Format("Failed to terminate session {0} job {1} ", session, job.Id),
                                        this.GetType());
                        Debug.WriteLine(ex.Message);
                    }
                }
            }
            return(num);
        }