public int EnqueueRecurrentTask(Guid identifier, long interval) { if (interval < 1000) { throw new ArgumentException("interval"); } var info = this.GetTaskInfo(identifier); if (info == null) { throw new ArgumentException(String.Format("Task identifier [{0}] not registered.", identifier.ToString())); } LockInfo lockInfo = null; try { if (LockManager.WaitForLock(ENQUEUE_RECURRENT_TASK_LOCK, new TimeSpan(TimeSpan.TicksPerMinute), new TimeSpan(TimeSpan.TicksPerSecond * 10), out lockInfo)) { using (var tr = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) using (var context = CreateRepository()) { //Adds or updates a recurrent task var data = context.RecurrentTasks .Where(t => t.Identifier == info.Identifier) .OrderBy(t => t.Id).FirstOrDefault(); data = data ?? new RecurrentTaskDefinition() { Identifier = info.Identifier }; data.Enabled = true; data.Interval = interval; if (data.Id == 0) { context.RecurrentTasks.Add(data); } context.SaveChanges(); var copies = context.RecurrentTasks .Where(t => t.Identifier == info.Identifier) .OrderBy(t => t.Id).ToList(); if (copies.Count == 0) { throw new ApplicationException("Database task entry was modified while enqueuing task."); } //Recurrent tasks cannot have duplicates foreach (var duplicate in copies.Skip(1)) { context.RecurrentTasks.Remove(duplicate); } var first = copies.FirstOrDefault(); if (first.Interval != interval) { first.Interval = interval; } context.SaveChanges(); return(first.Id); } } else { throw new ApplicationException("Lock for resource ENQUEUE_RECURRENT_TASK_LOCK has been constantly denied."); } } finally { LockManager.Release(lockInfo); } }