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);
         }
 }
 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 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();
         }
 }
 public void Reset()
 {
     using (var scope = CreateScope())
         using (var db = new MarvinEntities())
         {
             db.semaphore.RemoveRange(db.semaphore);
             db.SaveChanges();
             scope.Complete();
         }
 }
Beispiel #5
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 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);
                }
        }
Beispiel #7
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();
     }
 }
Beispiel #8
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();
            }
        }
        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();
             }
         }
 }