public IJobQueue Register(IConsumerRun run)
        {
            this.run = run;
            IConsumerContext ctx = Turbine.Consumer.AppUtility.GetConsumerContext();
            //Guid consumerId = ctx.Id;
            //Guid consumerId = Guid.NewGuid();
            String hostname = ctx.Hostname;
            String appName  = run.SupportedApplications.ElementAtOrDefault(0);

            Debug.WriteLine(String.Format("Register({0}) as {1}, {2}", appName, run.ConsumerId, hostname), this.GetType().Name);
            using (ProducerContext db = new ProducerContext())
            {
                // TODO: Registering as a single application is dubious.
                // IF support multiple apps in single consumer need to expose that via the database ( update SCHEMA!! )
                var app      = db.Applications.Single(a => a.Name == appName);
                var consumer = new Turbine.Data.Entities.JobConsumer
                {
                    Application = app,
                    Id          = run.ConsumerId,
                    hostname    = hostname,
                    processId   = System.Diagnostics.Process.GetCurrentProcess().Id,
                    keepalive   = DateTime.UtcNow,
                    status      = "up"
                };
                db.Consumers.Add(consumer);
                db.SaveChanges();
            }
            queue = AppUtility.GetJobQueue(run);
            //((DBJobQueue)queue).consumerId = consumerId;
            return(queue);
        }
예제 #2
0
        static public bool Check(string username, string password)
        {
            bool     is_authorized = false;
            DateTime now           = DateTime.UtcNow;
            string   key           = String.Format("{0}:{1}", username, password);

            if (userCache.ContainsKey(key) && userCache[key] > now)
            {
                Debug.WriteLine("user cache accessed: " + username, "AuthenticateCredentials");
                return(true);
            }
            using (var db = new ProducerContext())
            {
                // Assuming User.Name is unique
                // Unfortunately cannot specify unique in EF
                //User user = container.Users.FirstOrDefault<User>(
                //    s => s.Name == username & s.Token == password);
                //User user = container.Users.SingleOrDefault<User>(s => s.Name == username);
                User user = db.Users.SingleOrDefault <User>(s => s.Name == username);
                if (user != null)
                {
                    Debug.WriteLine("check token: " + username, "AuthenticateCredentials");
                    is_authorized = Turbine.Security.AuthenticateCredentials.CheckToken(password, user.Token);
                    if (is_authorized)
                    {
                        userCache[key] = now.AddSeconds(cacheRefreshSec);
                    }
                }
                return(is_authorized);
            }
        }
예제 #3
0
        internal static ApplicationList GetApplicationList()
        {
            var appList = new ApplicationList();

            Debug.WriteLine("Application", "TurbineLite");
            using (var db = new ProducerContext())
            {
                Debug.WriteLine("Application ProducerContext", "TurbineLite");
                foreach (var entity in db.Applications)
                {
                    Debug.WriteLine("Application Add", "TurbineLite");
                    Application app = new Application {
                        Name = entity.Name, Inputs = new InputTypeList()
                    };
                    appList.Add(app);
                    foreach (var i in app.Inputs)
                    {
                        app.Inputs.Add(new InputType()
                        {
                            Name = i.Name, Required = i.Required, Type = i.Type
                        });
                    }
                }
            }
            return(appList);
        }
예제 #4
0
        internal static string[] GetStagedInputs(string nameOrID, bool isGuid)
        {
            List <String> stagedInputList = new List <String>();

            using (var db = new ProducerContext())
            {
                Turbine.Data.Entities.Simulation entity = null;

                if (isGuid == true)
                {
                    Guid simulationid = new Guid(nameOrID);
                    entity = db.Simulations.OrderByDescending(q => q.Count).Single(a => a.Id == simulationid);
                }
                else
                {
                    entity = db.Simulations.OrderByDescending(q => q.Count).First(a => a.Name == nameOrID);
                }

                foreach (var si in entity.SimulationStagedInputs)
                {
                    stagedInputList.Add(si.Name);
                }
            }
            return(stagedInputList.ToArray <String>());;
        }
        /// <summary>
        /// Delete moves all jobs to cancel or terminate, then safely deletes them.
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public int Delete(Guid session)
        {
            int num   = 0;
            int count = 0;

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

            using (var db = new ProducerContext())
            {
                Session obj = db.Sessions.Single <Session>(s => s.Id == session);
                foreach (Job j in obj.Jobs)
                {
                    db.Processes.Remove(j.Process);
                    db.Messages.RemoveRange(j.Messages);
                    db.StagedInputFiles.RemoveRange(j.StagedInputFiles);
                    db.StagedOutputFiles.RemoveRange(j.StagedOutputFiles);
                    db.GeneratorJobs.RemoveRange(db.GeneratorJobs.Where(g => g.JobId == j.Id));
                }
                db.Generators.RemoveRange(db.Generators.Where(g => g.SessionId == obj.Id));
                db.Jobs.RemoveRange(obj.Jobs);
                db.Sessions.Remove(obj);
                db.SaveChanges();
            }
            return(count);
        }
예제 #6
0
        internal static int StopConsumer(Guid consumerID)
        {
            // 0 Failed to stop the consumer
            // -1 Consumer is already down
            // 1 Succeeded to stop the consumer

            Debug.WriteLine("Stopping consumer", "DataMarshall.StopConsumer");

            int stopped = 0;

            using (var db = new ProducerContext())
            {
                var entity = db.Consumers.Single(c => c.Id == consumerID);
                if (entity != null)
                {
                    if (entity.status != "down")
                    {
                        Debug.WriteLine("Changing status to down", "DataMarshall.StopConsumer");
                        entity.status = "down";
                        stopped       = 1;
                    }
                    else
                    {
                        Debug.WriteLine("Consumer is already down", "DataMarshall.StopConsumer");
                        stopped = -1;
                    }
                }
                db.SaveChanges();
            }
            return(stopped);
        }
        /// <summary>
        /// Assume that state of jobs cannot be changed during this call.  Therefore
        /// if one of them has the behavior is undefined.
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public int Unpause(Guid session)
        {
            int num = 0;

            using (ProducerContext db = new ProducerContext())
            {
                Session obj = db.Sessions.Single <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());
                    }
                }
                db.SaveChanges();
            }
            return(num);
        }
        public Guid CreateGenerator(Guid sessionid)
        {
            Guid generator = Guid.NewGuid();

            using (var db = new ProducerContext())
            {
                Debug.WriteLine("Looking for session: " + sessionid.ToString(), "ProducerSessionContract.CreateGenerator");
                //var jobs = db.Jobs.Where<Job>(j => j.SessionId == sessionid);
                Session session = db.Sessions.Single <Session>(s => s.Id == sessionid);
                Debug.WriteLine("Result for session: " + sessionid.ToString(), "ProducerSessionContract.CreateGenerator");
                if (session != null)
                {
                    Debug.WriteLine("Found the session", "ProducerSessionContract.CreateGenerator");
                    db.Generators.Add(new Generator()
                    {
                        Id        = generator,
                        SessionId = sessionid,
                        Page      = 0,
                        Create    = DateTime.UtcNow
                    });
                }
                else
                {
                    Debug.WriteLine(String.Format("InputError: This Session Id doesn't exist"),
                                    this.GetType());
                    return(Guid.Empty);
                }
                db.SaveChanges();
            }
            return(generator);
        }
예제 #9
0
        public void TestProcess_Empty()
        {
            Guid jobId = Guid.NewGuid();

            using (var db = new ProducerContext())
            {
                var item       = db.Sessions.Single(s => s.Id == sessionId);
                var user       = db.Users.Single(u => u.Name == userName);
                var simulation = db.Simulations.Single(s => s.Name == simulationName);
                var job        = new Turbine.Data.Entities.Job
                {
                    Id         = jobId,
                    Session    = item,
                    Create     = DateTime.UtcNow,
                    State      = "create",
                    User       = user,
                    Simulation = simulation
                };
                db.Jobs.Add(job);
                db.SaveChanges();
            }
            using (var db = new ProducerContext())
            {
                var item = db.Jobs.Single(s => s.Id == jobId);
                item.Process = new Turbine.Data.Entities.Process
                {
                };
                db.SaveChanges();
            }
        }
예제 #10
0
        internal static bool DeleteGenerator(Guid generatorid)
        {
            bool deleted = false;

            using (var db = new ProducerContext())
            {
                db.GeneratorJobs.RemoveRange(db.GeneratorJobs.Where(g => g.GeneratorId == generatorid));
                db.Generators.RemoveRange(db.Generators.Where(g => g.Id == generatorid));

                Debug.WriteLine("Delete Generators: " + db.Generators.Any(g => g.Id == generatorid).ToString()
                                + ", Delete GeneratorJobs: " + db.GeneratorJobs.Any(g => g.GeneratorId == generatorid).ToString(),
                                "DataMarshall.DeleteGenerator");

                deleted = !(db.Generators.Any(g => g.Id == generatorid) ||
                            db.GeneratorJobs.Any(g => g.GeneratorId == generatorid));

                db.SaveChanges();
            }

            using (var db = new ProducerContext())
            {
                Debug.WriteLine("Delete Generators: " + db.Generators.Any(g => g.Id == generatorid).ToString()
                                + ", Delete GeneratorJobs: " + db.GeneratorJobs.Any(g => g.GeneratorId == generatorid).ToString(),
                                "DataMarshall.DeleteGenerator");

                deleted = !(db.Generators.Any(g => g.Id == generatorid) ||
                            db.GeneratorJobs.Any(g => g.GeneratorId == generatorid));
            }
            return(deleted);
        }
        public static ISimulationProducerContract Get(string nameOrID)
        {
            Guid simulationId = Guid.Empty;
            bool isGuid       = Guid.TryParse(nameOrID, out simulationId);

            ProducerSimulationContract sc = new ProducerSimulationContract();

            using (var db = new ProducerContext())
            {
                Simulation obj = null;
                if (isGuid == true)
                {
                    obj = db.Simulations.SingleOrDefault(s => s.Id == simulationId);
                }
                else
                {
                    obj = db.Simulations
                          .OrderByDescending(q => q.Count).First(s => s.Name == nameOrID);
                }

                if (obj == null)
                {
                    return(null);
                }

                sc.simId      = obj.Id;
                sc.name       = obj.Name;
                sc.nameIsGuid = isGuid;
            }

            return(sc);
        }
 public void KeepAlive()
 {
     //IConsumerContext ctx = AppUtility.GetConsumerContext();
     //Guid consumerId = ctx.Id;
     Debug.WriteLine(String.Format("KeepAlive Consumer {0}", run.ConsumerId), this.GetType().Name);
     using (ProducerContext db = new ProducerContext())
     {
         var consumer = db.Consumers.Single <JobConsumer>(s => s.Id == run.ConsumerId);
         if (consumer.status.Equals("up"))
         {
             consumer.keepalive = DateTime.UtcNow;
             try
             {
                 db.SaveChanges();
             }
             catch (System.Data.Entity.Validation.DbEntityValidationException e)
             {
                 foreach (var eve in e.EntityValidationErrors)
                 {
                     Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                       eve.Entry.Entity.GetType().Name, eve.Entry.State);
                     foreach (var ve in eve.ValidationErrors)
                     {
                         Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                           ve.PropertyName, ve.ErrorMessage);
                     }
                 }
                 throw;
             }
         }
     }
 }
예제 #13
0
 void SetOutput(string data)
 {
     using (ProducerContext db = new ProducerContext())
     {
         var entity = db.Processes.Single(s => s.Id == id);
         entity.Output = data;
         try
         {
             db.SaveChanges();
         }
         catch (System.Data.Entity.Validation.DbEntityValidationException e)
         {
             foreach (var eve in e.EntityValidationErrors)
             {
                 Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                   eve.Entry.Entity.GetType().Name, eve.Entry.State);
                 foreach (var ve in eve.ValidationErrors)
                 {
                     Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                       ve.PropertyName, ve.ErrorMessage);
                 }
             }
             throw;
         }
     }
 }
예제 #14
0
        public GetStorageTokenController()
        {
            context = new ProducerContext();

            ConnectionString = ConfigurationManager.ConnectionStrings[connString].ConnectionString;
            StorageAccount   = CloudStorageAccount.Parse(ConnectionString);
            BlobClient       = StorageAccount.CreateCloudBlobClient();
        }
예제 #15
0
 public void SetStatus(int status)
 {
     using (ProducerContext db = new ProducerContext())
     {
         var entity = db.Processes.Single(s => s.Id == id);
         entity.Status = status;
         db.SaveChanges();
     }
 }
예제 #16
0
 /*
  * public void AddStderr(string data)
  * {
  *  using (TurbineCompactDatabase container = new TurbineCompactDatabase())
  *  {
  *      SinterProcess entity = container.SinterProcesses.Single<SinterProcess>(s => s.Id == id);
  *      byte[] stdout = entity.Stderr;
  *      if (stdout == null)
  *          entity.Stderr = System.Text.Encoding.ASCII.GetBytes(data);
  *      else
  *      {
  *          List<byte> list = new List<byte>(entity.Stderr);
  *          list.AddRange(System.Text.Encoding.ASCII.GetBytes(data));
  *          entity.Stderr = list.ToArray<byte>();
  *      }
  *      container.SaveChanges();
  *  }
  * }
  */
 private void SetInput(string data)
 {
     using (ProducerContext db = new ProducerContext())
     {
         var entity = db.Processes.Single(s => s.Id == id);
         entity.Input = data;
         db.SaveChanges();
     }
 }
예제 #17
0
        public Turbine.Data.Contract.Behaviors.IProcess Setup()
        {
            string baseDir = AppUtility.GetAppContext().BaseWorkingDirectory;
            Guid   pid     = Guid.Empty;

            using (ProducerContext db = new ProducerContext())
            {
                var job      = db.Jobs.Single(j => j.Count == id);
                var consumer = job.Consumer;

                if (consumer == null)
                {
                    throw new IllegalAccessException("Setup failed, Consumer is not registered");
                }
                if (consumer.status != "up")
                {
                    throw new IllegalAccessException(String.Format(
                                                         "Setup failed, Consumer status {0} != up", consumer.status));
                }
                if (job.ConsumerId != ConsumerId)
                {
                    throw new IllegalAccessException(String.Format(
                                                         "job({0}) consumer access denied {1} != {2}", job.Id, job.ConsumerId, ConsumerId)
                                                     );
                }
                Debug.WriteLine(String.Format("Setup consumer GUID {0}", consumer.Id), this.GetType().Name);

                if (job.State != "locked")
                {
                    throw new InvalidStateChangeException("Violation of state machine");
                }

                if (job.Process == null)
                {
                    throw new InvalidStateChangeException("Process configuration missing");
                }

                processId = job.Process.Id;
                job.State = "setup";
                job.Setup = DateTime.UtcNow;
                job.Process.WorkingDir = System.IO.Path.Combine(baseDir, id.ToString());
                job.Consumer           = (JobConsumer)consumer;
                job.Messages.Add(new Turbine.Data.Entities.Message {
                    Id     = Guid.NewGuid(),
                    Create = DateTime.UtcNow,
                    Value  = String.Format("event=setup,consumer={0}", consumer.Id)
                });
                pid            = job.Process.Id;
                simulationName = job.Simulation.Name;
                db.SaveChanges();
            }
            state = States.SETUP;
            return(Turbine.DataEF6.Contract.ProcessContract.Get(pid));
        }
예제 #18
0
        public static Dictionary <string, object> GetJobDict(int id, bool verbose)
        {
            var dict = new Dictionary <string, Object>();

            using (var db = new ProducerContext())
            {
                var job = db.Jobs.Single <Turbine.Data.Entities.Job>(j => j.Count == id);
                dict = GetJobRepresentation(job, verbose);
            }
            return(dict);
        }
 public static IProcess Get(Guid id)
 {
     using (ProducerContext db = new ProducerContext())
     {
         var entity = db.Processes.Single(p => p.Id == id);
         return(new ProcessContract()
         {
             id = id
         });
     }
 }
예제 #20
0
        public bool Initialize()
        {
            bool val = false;

            using (ProducerContext db = new ProducerContext())
            {
                Job obj = db.Jobs.Single <Job>(s => s.Count == id);
                val = obj.Initialize;
            }
            return(val);
        }
예제 #21
0
 static bool AddUser(string username, string token)
 {
     using (ProducerContext container = new ProducerContext())
     {
         Turbine.Data.Entities.User entity = new Turbine.Data.Entities.User();
         entity.Name  = username;
         entity.Token = token;
         container.Users.Add(entity);
         container.SaveChanges();
     }
     return(true);
 }
예제 #22
0
        public void TestCreateSimulations()
        {
            CreateSimulationEmptyInputs("Test3");
            CreateSimulationEmptyInputs("Test4");

            using (var db = new ProducerContext())
            {
                var obj   = db.Simulations.Single(i => i.Name == "Test3");
                var input = obj.SimulationStagedInputs.SingleOrDefault(i => i.Name == "configuration");
                Assert.IsNotNull(input);
            }
        }
예제 #23
0
        public static void ClassInit(TestContext context)
        {
            BaseDatabaseTest.CleanUpDatabase();

            using (var db = new ProducerContext())
            {
                var app = new Turbine.Data.Entities.Application {
                    Name = "ACM", Version = "7.3"
                };
                db.Applications.Add(app);
                var user = new Turbine.Data.Entities.User {
                    Name = username
                };
                db.Users.Add(user);
                db.SaveChanges();
            }
            using (var db = new ProducerContext())
            {
                var item = db.Applications.Single(s => s.Name == "ACM");
                item.InputFileTypes.Add(new Turbine.Data.Entities.InputFileType
                {
                    Id       = Guid.NewGuid(),
                    Name     = "configuration",
                    Required = true,
                    Type     = "plain/text"
                });
                item.InputFileTypes.Add(new Turbine.Data.Entities.InputFileType
                {
                    Id       = Guid.NewGuid(),
                    Name     = "aspenfile",
                    Required = true,
                    Type     = "plain/text"
                });
                db.SaveChanges();
            }
            using (var db = new ProducerContext())
            {
                var app  = db.Applications.Single(s => s.Name == "ACM");
                var user = db.Users.Single(u => u.Name == username);
                var sim  = new Turbine.Data.Entities.Simulation
                {
                    Id          = Guid.NewGuid(),
                    Application = app,
                    Create      = DateTime.UtcNow,
                    Name        = "Test",
                    Update      = DateTime.UtcNow,
                    User        = user
                };
                db.Simulations.Add(sim);
                db.SaveChanges();
            }
        }
 public static string[] ListOfNames()
 {
     using (var db = new ProducerContext())
     {
         Debug.WriteLine("ListOfNames", "ProducerSimulationContract");
         List <string> l = new List <string>();
         foreach (Simulation sim in db.Simulations)
         {
             l.Add(sim.Name);
         }
         return(l.ToArray <string>());
     }
 }
예제 #25
0
 public void TestSession_No_Create()
 {
     // Session
     using (var db = new ProducerContext())
     {
         var item = new Turbine.Data.Entities.Session
         {
             Id          = Guid.NewGuid(),
             Description = "testing simulation"
         };
         db.Sessions.Add(item);
         db.SaveChanges();
     }
 }
예제 #26
0
 public bool IsTerminated()
 {
     using (ProducerContext db = new ProducerContext())
     {
         Job obj = db.Jobs.Single <Job>(s => s.Count == id);
         if (obj.ConsumerId != ConsumerId)
         {
             throw new IllegalAccessException(String.Format(
                                                  "job({0}) consumer access denied {1} != {2}", obj.Id, obj.ConsumerId, ConsumerId)
                                              );
         }
         return("terminate".Equals(obj.State));
     }
 }
예제 #27
0
        public void Message(string msg)
        {
            Message w = new Message()
            {
                Id = Guid.NewGuid(), Value = msg, Create = DateTime.UtcNow
            };

            using (ProducerContext db = new ProducerContext())
            {
                Job obj = db.Jobs.Single <Job>(s => s.Count == id);
                obj.Messages.Add(w);
                db.SaveChanges();
            }
        }
예제 #28
0
        internal static List <Guid> GetSessions(int page, int rpp)
        {
            var l = new List <Guid>();

            Debug.WriteLine("GetSessions", "Turbine.Lite.Web.Resources.Contracts.DataMarshall");
            using (var db = new ProducerContext())
            {
                foreach (var entity in db.Sessions.OrderBy(s => s.Create).Take(rpp).Skip((page - 1) * rpp))
                {
                    l.Add(entity.Id);
                }
            }
            return(l);
        }
예제 #29
0
 static bool UpdateUser(string username, string token)
 {
     using (ProducerContext container = new ProducerContext())
     {
         Turbine.Data.Entities.User user = container.Users.SingleOrDefault <Turbine.Data.Entities.User>(s => s.Name == username);
         if (user == null)
         {
             return(false);
         }
         user.Token = token;
         container.SaveChanges();
     }
     return(true);
 }
 public void Error()
 {
     //IConsumerContext ctx = AppUtility.GetConsumerContext();
     //Guid consumerId = ctx.Id;
     Debug.WriteLine(String.Format("Error as {0}, {1}", run.ConsumerId), this.GetType().Name);
     using (ProducerContext db = new ProducerContext())
     {
         var consumer = db.Consumers.Single <JobConsumer>(s => s.Id == run.ConsumerId);
         if (!consumer.status.Equals("error"))
         {
             consumer.status = "error";
             db.SaveChanges();
         }
     }
 }