/// <summary> /// Trigger commit success event /// </summary> /// <param name="commitResult">Work commit result</param> void TriggerCommitSuccessEvent(WorkCommitResult commitResult) { foreach (var handler in commitSuccessEventHandlers) { var eventHandler = handler; ThreadPool.QueueUserWorkItem(s => { eventHandler(this, commitResult, commandCollection); }); } }
/// <summary> /// Trigger commit fail event /// </summary> /// <param name="work">Work object</param> /// <param name="commitResult">Work commit result</param> /// <param name="commands">Commands</param> internal static void TriggerWorkCommitFailEvent(IWork work, WorkCommitResult commitResult, IEnumerable <ICommand> commands) { if (WorkCommitFailEventHandlers.IsNullOrEmpty()) { return; } foreach (var handler in WorkCommitFailEventHandlers) { var eventHandler = handler; ThreadPool.QueueUserWorkItem(s => { eventHandler(work, commitResult, commands); }); } }
/// <summary> /// Commit work /// </summary> /// <returns>Return work commit result</returns> public async Task <WorkCommitResult> CommitAsync() { try { //build commands BuildCommand(); if (commandEngineGroups.IsNullOrEmpty()) { return(WorkCommitResult.Empty()); } var executeOption = GetCommandExecuteOption(); var result = await CommandExecuteManager.ExecuteAsync(executeOption, commandEngineGroups.Values).ConfigureAwait(false); var commitResult = new WorkCommitResult() { CommitCommandCount = commandCollection.Count, ExecutedDataCount = result, AllowEmptyResultCommandCount = allowEmptyResultCommandCount }; // trigger command callback event TriggerCommandCallbackEvent(commitResult.EmptyResultOrSuccess); if (commitResult.EmptyResultOrSuccess) { //Trigger commit success event TriggerCommitSuccessEvent(commitResult); //Trigger work global success event WorkManager.TriggerWorkCommitSuccessEvent(this, commitResult, commandCollection); //Execute domain event TriggerWorkCompletedDomainEvent(); //Execute data access event TriggerDataAccessEvent(); } else { //Trigger work global commit fail event WorkManager.TriggerWorkCommitFailEvent(this, commitResult, commandCollection); } return(commitResult); } catch (Exception ex) { Reset(); throw ex; } }
/// <summary> /// Commit work /// </summary> /// <returns>Return work commit result</returns> public async Task <WorkCommitResult> CommitAsync() { try { if (allowTraceLog) { LogManager.LogInformation <DefaultWork>($"===== Work:{WorkId} commit begin ====="); } //build commands BuildCommand(); WorkCommitResult commitResult = null; if (commandEngineGroups.IsNullOrEmpty()) { commitResult = WorkCommitResult.Empty(); } else { var executeOptions = GetCommandExecuteOptions(); if (allowTraceLog) { LogManager.LogInformation <DefaultWork>($"Work:{WorkId},Command execute options:{JsonSerializeHelper.ObjectToJson(executeOptions)}"); LogManager.LogInformation <DefaultWork>($"Work:{WorkId},Command engine keys:{string.Join(",", commandEngineGroups.Keys)},Command count:{commandCollection.Count}"); } int returnValue = await CommandExecuteManager.ExecuteAsync(executeOptions, commandEngineGroups.Values).ConfigureAwait(false); commitResult = new WorkCommitResult() { CommitCommandCount = commandCollection.Count, ExecutedDataCount = returnValue, AllowEmptyResultCommandCount = allowEmptyResultCommandCount }; } // trigger command callback event TriggerCommandCallbackEvent(commitResult.EmptyResultOrSuccess); if (commitResult.EmptyResultOrSuccess) { //Trigger commit success event TriggerCommitSuccessEvent(commitResult); //Trigger work global success event WorkManager.TriggerWorkCommitSuccessEvent(this, commitResult, commandCollection); //Execute domain event TriggerWorkCompletedDomainEvent(); //Execute data access event TriggerDataAccessEvent(); } else { //Trigger work global commit fail event WorkManager.TriggerWorkCommitFailEvent(this, commitResult, commandCollection); } if (allowTraceLog) { LogManager.LogInformation <DefaultWork>($"Work:{WorkId},Commit command count:{commitResult.CommitCommandCount},Execute data count:{commitResult.ExecutedDataCount},Allow empty result command result:{allowEmptyResultCommandCount}"); } return(commitResult); } catch (Exception ex) { Reset(); if (allowTraceLog) { LogManager.LogInformation <DefaultWork>($"Work:{WorkId},Exception message : {ex.Message}"); } throw ex; } finally { if (allowTraceLog) { LogManager.LogInformation <DefaultWork>($"===== Work:{WorkId} commit end ====="); } } }