Exemplo n.º 1
0
        public void Handle(CoreProjectionManagementMessage.CreateAndPrepare message)
        {
            try
            {
                //TODO: factory method can throw
                IProjectionStateHandler stateHandler = message.HandlerFactory();
                string name             = message.Name;
                var    sourceDefinition = ProjectionSourceDefinition.From(
                    name, stateHandler.GetSourceDefinition(), message.HandlerType, message.Query);
                var projectionVersion = message.Version;
                var projectionConfig  = message.Config;
                var namesBuilder      = new ProjectionNamesBuilder(name, sourceDefinition);

                var projectionProcessingStrategy = _processingStrategySelector.CreateProjectionProcessingStrategy(
                    name, projectionVersion, namesBuilder,
                    sourceDefinition, projectionConfig, message.HandlerFactory, stateHandler);

                var slaveProjections = projectionProcessingStrategy.GetSlaveProjections();
                CreateCoreProjection(message.ProjectionId, projectionConfig.RunAs, projectionProcessingStrategy);
                message.Envelope.ReplyWith(
                    new CoreProjectionManagementMessage.Prepared(
                        message.ProjectionId, sourceDefinition, slaveProjections));
            }
            catch (Exception ex)
            {
                message.Envelope.ReplyWith(
                    new CoreProjectionManagementMessage.Faulted(message.ProjectionId, ex.Message));
            }
        }
Exemplo n.º 2
0
        private void BeginCreateAndPrepare(
            ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config,
            Action onPrepared)
        {
            _onPrepared = onPrepared;
            if (handlerFactory == null)
            {
                throw new ArgumentNullException("handlerFactory");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            //TODO: which states are allowed here?
            if (_state >= ManagedProjectionState.Preparing)
            {
                throw new InvalidOperationException("Already preparing or has been prepared");
            }

            //TODO: load configuration from the definition


            var createProjectionMessage =
                new CoreProjectionManagementMessage.CreateAndPrepare(
                    new PublishEnvelope(_inputQueue), _id, _name, config, delegate
            {
                // this delegate runs in the context of a projection core thread
                // TODO: move this code to the projection core service as we may be in different processes in the future
                IProjectionStateHandler stateHandler = null;
                try
                {
                    stateHandler = handlerFactory.Create(HandlerType, Query, Console.WriteLine);
                    var checkpointStrategyBuilder = new CheckpointStrategy.Builder();
                    stateHandler.ConfigureSourceProcessingStrategy(checkpointStrategyBuilder);
                    checkpointStrategyBuilder.Validate(Mode);             // avoid future exceptions in coreprojection
                    return(stateHandler);
                }
                catch (Exception ex)
                {
                    SetFaulted(
                        string.Format(
                            "Cannot create a projection state handler.\r\n\r\nHandler type: {0}\r\nQuery:\r\n\r\n{1}\r\n\r\nMessage:\r\n\r\n{2}",
                            HandlerType, Query, ex.Message), ex);
                    if (stateHandler != null)
                    {
                        stateHandler.Dispose();
                    }
                    throw;
                }
            });

            //note: set runnign before start as coreProjection.start() can respond with faulted
            _state = ManagedProjectionState.Preparing;
            _coreQueue.Publish(createProjectionMessage);
        }
Exemplo n.º 3
0
        public void Handle(CoreProjectionManagementMessage.CreateAndPrepare message)
        {
            var command = new CreateAndPrepareCommand {
                Config      = new PersistedProjectionConfig(message.Config),
                HandlerType = message.HandlerType,
                Id          = message.ProjectionId.ToString("N"),
                Name        = message.Name,
                Query       = message.Query,
                Version     = message.Version,
            };

            _commandWriter.PublishResponse("$create-and-prepare", message.WorkerId, command);
        }
Exemplo n.º 4
0
        public void Handle(CoreProjectionManagementMessage.CreateAndPrepare message)
        {
            try
            {
                //TODO: factory method can throw
                var stateHandler = CreateStateHandler(
                    _timeoutScheduler,
                    _logger,
                    message.HandlerType,
                    message.Query);

                string name             = message.Name;
                var    sourceDefinition = ProjectionSourceDefinition.From(stateHandler.GetSourceDefinition());

                var projectionVersion = message.Version;
                var projectionConfig  = message.Config;
                var namesBuilder      = new ProjectionNamesBuilder(name, sourceDefinition);

                var projectionProcessingStrategy = _processingStrategySelector.CreateProjectionProcessingStrategy(
                    name,
                    projectionVersion,
                    namesBuilder,
                    sourceDefinition,
                    projectionConfig,
                    stateHandler,
                    message.HandlerType,
                    message.Query);

                CreateCoreProjection(message.ProjectionId, projectionConfig.RunAs, projectionProcessingStrategy);
                _publisher.Publish(
                    new CoreProjectionStatusMessage.Prepared(
                        message.ProjectionId, sourceDefinition));
            }
            catch (Exception ex)
            {
                _publisher.Publish(
                    new CoreProjectionStatusMessage.Faulted(message.ProjectionId, ex.Message));
            }
        }
Exemplo n.º 5
0
 public void Handle(CoreProjectionManagementMessage.CreateAndPrepare message)
 {
     try
     {
         //TODO: factory method can throw!
         IProjectionStateHandler stateHandler = message.HandlerFactory();
         // constructor can fail if wrong source definition
         ProjectionSourceDefinition sourceDefinition;
         var projection = CoreProjection.CreateAndPrepare(
             message.Name, message.Version, message.ProjectionId, _publisher, stateHandler, message.Config,
             _readDispatcher, _writeDispatcher, _subscriptionDispatcher, _logger, _timeProvider,
             out sourceDefinition);
         _projections.Add(message.ProjectionId, projection);
         message.Envelope.ReplyWith(
             new CoreProjectionManagementMessage.Prepared(message.ProjectionId, sourceDefinition));
     }
     catch (Exception ex)
     {
         message.Envelope.ReplyWith(
             new CoreProjectionManagementMessage.Faulted(message.ProjectionId, ex.Message));
     }
 }
 public void Handle(CoreProjectionManagementMessage.CreateAndPrepare message)
 {
     try
     {
         //TODO: factory method can throw!
         IProjectionStateHandler stateHandler = message.HandlerFactory();
         // constructor can fail if wrong source defintion
         //TODO: revise it
         var sourceDefintionRecorder = new SourceDefintionRecorder();
         stateHandler.ConfigureSourceProcessingStrategy(sourceDefintionRecorder);
         var sourceDefintion = sourceDefintionRecorder.Build();
         var projection      = new CoreProjection(
             message.Name, message.CorrelationId, _publisher, stateHandler, message.Config, _readDispatcher,
             _writeDispatcher, _logger);
         _projections.Add(message.CorrelationId, projection);
         message.Envelope.ReplyWith(
             new CoreProjectionManagementMessage.Prepared(message.CorrelationId, sourceDefintion));
     }
     catch (Exception ex)
     {
         message.Envelope.ReplyWith(
             new CoreProjectionManagementMessage.Faulted(message.CorrelationId, ex.Message));
     }
 }
Exemplo n.º 7
0
        private void BeginCreateAndPrepare(
            ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config, Action onPrepared)
        {
            _onPrepared = _onStopped = () =>
            {
                _onStopped  = null;
                _onPrepared = null;
                if (onPrepared != null)
                {
                    onPrepared();
                }
            };
            if (handlerFactory == null)
            {
                throw new ArgumentNullException("handlerFactory");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            //TODO: which states are allowed here?
            if (_state >= ManagedProjectionState.Preparing)
            {
                DisposeCoreProjection();
                _state = ManagedProjectionState.Loaded;
            }

            //TODO: load configuration from the definition


            var createProjectionMessage =
                new CoreProjectionManagementMessage.CreateAndPrepare(
                    new PublishEnvelope(_inputQueue), _id, _name,
                    new ProjectionVersion(_projectionId, _persistedState.Epoch ?? 0, _persistedState.Version ?? 0),
                    config, delegate
            {
                // this delegate runs in the context of a projection core thread
                // TODO: move this code to the projection core service as we may be in different processes in the future
                IProjectionStateHandler stateHandler = null;
                try
                {
                    stateHandler = handlerFactory.Create(
                        HandlerType, Query, logger: Console.WriteLine,
                        cancelCallbackFactory:
                        _timeoutScheduler == null
                                            ? (Action <int, Action>)null
                                            : _timeoutScheduler.Schedule);
                    return(stateHandler);
                }
                catch (Exception ex)
                {
                    SetFaulted(
                        string.Format(
                            "Cannot create a projection state handler.\r\n\r\nHandler type: {0}\r\nQuery:\r\n\r\n{1}\r\n\r\nMessage:\r\n\r\n{2}",
                            HandlerType, Query, ex.Message), ex);
                    if (stateHandler != null)
                    {
                        stateHandler.Dispose();
                    }
                    throw;
                }
            });

            //note: set runnign before start as coreProjection.start() can respond with faulted
            _state = ManagedProjectionState.Preparing;
            _coreQueue.Publish(createProjectionMessage);
        }