public void Increment(string id, string message)
 {
     using (var scope = CreateScope())
         using (var db = new MarvinEntities())
         {
             var entry = db.healthCounter.FirstOrDefault(hc => hc.id == id);
             var now   = _timeProvider.GetUtcNow();
             if (entry == null)
             {
                 entry = new healthCounter
                 {
                     id        = id,
                     message   = message,
                     count     = 1,
                     timestamp = now
                 };
                 db.healthCounter.Add(entry);
             }
             else
             {
                 entry.message = message;
                 if (entry.timestamp + MaxAge > now)
                 {
                     entry.count++;
                 }
                 else
                 {
                     entry.count = 1;
                 }
                 entry.timestamp = now;
             }
             db.SaveChanges();
             scope.Complete();
         }
 }
 static SqlJobRepository() //init logic
 {
     using (var scope = CreateScope())
         using (var db = new MarvinEntities())
         {
             // UPDATE enum tables if needed
             foreach (var enumType in new []
             {
                 typeof(StateFormat), typeof(ExecutionState), typeof(JobEssenceType), typeof(ExecutionEssenceType),
                 typeof(EssenceFileKind), typeof(AttachmentType), typeof(CommandType)
             })
             {
                 var tableName = enumType.Name;
                 tableName = tableName.First().ToString().ToLower() + tableName.Substring(1);
                 foreach (var id in Enum.GetValues(enumType))
                 {
                     var name = Enum.GetName(enumType, id);
                     db.Database.ExecuteSqlCommand($@"
                     SELECT * FROM {tableName} WHERE id=@id AND name=@name
                     IF @@ROWCOUNT = 0
                     BEGIN
                         UPDATE {tableName} SET name=@name WHERE id=@id
                         IF @@ROWCOUNT = 0
                         INSERT INTO {tableName} ([id] ,[name]) VALUES (@id, @name)
                     END
                     ",
                                                   new SqlParameter("@id", id),
                                                   new SqlParameter("@name", name));
                 }
             }
             db.SaveChanges();
             scope.Complete();
         }
 }
 public string GetEnvironment()
 {
     using (var db = new MarvinEntities())
     {
         var connection = db.Database.Connection.ConnectionString.Split(';').FirstOrDefault(a => a.ToLowerInvariant().StartsWith("data source"));
         return(connection != null?connection.Split('=').Last() : "Unknown");
     }
 }
 public Semaphore Probe(string semaphoreId)
 {
     using (CreateScope())
         using (var db = new MarvinEntities())
         {
             var semaphore = db.semaphore.SingleOrDefault(a => a.semaphoreId == semaphoreId);
             return(Mapper.Map <Semaphore>(semaphore));
         }
 }
 public IEnumerable <Job> WaitingJobs()
 {
     using (var _ = CreateScope())
         using (var db = new MarvinEntities())
         {
             var jobsWithoutPlan = db.job.Where(a => a.executionPlan == null).IncludeFullJob();
             var res             = jobsWithoutPlan.Select(Mapper.Map <Job>).ToList();
             return(res);
         }
 }
 public void Reset()
 {
     using (var scope = CreateScope())
         using (var db = new MarvinEntities())
         {
             db.semaphore.RemoveRange(db.semaphore);
             db.SaveChanges();
             scope.Complete();
         }
 }
Beispiel #7
0
        public IEnumerable<Command> GetAll()
        {
            using (var scope = CreateScope())
            using (var db = new MarvinEntities())
            {
                var commands = db.command;

                return commands.Select(Mapper.Map<Command>).ToList();
            }
        }
 public Job GetNewest()
 {
     using (var _ = CreateScope())
         using (var db = new MarvinEntities())
         {
             var job = db.job
                       .OrderByDescending(m => m.issued).Take(1).IncludeFullJob().FirstOrDefault();
             var res = Mapper.Map <Job>(job);
             return(res);
         }
 }
Beispiel #9
0
        public void Add(Command command)
        {
            using (var scope = CreateScope())
            using (var db = new MarvinEntities())
            {
                db.command.Add(Mapper.Map<command>(command));

                db.SaveChanges();
                scope.Complete();
            }
        }
 private static IEnumerable <Job> RecentlyJobs(Expression <Func <job, bool> > predicate, DateTime?after = null)
 {
     using (var _ = CreateScope())
         using (var db = new MarvinEntities())
         {
             var resDb = db.job
                         .Where(predicate)
                         .Where(a => (!after.HasValue || a.modified >= after)).IncludeFullJob();
             var res = resDb.Select(Mapper.Map <Job>).ToList();
             return(res);
         }
 }
 public IEnumerable <Job> ActiveJobs()
 {
     using (var _ = CreateScope())
         using (var db = new MarvinEntities())
         {
             var activeJobs =
                 db.job.Where(a =>
                              a.executionPlan.executionState == (int)ExecutionState.Queued ||
                              a.executionPlan.executionState == (int)ExecutionState.Running).IncludeFullJob();
             var res = activeJobs.Select(Mapper.Map <Job>).ToList();
             return(res);
         }
 }
Beispiel #12
0
        public void Remove(Command command)
        {
            using (var scope = CreateScope())
            using (var db = new MarvinEntities())
            {
                var com = Mapper.Map<command>(command);

                db.command.Attach(com);
                db.command.Remove(com);

                db.SaveChanges();
                scope.Complete();
            }
        }
Beispiel #13
0
 /// <summary>
 /// :warning: Only use for unit testing on local db.
 /// </summary>
 internal void Reset()
 {
     //Resetting commands - dangerzone! :fire:
     using (var scope = CreateScope())
     using (var db = new MarvinEntities())
     {
         var connectionString = db.Database.Connection.ConnectionString;
         if (!connectionString.Contains("MarvinLocal") || !connectionString.Contains("user id=nunit"))
             throw new Exception("Reset method is only allow for MarvinLocal and nunit user.");
         db.command.RemoveRange(db.command);
         db.SaveChanges();
         scope.Complete();
     }
 }
        private job InternalAdd(Job job)
        {
            job.LastModified = _timeProvider.GetUtcNow();

            using (var scope = CreateScope())
                using (var db = new MarvinEntities())
                {
                    var mappedObject = Mapper.Map <job>(job);
                    var result       = db.job.Add(mappedObject);
                    db.SaveChanges();
                    scope.Complete();
                    return(result);
                }
        }
 public void Release(string semaphoreId, string callerId)
 {
     using (var scope = CreateScope())
         using (var db = new MarvinEntities())
         {
             var semaphore =
                 db.semaphore.SingleOrDefault(a => a.semaphoreId == semaphoreId && a.currentOwnerId == callerId);
             if (semaphore == null)
             {
                 return;
             }
             db.semaphore.Remove(semaphore);
             scope.Complete();
         }
 }
        public Job Get(string urn)
        {
            var id = Guid.Parse(Regex.Match(urn, "(?<=^urn:dr:marvin:job:).*", RegexOptions.IgnoreCase).Value);

            using (CreateScope())
                using (var db = new MarvinEntities())
                {
                    var job = db.job.Where(a => a.id == id).IncludeFullJob().FirstOrDefault();
                    if (job == null)
                    {
                        throw new KeyNotFoundException();
                    }
                    return(Mapper.Map <Job>(job));
                }
        }
 public IEnumerable <HealthCounter> ProbeAndPrune()
 {
     using (var scope = CreateScope())
         using (var db = new MarvinEntities())
         {
             var limit = _timeProvider.GetUtcNow() - MaxAge;
             var old   = db.healthCounter.Where(hc => hc.timestamp < limit);
             db.healthCounter.RemoveRange(old);
             db.SaveChanges();
             var res = db.healthCounter.Select(Mapper.Map <HealthCounter>).ToList();
             db.SaveChanges();
             scope.Complete();
             return(res);
         }
 }
        public void Update(Job job)
        {
            job.LastModified = _timeProvider.GetUtcNow();

            using (var scope = CreateScope())
                using (var db = new MarvinEntities())
                {
                    var dbJob = db.job.Where(a => a.id == job.Id).IncludeFullJob().FirstOrDefault();
                    if (dbJob == null)
                    {
                        throw new KeyNotFoundException("Could'nt find job to update - are you missing and ID?");
                    }
                    lock (JobMappingProfile.Lock)
                    {
                        JobMappingProfile.Db = db; // :skull: :arrow_backward: this is a horrible ugly hack. But need for deletions.
                        Mapper.Map(job, dbJob);
                    }
                    db.SaveChanges();
                    scope.Complete();
                }
        }
 public bool Get(string semaphoreId, string callerId, out string currentOwnerId)
 {
     using (var scope = CreateScope())
         using (var db = new MarvinEntities())
         {
             try
             {
                 //Get semaphore by ID
                 var semaphore = db.semaphore.SingleOrDefault(a => a.semaphoreId == semaphoreId);
                 //if we don't get one, create it!
                 if (semaphore == null)
                 {
                     db.semaphore.Add(new semaphore
                     {
                         semaphoreId    = semaphoreId,
                         currentOwnerId = callerId,
                         heartBeat      = _timeProvider.GetUtcNow()
                     });
                     //Try to save changes
                     try
                     {
                         db.SaveChanges();
                         currentOwnerId = callerId;
                         return(true);
                     }
                     //If we get updateexception due to new record already inserted - get that record and return false.
                     catch (DbUpdateException)
                     {
                         var newSemaphore = db.semaphore.Single(a => a.semaphoreId == semaphoreId);
                         currentOwnerId = newSemaphore.currentOwnerId;
                         return(false);
                     }
                 }
                 //If caller isn't owner and maxage haven't been reached return currentowner.
                 if (semaphore.currentOwnerId != callerId &&
                     semaphore.heartBeat.Add(MaxAge) > _timeProvider.GetUtcNow())
                 {
                     currentOwnerId = semaphore.currentOwnerId;
                     return(false);
                 }
                 //Otherwise we need to attach caller as owner and update semaphore.
                 semaphore.currentOwnerId = callerId;
                 semaphore.heartBeat      = _timeProvider.GetUtcNow();
                 //Try to save Semaphore entry
                 try
                 {
                     db.SaveChanges();
                     currentOwnerId = callerId;
                 }
                 //If row has been updated in the meantime. Reload DB version and return it
                 catch (DbUpdateConcurrencyException ex)
                 {
                     ex.Entries.Single().Reload();
                     currentOwnerId = semaphore.currentOwnerId;
                     return(false);
                 }
                 return(true);
             }
             finally
             {
                 scope.Complete();
             }
         }
 }