/// <summary> /// 移除超过24小时已完成的事件锁、事件监视器、队列信息 /// 我们不能在执行完领域事件后立即删除这些信息,因为有可能是外界调用本地的事件,稍后可能外界要求回逆事件 /// 因此我们只删除24小时过期的信息,因为外界不可能过了24小时后又要求回逆 /// </summary> /// <param name="minutes"></param> private static void Clear() { DataContext.NewScope(() => { var repository = Repository.Create <IEventLockRepository>(); var locks = repository.FindExpireds(24); foreach (var @lock in locks) { var queueId = @lock.Id; var queue = EventQueue.Find(queueId); if (!queue.IsSucceeded) { continue; //对于没有执行成功的队列,我们不删除日志等信息,这样管理员可以排查错误 } DataContext.NewScope(() => { var monitor = EventMonitor.Find(queueId); if (!monitor.IsEmpty()) { EventMonitor.Delete(monitor); } EventQueue.Delete(queueId); EventLogEntry.Deletes(queueId); //删除日志的所有条目 EventLog.Delete(queueId); }, true); EventLock.Delete(@lock); } }, true); }
/// <summary> /// 根据写入的日志执行恢复操作 /// </summary> /// <param name="logId"></param> /// <param name="process"></param> internal static void Restore(Guid logId, Action <EventLog, EventLogEntry> process) { DataContext.NewScope(() => { var log = GetLog(logId); if (log.IsEmpty() || log.Status == EventLogStatus.Reversed) { return; //表示已经回逆了 } AppSession.Identity = log.GetIdentity(); //初始化会话身份 var repository = Repository.Create <IEventLogEntryRepository>(); var entries = repository.FindByReverseOrder(logId); foreach (var entry in entries) { if (entry.IsReversed) { continue; } //为日志条目的回逆创建独立的事务 DataContext.NewScope(() => { process(log, entry); entry.IsReversed = true; EventLogEntry.Update(entry); }, true); } log.Status = EventLogStatus.Reversed; UpdateLog(log); }, true); }
private static void Write(EventLog log, EventOperation operation, DTObject content) { //写入日志条目 var index = log.EntryCount; var contentCode = content.IsEmpty() ? "{}" : content.GetCode(); var entry = new EventLogEntry(log, operation, contentCode, index); var repository = Repository.Create <IEventLogEntryRepository>(); repository.Add(entry); log.EntryCount++; UpdateLog(log); }