public override void Submit()
        {
            // NOTE: If SQS Failure occurs ignore.
            // Orchestrator will check submit queue

            var    listener = new Turbine.Orchestrator.AWS.Data.Contract.SimpleMessageListener();
            var    job      = new Turbine.Orchestrator.AWS.Data.Contract.Messages.SubmitJobMessage();
            string user     = Container.GetAppContext().UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Job obj = container.Jobs.Single <Job>(s => s.Id == this.Id);  //Pulls the job that matches id
                if (obj.User.Name != user)
                {
                    throw new ArgumentException(
                              String.Format("Authorization Denied For {0}, Owner {1}", user, obj.User.Name)
                              );
                }
                if (obj.State != "create")
                {
                    throw new InvalidStateChangeException("Violation of state machine");
                }

                // Simple check
                ValidateConfig(obj.Simulation);
                // TODO: DIJ Application Validators
                // container.GetApplication(obj.Simulation.Application).Validate(obj.Simulation)

                obj.State  = "submit";
                obj.Submit = DateTime.UtcNow;

                container.SaveChanges();

                job.Id             = obj.guid;
                job.Inc            = obj.Id;
                job.Initialize     = obj.Initialize;
                job.Reset          = obj.Reset;
                job.SimulationId   = obj.SimulationId;
                job.SimulationName = obj.Simulation.Name;
                var messages  = new Turbine.Orchestrator.AWS.Data.Contract.Messages.SubmitJobMessage[] { job };
                var jobIdList = listener.PutOnQueue(messages);

                if (jobIdList.Count != 1)
                {
                    obj.State    = "failure";
                    obj.Finished = DateTime.UtcNow;
                    string msg = String.Format("Failed to Add Job {0} to SQS Queue", obj.guid);
                    var    w   = new Message()
                    {
                        Id = Guid.NewGuid(), Value = msg
                    };
                    obj.Messages.Add(w);
                    container.SaveChanges();
                }
            }
        }
        /* Submit
         *
         * IJobProducerContract event requires user name check
         * Better solution to use DIJ for Producer.CheckUser
         * and State Checks, throw Application Specific
         * Exceptions.  Move to POCO Data Model first.
         */
        public void Submit()
        {
            string user = AppUtility.GetAppContext().UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Job obj = container.Jobs.Single <Job>(s => s.Id == id);  //Pulls the job that matches id
                if (obj.User.Name != user)
                {
                    throw new ArgumentException(
                              String.Format("Authorization Denied For {0}, Owner {1}", user, obj.User.Name)
                              );
                }
                if (obj.State != "create")
                {
                    throw new InvalidStateChangeException("Violation of state machine");
                }

                // Backup and Config must be valid to submit/run
                ValidateBackup(obj.Simulation);
                ValidateConfig(obj.Simulation);

                obj.State  = "submit";
                obj.Submit = DateTime.UtcNow;

                container.SaveChanges();
            }
        }
Пример #3
0
        /* Cancel:
         *
         * IJobProducerContract
         */
        public void Cancel()
        {
            string user = Container.GetAppContext().UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Job obj = container.Jobs.Single <Job>(s => s.Id == id);

                if (obj.User.Name != user)
                {
                    throw new AuthorizationError(
                              String.Format("Cancel Authorization Denied For {0}", user)
                              );
                }

                if (obj.State == "create" || obj.State == "submit" || obj.State == "setup" || obj.State == "pause")
                {
                    obj.State    = "cancel";
                    obj.Finished = DateTime.UtcNow;
                }
                else
                {
                    throw new InvalidStateChangeException(
                              String.Format("Cannot cancel a job {0} in state {1}", id, obj.State)
                              );
                }
                container.SaveChanges();
            }
        }
Пример #4
0
        /*  Running: Moves job from running
         *    Only can be performed by declared consumer
         *
         */
        public void Running()
        {
            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Job obj = container.Jobs.Single <Job>(s => s.Id == id);
                AuthorizeConsumer(obj);

                if (obj.State != "setup")
                {
                    throw new InvalidStateChangeException("Violation of state machine");
                }
                if (obj.Process == null)
                {
                    throw new InvalidStateChangeException("Process configuration missing");
                }
                if (obj.Process.WorkingDir == null)
                {
                    throw new InvalidStateChangeException("Process configuration missing working directory");
                }
                if (obj.Process.Input == null)
                {
                    throw new InvalidStateChangeException("Process configuration missing Input BLOB");
                }

                obj.State   = "running";
                obj.Running = DateTime.UtcNow;
                container.SaveChanges();
            }
        }
Пример #5
0
        public void Warning(string msg)
        {
            if (msg.Length > MAX_MESSAGE_LENGTH)
            {
                msg = msg.Substring(0, MAX_MESSAGE_LENGTH);
            }

            var w = new Message()
            {
                Id = Guid.NewGuid(), Value = msg
            };

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Job obj = container.Jobs.Single <Job>(s => s.Id == id);
                AuthorizeConsumer(obj);

                if (obj.State != "running")
                {
                    throw new InvalidStateChangeException("Violation of state machine");
                }
                obj.State = "warning";
                obj.Messages.Add(w);
                obj.Finished = DateTime.UtcNow;
                container.SaveChanges();
            }
        }
        public bool UpdateInputFileType(string name, bool required, string type)
        {
            string owner = Container.GetAppContext().UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                var user = container.Users.Single <User>(s => s.Name == owner);
                var obj  = container.Applications.
                           Single <Application>(s => s.Name == applicationName);

                if (user != obj.User)
                {
                    throw new AuthorizationError(String.Format("Only owner {0} of application {1} can update it", owner, name));
                }

                var fileType = container.InputFileTypes.SingleOrDefault <InputFileType>(s => s.ApplicationName == applicationName & s.Name == name);
                if (fileType == null)
                {
                    fileType = new InputFileType()
                    {
                        Name = name, Required = required, Type = type, Id = Guid.NewGuid(), ApplicationName = applicationName
                    }
                }
                ;
                fileType.Required = required;
                fileType.Type     = type;
                obj.InputFileTypes.Add(fileType);
                container.SaveChanges();
            }
            return(true);
        }
    }
        /*  Unpause:
         *     Assume that state of jobs cannot be changed during this call.  Therefore
         *     if one of them has the behavior is undefined.
         */
        public int Unpause(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 == "pause"))
                {
                    try
                    {
                        job.State  = "submit";
                        job.Submit = DateTime.UtcNow;
                        num       += 1;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(String.Format("Failed to unpause session {0} job {1} ", session, job.Id),
                                        this.GetType());
                    }
                }
                container.SaveChanges();
            }
            return(num);
        }
Пример #8
0
        static void Main(string[] args)
        {
            System.Console.Out.Write("Username: "******"Password: "******"Updating Password");
                    user.Token = password;
                }
                else
                {
                    System.Console.Out.Write("Creating new user");
                    user = new User();
                    //user.Id = Guid.NewGuid();
                    user.Name  = username;
                    user.Token = password;
                    container.Users.AddObject(user);
                }
                container.SaveChanges();
            }
            System.Console.In.ReadLine();
        }
        public int Delete(Guid session)
        {
            int num   = 0;
            int count = 0;

            num += Cancel(session);
            num += Terminate(session);

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Session obj = container.Sessions.First <Session>(s => s.Id == session);
                count = obj.Jobs.Count <Job>();
                foreach (var job in obj.Jobs)
                {
                    SinterProcess p = job.Process;
                    container.DeleteObject(p);
                    job.Messages.ToList().ForEach(m => container.DeleteObject(m));
                }

                obj.Jobs.ToList().ForEach(j => container.DeleteObject(j));
                container.DeleteObject(obj);
                container.SaveChanges();
            }
            return(count);
        }
        /* 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);
        }
Пример #11
0
        /* Submit
         *
         * IJobProducerContract event requires user name check
         * Better solution to use DIJ for Producer.CheckUser
         * and State Checks, throw Application Specific
         * Exceptions.  Move to POCO Data Model first.
         */
        public virtual void Submit()
        {
            string user = Container.GetAppContext().UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Job obj = container.Jobs.Single <Job>(s => s.Id == id);  //Pulls the job that matches id
                if (obj.User.Name != user)
                {
                    throw new ArgumentException(
                              String.Format("Authorization Denied For {0}, Owner {1}", user, obj.User.Name)
                              );
                }
                if (obj.State != "create")
                {
                    throw new InvalidStateChangeException("Violation of state machine");
                }

                Debug.WriteLine("SimulationId: " + obj.SimulationId);
                // Simple check
                ValidateConfig(obj.Simulation);
                // TODO: DIJ Application Validators
                // container.GetApplication(obj.Simulation.Application).Validate(obj.Simulation)

                obj.State  = "submit";
                obj.Submit = DateTime.UtcNow;

                container.SaveChanges();
            }
        }
Пример #12
0
        public void TestSweep()
        {
            int count = JobSweeper.SweepSubmitQueue(5000, 5);

            Assert.AreEqual(count, 5);
            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                count = container.Jobs.Count(s => s.State == "expired");
            }

            Assert.AreEqual(count, 5);
            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                jobCount = container.Jobs.Count();
                foreach (Job obj in container.Jobs.OrderBy(s => s.Submit))
                {
                    Console.WriteLine(String.Format("job({0}) submit {1} state {2}", obj.Id, obj.Submit, obj.State));
                }
                container.SaveChanges();
            }

            count = JobSweeper.SweepSubmitQueue(5000, 5);
            Assert.AreEqual(count, 5);
            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                count = container.Jobs.Count(s => s.State == "expired");
            }
            Assert.AreEqual(count, 10);
        }
Пример #13
0
        // Moves old jobs to expired
        public static int SweepSetup(int expiredMS, int max = 0)
        {
            DateTime expired = DateTime.UtcNow.AddMilliseconds(-expiredMS);
            int      swept   = 0;
            Job      job     = null;
            Message  msg     = null;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                while (true)
                {
                    job = container.Jobs.OrderBy(t => t.Submit).FirstOrDefault(s => s.State == "setup" && s.Create <= expired);
                    if (job == null)
                    {
                        break;
                    }
                    job.State    = "expired";
                    job.Finished = DateTime.UtcNow;
                    msg          = new Message();
                    msg.Value    = "Job has expired in setup state after waiting for " + expiredMS + " milliseconds";
                    job.Messages.Add(msg);
                    Debug.WriteLine(String.Format("Job({0}) moved to expired state", job.Id));
                    swept += 1;
                    container.SaveChanges();
                    if (max > 0 && swept >= max)
                    {
                        break;
                    }
                }
            }
            return(swept);
        }
Пример #14
0
        public IJobQueue Register()
        {
            IConsumerContext ctx        = Turbine.Consumer.AppUtility.GetConsumerContext();
            Guid             consumerId = ctx.Id;
            String           hostname   = ctx.Hostname;

            Debug.WriteLine(String.Format("Register as {0}, {1}", consumerId, hostname), this.GetType().Name);

            //
            // NOTE: Amazon InstanceID and AMI removed
            //
            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                container.JobConsumers.AddObject(new JobConsumer()
                {
                    Id        = consumerId,
                    hostname  = hostname,
                    AMI       = "",
                    instance  = "",
                    processId = Process.GetCurrentProcess().Id.ToString(),
                    status    = "up",
                    keepalive = DateTime.UtcNow
                }
                                                 );
                container.SaveChanges();
            }
            queue = new DBJobQueue();
            return(queue);
        }
        /* Terminate: Administrator function, for killing jobs.
         * IJobProducerContract
         */
        public void Terminate()
        {
            string user = AppUtility.GetAppContext().UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Job obj = container.Jobs.Single <Job>(s => s.Id == id);

                if (obj.User.Name != user)
                {
                    throw new AuthorizationError(
                              String.Format("Termination Authorization Denied For {0}", user)
                              );
                }

                if (obj.State == "running" || obj.State == "setup")
                {
                    obj.State    = "terminate";
                    obj.Finished = DateTime.UtcNow;
                }
                else
                {
                    throw new InvalidStateChangeException(
                              String.Format("Cannot terminate a job {0} in state {1}", id, obj.State)
                              );
                }
                container.SaveChanges();
            }
        }
Пример #16
0
        public static IJobProducerContract Create(string simulationName, Guid sessionID, bool initialize, bool reset)
        {
            int id = -1;
            IProducerContext ctx      = Container.GetAppContext();
            String           userName = ctx.UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Simulation obj     = container.Simulations.OrderByDescending(q => q.Create).First <Simulation>(s => s.Name == simulationName);
                User       user    = container.Users.Single <User>(u => u.Name == userName);
                Session    session = container.Sessions.SingleOrDefault <Session>(i => i.Id == sessionID);
                if (session == null)
                {
                    session = new Session()
                    {
                        Id = sessionID, Create = DateTime.UtcNow, User = user
                    };
                    container.Sessions.AddObject(session);
                    container.SaveChanges();
                }
                System.Diagnostics.Debug.WriteLine(String.Format("simulation {0}, User {1}, Session {2}",
                                                                 simulationName, user.Name, session.Id), "AspenJobContract");
                Job job = new Job()
                {
                    guid       = Guid.NewGuid(),
                    Simulation = obj,
                    State      = "create",
                    Create     = DateTime.UtcNow,
                    Process    = new SinterProcess()
                    {
                        Id = Guid.NewGuid()
                    },
                    User       = user,
                    Session    = session,
                    Initialize = initialize,
                    Reset      = reset,
                };
                container.SaveChanges();
                id = job.Id;
            }
            return(new AspenJobProducerContract()
            {
                Id = id
            });
        }
 private void SetDefaults(byte[] data)
 {
     using (TurbineModelContainer container = new TurbineModelContainer())
     {
         Simulation entity = container.Simulations.Single <Simulation>(s => s.Name == name);
         entity.Defaults = data;
         container.SaveChanges();
     }
 }
Пример #18
0
 void SetInput(string data)
 {
     using (TurbineModelContainer container = new TurbineModelContainer())
     {
         SinterProcess entity = container.SinterProcesses.Single <SinterProcess>(s => s.Id == id);
         entity.Input = data;
         container.SaveChanges();
     }
 }
Пример #19
0
 public void SetStatus(int status)
 {
     using (TurbineModelContainer container = new TurbineModelContainer())
     {
         SinterProcess entity = container.SinterProcesses.Single <SinterProcess>(s => s.Id == id);
         entity.Status = status;
         container.SaveChanges();
     }
 }
Пример #20
0
 //Sets the dir in the function to MyDocs, but doesn't actually chdir
 void SetWorkingDirectory(string path)
 {
     using (TurbineModelContainer container = new TurbineModelContainer())
     {
         SinterProcess entity = container.SinterProcesses.Single <SinterProcess>(s => s.Id == id);
         entity.WorkingDir = path;
         container.SaveChanges();
     }
 }
Пример #21
0
        public static bool UpdateDescription(string sessionid, string description)
        {
            Guid id = Guid.Parse(sessionid);

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Turbine.Data.Session entity = container.Sessions.Single(s => s.Id == id);
                entity.Descrption = description;
                container.SaveChanges();
            }
            return(true);
        }
Пример #22
0
        /*  Message: Consumer changes job message without changing state.
         *
         * IJobConsumerContract
         *
         */
        public void Message(string msg)
        {
            Message w = new Message();

            w.Value = msg;
            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Job obj = container.Jobs.Single <Job>(s => s.Id == id);
                AuthorizeConsumer(obj);
                obj.Messages.Add(w);
                container.SaveChanges();
            }
        }
Пример #23
0
        public static ISimulationProducerContract Create(string simulationName, string applicationName)
        {
            string owner = Container.GetAppContext().UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                User       user = container.Users.Single <User>(s => s.Name == owner);
                Simulation obj  = container.Simulations.OrderByDescending(q => q.Create)
                                  .FirstOrDefault(s => s.Name == simulationName);
                if (obj != null)
                {
                    throw new InvalidStateChangeException(String.Format(
                                                              "Simulation with Name '{0}' already exists", simulationName));
                }

                Application app = container.Applications.SingleOrDefault <Application>(s => s.Name == applicationName);
                if (app == null)
                {
                    throw new ArgumentException(String.Format(
                                                    "Application '{0}' does not exist", applicationName));
                }

                var sim = new Simulation()
                {
                    Name        = simulationName,
                    Id          = Guid.NewGuid(),
                    User        = user,
                    Application = app,
                    Create      = DateTime.UtcNow,
                    Update      = DateTime.UtcNow
                };

                foreach (var input in app.InputFileTypes)
                {
                    if ("any".Equals(input.Name))
                    {
                        continue;
                    }
                    sim.SimulationStagedInputs.Add(new SimulationStagedInput()
                    {
                        Id = Guid.NewGuid(), Name = input.Name, InputFileType = input, Hash = null, Simulation = sim
                    });
                }
                container.Simulations.AddObject(sim);
                container.SaveChanges();
            }
            ProducerSimulationContract sc = new ProducerSimulationContract();

            sc.name = simulationName;
            return(sc);
        }
Пример #24
0
        public void Error()
        {
            IConsumerContext ctx        = AppUtility.GetConsumerContext();
            Guid             consumerId = ctx.Id;

            Debug.WriteLine(String.Format("Error as {0}, {1}", consumerId), this.GetType().Name);

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                var consumer = (JobConsumer)container.JobConsumers.Single <Turbine.Data.JobConsumer>(s => s.Id == consumerId);
                consumer.status = "error";
                container.SaveChanges();
            }
        }
Пример #25
0
        public static IJobProducerContract Create(string simulationName, Guid sessionID, bool initialize)
        {
            int      id       = -1;
            IContext ctx      = AppUtility.GetAppContext();
            String   userName = ctx.UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Simulation obj     = container.Simulations.Single <Simulation>(s => s.Name == simulationName);
                User       user    = container.Users.Single <User>(u => u.Name == userName);
                Session    session = container.Sessions.SingleOrDefault <Session>(i => i.guid == sessionID);
                if (session == null)
                {
                    session = new Session()
                    {
                        guid = sessionID, Create = DateTime.UtcNow, User = user
                    };
                    container.Sessions.AddObject(session);
                    container.SaveChanges();
                }

                Job job = new Job()
                {
                    Simulation = obj,
                    State      = "create",
                    Create     = DateTime.UtcNow,
                    Process    = new SinterProcess(),
                    User       = user,
                    Session    = session,
                    Initialize = initialize
                };
                container.SaveChanges();
                id = job.Id;
            }
            return(new AspenJobProducerContract(id));
        }
        public Guid Create()
        {
            Guid   session = Guid.NewGuid();
            string owner   = Container.GetAppContext().UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                User user = container.Users.Single <User>(s => s.Name == owner);
                container.Sessions.AddObject(new Session()
                {
                    Id = session, User = user, Create = DateTime.UtcNow
                });
                container.SaveChanges();
            }
            return(session);
        }
Пример #27
0
        public void Success()
        {
            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Job obj = container.Jobs.Single <Job>(s => s.Id == id);
                AuthorizeConsumer(obj);

                if (obj.State != "running")
                {
                    throw new InvalidStateChangeException("Violation of state machine");
                }
                obj.State    = "success";
                obj.Finished = DateTime.UtcNow;
                container.SaveChanges();
            }
        }
        public bool UpdateConfiguration(byte[] data)
        {
            string owner = AppUtility.GetAppContext().UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Simulation obj = container.Simulations.Single <Simulation>(s => s.Name == name);
                if (obj.User.Name != owner)
                {
                    throw new ArgumentException("Only owner {0} can update simulation", obj.User.Name);
                }
                obj.Configuration = data;
                container.SaveChanges();
            }
            return(true);
        }
Пример #29
0
        /* UnRegister
         *     If state is "up" move to "down", else leave alone.
         */
        public void UnRegister()
        {
            IConsumerContext ctx        = AppUtility.GetConsumerContext();
            Guid             consumerId = ctx.Id;

            Debug.WriteLine(String.Format("UnRegister Consumer {0}", consumerId), this.GetType().Name);

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                var consumer = (JobConsumer)container.JobConsumers.First <Turbine.Data.JobConsumer>(s => s.Id == consumerId);
                if (consumer.status == "up")
                {
                    consumer.status = "down";
                }
                container.SaveChanges();
            }
        }
Пример #30
0
        public void KeepAlive()
        {
            IConsumerContext ctx        = AppUtility.GetConsumerContext();
            Guid             consumerId = ctx.Id;

            Debug.WriteLine(String.Format("KeepAlive Consumer {0}", consumerId), this.GetType().Name);

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                var consumer = (JobConsumer)container.JobConsumers.Single <Turbine.Data.JobConsumer>(s => s.Id == consumerId);
                if (consumer.status == "up")
                {
                    consumer.keepalive = DateTime.UtcNow;
                }
                container.SaveChanges();
            }
        }