示例#1
0
        // EnsureIdTokenCacheAsync() -> object<nn::account::detail::IAsyncContext>
        public ResultCode EnsureIdTokenCacheAsync(ServiceCtx context)
        {
            KEvent         asyncEvent     = new KEvent(context.Device.System.KernelContext);
            AsyncExecution asyncExecution = new AsyncExecution(asyncEvent);

            asyncExecution.Initialize(1000, EnsureIdTokenCacheAsyncImpl);

            MakeObject(context, new IAsyncContext(asyncExecution));

            // return ResultCode.NullObject if the IAsyncContext pointer is null. Doesn't occur in our case.

            return(ResultCode.Success);
        }
示例#2
0
        private async Task <ISaga> DispatchStepSync(
            ExecuteStepCommand command)
        {
            using (IServiceScope scope = serviceScopeFactory.CreateScope())
            {
                ExecuteStepCommandHandler stepExecutor = ActivatorUtilities.
                                                         CreateInstance <ExecuteStepCommandHandler>(scope.ServiceProvider);

                ISaga saga = await stepExecutor.
                             Handle(command);

                if (saga == null)
                {
                    await messageBus.Publish(
                        new ExecutionEndMessage(SagaID.From(command.Saga.Data.ID)));

                    return(null);
                }
                else
                {
                    if (saga.IsIdle())
                    {
                        await messageBus.Publish(
                            new ExecutionEndMessage(SagaID.From(saga.Data.ID)));

                        if (saga.HasError())
                        {
                            throw saga.ExecutionState.CurrentError;
                        }

                        return(saga);
                    }
                    else
                    {
                        return(await Handle(new ExecuteActionCommand()
                        {
                            Async = AsyncExecution.False(),
                            Saga = saga,
                            Model = command.Model
                        }));
                    }
                }
            }
        }
        public async Task <ISaga> Handle(ExecuteActionCommand command)
        {
            ISaga saga = command.Saga;

            if (saga == null)
            {
                throw new SagaInstanceNotFoundException(command.Model.SagaStateType);
            }

            ISagaStep step = command.Model.Actions.
                             FindStepForExecutionStateAndEvent(saga);

            ISagaAction action = command.Model.
                                 FindActionForStep(step);

            if (step.Async)
            {
                saga.ExecutionState.AsyncExecution = AsyncExecution.True();
            }

            ExecuteStepCommand executeStepCommand = new ExecuteStepCommand
            {
                Saga       = saga,
                SagaStep   = step,
                SagaAction = action,
                Model      = command.Model
            };

            logger.
            LogDebug($"Saga: {saga.Data.ID}; Executing {(step.Async ? "async " : "")}step: {step.StepName}");

            if (step.Async)
            {
                DispatchStepAsync(executeStepCommand);
                return(saga);
            }
            else
            {
                using (IServiceScope scope = serviceScopeFactory.CreateScope())
                {
                    return(await DispatchStepSync(scope.ServiceProvider, executeStepCommand));
                }
            }
        }
        private async Task <ISaga> ExecuteSaga(
            ISagaEvent @event, ISagaModel model,
            ISaga saga,
            Guid sagaID,
            IDictionary <string, object> executionValues,
            bool resume)
        {
            bool sagaStarted = false;

            try
            {
                serviceProvider.
                GetRequiredService <ObservableRegistrator>().
                Initialize();

                await messageBus.
                Publish(new ExecutionStartMessage(SagaID.From(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 (!resume)
                {
                    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(SagaID.From(sagaID)));
                }

                if (ex is SagaStepException sagaStepException && sagaStepException?.OriginalException != null)
                {
                    throw sagaStepException.OriginalException;
                }

                throw;
            }
        }
示例#5
0
 public IAsyncContext(AsyncExecution asyncExecution)
 {
     _asyncExecution = asyncExecution;
 }