public async Task <ISaga> Publish(ISagaEvent @event, IDictionary <string, object> executionValues, Guid?parentId, SagaRunOptions runOptions) { runOptions = runOptions ?? new SagaRunOptions(); Type eventType = @event.GetType(); SagaID sagaId = SagaID.From(@event.ID); ISagaModel model = sagaRegistrator. FindModelForEventType(eventType); if (model == null) { throw new SagaEventNotRegisteredException(eventType); } SagaID?parentSagaId = parentId == null ? (SagaID?)null : SagaID.From(parentId.Value); ISaga newSaga = await CreateNewSagaIfRequired(model, sagaId, parentSagaId, eventType); var id = SagaID.From(newSaga?.Data?.ID ?? sagaId.Value); try { var createdSaga = await ExecuteSaga( @event, model, newSaga, id, executionValues, false, runOptions); return(createdSaga); } /*catch (SagaStopException ex) * { * return await sagaPersistance.Get(id.Value); * }*/ catch { //if (newSaga != null) // await sagaPersistance.Remove(newSaga.Data.ID); throw; } }
private async Task <ISaga> ExecuteSaga( ISagaEvent @event, ISagaModel model, ISaga saga, Guid sagaID, IDictionary <string, object> executionValues, bool executeResuming, SagaRunOptions runOptions) { bool sagaStarted = false; try { serviceProvider. GetRequiredService <ObservableRegistrator>(). Initialize(); await messageBus. Publish(new ExecutionStartMessage(saga ?? new Saga { Data = new EmptySagaData { ID = sagaID } }, model)); sagaStarted = true; if (saga == null) { saga = await sagaPersistance.Get(sagaID); } if (saga == null) { throw new SagaInstanceNotFoundException(); } if (saga.ExecutionState.IsDeleted) { throw new CountNotExecuteDeletedSagaException(sagaID); } if (!executeResuming) { if (runOptions != null) { saga.ExecutionState.CanBeResumed = runOptions.CanBeResumed; } if (saga.IsIdle()) { saga.ExecutionState.CurrentError = null; saga.ExecutionState.ExecutionID = ExecutionID.New(); if (model.HistoryPolicy == ESagaHistoryPolicy.StoreOnlyCurrentStep) { saga.ExecutionState.History.Clear(); } saga.ExecutionValues. Set(executionValues); saga.ExecutionState. CurrentEvent = @event ?? new EmptyEvent(); } else { throw new SagaNeedToBeResumedException(saga.Data.ID); } logger. LogInformation($"Executing saga: {saga.Data.ID}"); } else { logger. LogInformation($"Resuming saga: {saga.Data.ID}"); } ExecuteActionCommandHandler handler = serviceProvider. GetRequiredService <ExecuteActionCommandHandler>(); return(await handler.Handle(new ExecuteActionCommand { Async = AsyncExecution.False(), Saga = saga, Model = model })); } catch (Exception ex) { if (sagaStarted) { await messageBus.Publish( new ExecutionEndMessage(new Saga { Data = new EmptySagaData { ID = sagaID } }, ex)); } if (ex is SagaStepException sagaStepException && sagaStepException?.OriginalException != null) { throw sagaStepException.OriginalException; } throw; } }
public async Task <ISaga> Publish(ISagaEvent @event, IDictionary <string, object> executionValues, SagaRunOptions runOptions) { return(await Publish(@event, executionValues, null, runOptions)); }