public Task(IContentManager contentManager, ScheduledTaskRecord record) { // in spite of appearances, this is actually a created class, not IoC, // but dependencies are passed in for lazy initialization purposes _contentManager = contentManager; _record = record; }
public void Sweep() { try { while (true) { ScheduledTaskRecord taskToRun = new ScheduledTaskRecord(); using (var scope = new TransactionScope(TransactionScopeOption.Suppress, new TransactionOptions() { IsolationLevel = IsolationLevel.Serializable })) { var taskRecord = _repository.Fetch(x => x.ScheduledUtc <= _clock.UtcNow).FirstOrDefault(); // another server or thread has performed this work before us if (taskRecord == null) { break; } _repository.Copy(taskRecord, taskToRun); _repository.Delete(taskRecord); _repository.Flush(); scope.Complete(); } //using (ISession _session = _sessionLocator.For(typeof(ScheduledTaskRecord))) //{ // using (var tx = _session.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted)) // { using (var scope = new TransactionScope(TransactionScopeOption.Required)) { var context = new ScheduledTaskContext { Task = new Task(_contentManager, taskToRun) }; // dispatch to standard or custom handlers foreach (var handler in _handlers) { handler.Process(context); } scope.Complete(); } _contentManager.Flush(); _contentManager.Clear(); } } catch (Exception ex) { Logger.Warning(ex, "Unable to process scheduled task."); //TODO: handle exception to rollback dedicated xact, and re-delete task record. // does this also need some retry logic? //Maybe put in taskToRun back in if retry parameter included? } }
public void CreateTask(string action, DateTime scheduledUtc, ContentItem contentItem) { var taskRecord = new ScheduledTaskRecord { TaskType = action, ScheduledUtc = scheduledUtc, }; if (contentItem != null) { taskRecord.ContentItemVersionRecord = contentItem.VersionRecord; } _repository.Create(taskRecord); }
public void CreateTask(string action, DateTime scheduledUtc) { var taskRecord = new ScheduledTaskRecord { TaskType = action, ScheduledUtc = scheduledUtc, }; _repository.Create(taskRecord); }
public void RecordsWhenTheyAreExecutedShouldBeDeleted() { var task = new ScheduledTaskRecord { TaskType = "Ignore", ScheduledUtc = _clock.UtcNow.Add(TimeSpan.FromHours(2)) }; _repository.Create(task); _repository.Flush(); _executor.Sweep(); _repository.Flush(); Assert.That(_repository.Count(x => x.TaskType == "Ignore"), Is.EqualTo(1)); _clock.Advance(TimeSpan.FromHours(3)); _repository.Flush(); _executor.Sweep(); _repository.Flush(); Assert.That(_repository.Count(x => x.TaskType == "Ignore"), Is.EqualTo(0)); }
public Task(ScheduledTaskRecord record) { _record = record; }
public void ScheduledTaskHandlersShouldBeCalledWhenTasksAreExecuted() { var task = new ScheduledTaskRecord { TaskType = "Ignore", ScheduledUtc = _clock.UtcNow.Add(TimeSpan.FromHours(2)) }; _repository.Create(task); _repository.Flush(); _clock.Advance(TimeSpan.FromHours(3)); Assert.That(_handler.TaskContext, Is.Null); _executor.Sweep(); Assert.That(_handler.TaskContext, Is.Not.Null); Assert.That(_handler.TaskContext.Task.TaskType, Is.EqualTo("Ignore")); Assert.That(_handler.TaskContext.Task.ContentItem, Is.Null); }