Exemple #1
0
        public Stream GetJob(string id)
        {
            bool verbose = JobQueryParameters.GetVerbose(false);

            Debug.WriteLine("GET job: " + id, this.GetType().Name);

            int myid = Convert.ToInt32(id);
            Dictionary <string, Object> d;

            try
            {
                d = DataMarshal.GetJobDict(myid, verbose);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(String.Format("EXCEPTION: {0}   {1}", ex, ex.Message), this.GetType().Name);
                Debug.WriteLine("Stack Trace: " + ex.StackTrace, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "No Job.Id=" + id
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.NotFound);
            }
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(d);

            return(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)));
        }
Exemple #2
0
        public Stream GetJob(string id)
        {
            Debug.WriteLine("get simulations: " + id, this.GetType().Name);
            int myid = Convert.ToInt32(id);
            Dictionary <string, Object> d = DataMarshal.GetJobDict(myid, true);
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(d);

            return(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)));
        }
Exemple #3
0
        public Stream KillJob(string id)
        {
            Debug.WriteLine("kill job " + id, this.GetType().Name);
            int myid = Convert.ToInt32(id);
            IJobProducerContract contract = new ProducerJobContract(myid);

            try
            {
                contract.Kill();
            }
            catch (Exception ex)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.JOB_STATE_ERROR,
                    "Failed to Kill job " + id + ", " + ex.Message
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }
            var d    = DataMarshal.GetJobDict(contract.Id, true);
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(d);

            return(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)));
        }
Exemple #4
0
        public Stream SubmitJob(string id)
        {
            Debug.WriteLine("submit job " + id, this.GetType().Name);
            int myid = Convert.ToInt32(id);
            IJobProducerContract contract = new AspenJobProducerContract(myid);

            try
            {
                contract.Submit();
            }
            catch (Exception ex)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Failed to Submit job " + id + ", traceback " + ex.StackTrace.ToString()
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }
            var d    = DataMarshal.GetJobDict(contract.Id, true);
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(d);

            return(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)));
        }
Exemple #5
0
        public Stream CreateJob(Stream data)
        {
            Debug.WriteLine("create new job", this.GetType().Name);
            StreamReader sr   = new StreamReader(data);
            string       json = sr.ReadToEnd();

            Debug.WriteLine("JSON: " + json, this.GetType().Name);
            Dictionary <string, Object> inputData = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(json);
            Object name = "";
            Guid   sessionID;

            if (!inputData.TryGetValue("Session", out name))
            {
                Debug.WriteLine("Missing Session", this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key Session is required"
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.BadRequest);
            }
            try
            {
                sessionID = Guid.Parse((string)name);
            }
            catch (Exception)
            {
                Debug.WriteLine("Bad Session", this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Bad Session ID, must be GUID"
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.BadRequest);
            }

            if (!inputData.TryGetValue("Simulation", out name))
            {
                Debug.WriteLine("Missing simulation name", this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key Simulation is required"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }


            Object tmp = false;

            if (!inputData.TryGetValue("initialize", out tmp))
            {
                tmp = false;
            }
            bool initialize;

            try
            {
                initialize = (bool)tmp;
            }
            catch (InvalidCastException)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key initialize is required to be a boolean"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }

            if (!inputData.TryGetValue("reset", out tmp))
            {
                tmp = true;
            }
            bool reset;

            try
            {
                reset = (bool)tmp;
            }
            catch (InvalidCastException)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key reset is required to be a boolean"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }

            Debug.WriteLine("Retrieve Simulation Contract: " + name, this.GetType().Name);
            ISimulationProducerContract contract = null;

            try
            {
                contract = AspenSimulationContract.Get((string)name);
            }
            catch (Exception ex)
            {
                string msg = String.Format("Failed to get Simulation {0}: {1}",
                                           name, ex.StackTrace.ToString());
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }
            Object inputs;

            if (!inputData.TryGetValue("Inputs", out inputs))
            {
                Debug.WriteLine("Missing input data", this.GetType().Name);
                throw new ArgumentException("No Input data name");
            }
            Debug.WriteLine("Found Inputs", this.GetType().Name);
            Newtonsoft.Json.Linq.JObject inputsD = null;
            try
            {
                inputsD = (Newtonsoft.Json.Linq.JObject)inputs;
            }
            catch (InvalidCastException)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key Inputs is required to be a dictionary"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }
            Debug.WriteLine("Cast Inputs", this.GetType().Name);

            Dictionary <String, Object> dd = new Dictionary <string, object>();

            foreach (var v in inputsD)
            {
                Debug.WriteLine("VALUE: " + v);
                dd[v.Key] = v.Value;
            }
            var jobContract = contract.NewJob(sessionID, initialize, reset);

            jobContract.Process.Input = dd;

            var d = DataMarshal.GetJobDict(jobContract.Id, true);

            json = Newtonsoft.Json.JsonConvert.SerializeObject(d);
            return(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)));
        }