コード例 #1
0
ファイル: StateEngine.cs プロジェクト: psibr/REstate-Legacy
        public async Task <Schematic> GetSchematic(string schematicName, CancellationToken cancellationToken)
        {
            Schematic configuration;

            using (var repository = _repositoryContextFactory
                                    .OpenContext(_apiKey))
            {
                configuration = await repository.Schematics
                                .RetrieveSchematicAsync(schematicName, cancellationToken)
                                .ConfigureAwait(false);
            }

            return(configuration);
        }
コード例 #2
0
        public async Task <InstanceRecord> FireAsync(
            Trigger trigger,
            string contentType, string payload, Guid?lastCommitTag,
            CancellationToken cancellationToken)
        {
            using (var dataContext = _repositoryContextFactory.OpenContext(ApiKey))
            {
                var currentState = await dataContext.Machines
                                   .GetMachineStateAsync(MachineId, cancellationToken).ConfigureAwait(false);

                var stateConfig = StateMappings[currentState];

                var transition = stateConfig.Transitions.Single(t => t.TriggerName == trigger.TriggerName);

                if (transition.Guard != null)
                {
                    var guardConnector = await _connectorFactoryResolver
                                         .ResolveConnectorFactory(transition.Guard.ConnectorKey)
                                         .BuildConnectorAsync(ApiKey, cancellationToken).ConfigureAwait(false);

                    if (!await guardConnector
                        .ConstructPredicate(this, transition.Guard.Configuration)
                        .Invoke(cancellationToken).ConfigureAwait(false))
                    {
                        throw new InvalidOperationException("Guard clause prevented transition.");
                    }
                }

                currentState = await dataContext.Machines.SetMachineStateAsync(
                    MachineId,
                    transition.ResultantStateName,
                    trigger.TriggerName,
                    lastCommitTag ?? Guid.Parse(currentState.CommitTag),
                    cancellationToken).ConfigureAwait(false);

                stateConfig = StateMappings[currentState];

                if (stateConfig.OnEntry != null)
                {
                    try
                    {
                        var entryConnector = await _connectorFactoryResolver
                                             .ResolveConnectorFactory(stateConfig.OnEntry.ConnectorKey)
                                             .BuildConnectorAsync(ApiKey, cancellationToken).ConfigureAwait(false);

                        await entryConnector
                        .ConstructAction(this, currentState, contentType, payload, stateConfig.OnEntry.Configuration)
                        .Invoke(cancellationToken).ConfigureAwait(false);
                    }
                    catch
                    {
                        if (stateConfig.OnEntry.FailureTransition != null)
                        {
                            await FireAsync(
                                new Trigger(stateConfig.OnEntry.FailureTransition.TriggerName),
                                contentType,
                                payload,
                                Guid.Parse(currentState.CommitTag),
                                cancellationToken).ConfigureAwait(false);
                        }

                        throw;
                    }
                }

                return(currentState);
            }
        }