public ManagedProjection(
     IPublisher coreQueue,
     Guid id, string name, ILogger logger,
     RequestResponseDispatcher <ClientMessage.WriteEvents, ClientMessage.WriteEventsCompleted> writeDispatcher,
     RequestResponseDispatcher <ClientMessage.ReadStreamEventsBackward, ClientMessage.ReadStreamEventsBackwardCompleted>
     readDispatcher, IPublisher inputQueue, ProjectionStateHandlerFactory projectionStateHandlerFactory)
 {
     if (id == Guid.Empty)
     {
         throw new ArgumentException("id");
     }
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (name == "")
     {
         throw new ArgumentException("name");
     }
     _coreQueue       = coreQueue;
     _id              = id;
     _name            = name;
     _logger          = logger;
     _writeDispatcher = writeDispatcher;
     _readDispatcher  = readDispatcher;
     _inputQueue      = inputQueue;
     _projectionStateHandlerFactory = projectionStateHandlerFactory;
 }
        public void Setup()
        {
            _state = null;
            _projection = null;
            Given();
            _logged = new List<string>();
            _stateHandlerFactory = new ProjectionStateHandlerFactory();
            _stateHandler = _stateHandlerFactory.Create(
                "JS", _projection, logger: s =>
                    {
                        if (s.StartsWith("P:"))
                            Console.WriteLine(s);
                        else
                            _logged.Add(s);
                    }); // skip prelude debug output
            _source = _stateHandler.GetSourceDefinition();

            if (_state != null)
                _stateHandler.Load(_state);
            else
                _stateHandler.Initialize();

            if (_sharedState != null)
                _stateHandler.LoadShared(_sharedState);
            When();
        }
        public void can_compile_million_times()
        {
            for (var i = 0; i < 10000000; i++)
            {
                if (_stateHandler != null)
                    _stateHandler.Dispose();
                _stateHandler = null;
/*
                _state = null;
*/
                _projection = null;
                _projection = @"
                fromAll();
                on_raw(function(state, event, streamId, eventType, sequenceNumber, metadata) {
                    emit('output-stream' + sequenceNumber, 'emitted-event' + sequenceNumber, {a: JSON.parse(event).a});
                    return {};
                });
            ";
                _logged = new List<string>();
                _stateHandlerFactory = new ProjectionStateHandlerFactory();
                _stateHandler = _stateHandlerFactory.Create(
                    "JS", _projection, logger: (s, _) =>
                        {
                            if (!s.StartsWith("P:")) _logged.Add(s);
                            else _logDelegate(s);
                        }); // skip prelude debug output
/*
                if (_state != null)
                    _stateHandler.Load(_state);
                else
                    _stateHandler.Initialize();
*/
                Console.Write(".");
            }
        }
Exemple #4
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);
        }
Exemple #5
0
 public ManagedProjection(
     IPublisher coreQueue, Guid id, int projectionId, string name, bool enabledToRun, ILogger logger,
     RequestResponseDispatcher <ClientMessage.WriteEvents, ClientMessage.WriteEventsCompleted> writeDispatcher,
     RequestResponseDispatcher
     <ClientMessage.ReadStreamEventsBackward, ClientMessage.ReadStreamEventsBackwardCompleted> readDispatcher,
     IPublisher inputQueue, IPublisher output, ProjectionStateHandlerFactory projectionStateHandlerFactory,
     ITimeProvider timeProvider, ISingletonTimeoutScheduler timeoutScheduler = null, bool isSlave = false,
     IPublisher slaveResultsPublisher = null, Guid slaveMasterCorrelationId = default(Guid))
 {
     if (coreQueue == null)
     {
         throw new ArgumentNullException("coreQueue");
     }
     if (id == Guid.Empty)
     {
         throw new ArgumentException("id");
     }
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (output == null)
     {
         throw new ArgumentNullException("output");
     }
     if (name == "")
     {
         throw new ArgumentException("name");
     }
     _coreQueue       = coreQueue;
     _id              = id;
     _projectionId    = projectionId;
     _name            = name;
     _enabledToRun    = enabledToRun;
     _logger          = logger;
     _writeDispatcher = writeDispatcher;
     _readDispatcher  = readDispatcher;
     _inputQueue      = inputQueue;
     _output          = output;
     _projectionStateHandlerFactory = projectionStateHandlerFactory;
     _timeProvider             = timeProvider;
     _timeoutScheduler         = timeoutScheduler;
     _isSlave                  = isSlave;
     _slaveResultsPublisher    = slaveResultsPublisher;
     _slaveMasterCorrelationId = slaveMasterCorrelationId;
     _getStateDispatcher       =
         new RequestResponseDispatcher
         <CoreProjectionManagementMessage.GetState, CoreProjectionManagementMessage.StateReport>(
             coreQueue, v => v.CorrelationId, v => v.CorrelationId, new PublishEnvelope(_inputQueue));
     _getResultDispatcher =
         new RequestResponseDispatcher
         <CoreProjectionManagementMessage.GetResult, CoreProjectionManagementMessage.ResultReport>(
             coreQueue, v => v.CorrelationId, v => v.CorrelationId, new PublishEnvelope(_inputQueue));
     _lastAccessed = _timeProvider.Now;
 }
        private void Start(IPublisher coreOutput, ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config)
        {
            if (coreOutput == null)
            {
                throw new ArgumentNullException("coreOutput");
            }
            if (handlerFactory == null)
            {
                throw new ArgumentNullException("handlerFactory");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (_coreProjection != null)
            {
                throw new InvalidOperationException("CoreProjection has been already created");
            }

            IProjectionStateHandler stateHandler = null;

            try
            {
                stateHandler = handlerFactory.Create(HandlerType, Query, Console.WriteLine);
                var checkpointStrategyBuilder = new CheckpointStrategy.Builder();
                stateHandler.ConfigureSourceProcessingStrategy(checkpointStrategyBuilder);
                checkpointStrategyBuilder.Validate(this.Mode); // avoid future exceptions in coreprojection
                // constructor can fail if wrong source defintion
                //TODO: revise it
                _coreProjection = new CoreProjection(_name, _id, coreOutput, stateHandler, config, _logger);
            }
            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();
                }
                return;
            }

            //TODO: load configuration from the definition
            _state = ManagedProjectionState.Running;
            //note: set runnign before start as coreProjection.start() can respond with faulted
            _coreProjection.Start();
        }
 public ManagedProjection(
     Guid id, string name, ILogger logger,
     RequestResponseDispatcher<ClientMessage.WriteEvents, ClientMessage.WriteEventsCompleted> writeDispatcher,
     RequestResponseDispatcher<ClientMessage.ReadEventsBackwards, ClientMessage.ReadEventsBackwardsCompleted>
         readDispatcher, IPublisher publisher, ProjectionStateHandlerFactory projectionStateHandlerFactory)
 {
     if (id == Guid.Empty) throw new ArgumentException("id");
     if (name == null) throw new ArgumentNullException("name");
     if (name == "") throw new ArgumentException("name");
     _id = id;
     _name = name;
     _logger = logger;
     _writeDispatcher = writeDispatcher;
     _readDispatcher = readDispatcher;
     _publisher = publisher;
     _projectionStateHandlerFactory = projectionStateHandlerFactory;
 }
        private void Start(ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config)
        {
            if (handlerFactory == null)
            {
                throw new ArgumentNullException("handlerFactory");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (_state == ManagedProjectionState.Running)
            {
                throw new InvalidOperationException("Already started");
            }

            //TODO: load configuration from the definition


            var createProjectionMessage = new ProjectionMessage.CoreService.Management.Create(new PublishEnvelope(_inputQueue), _id, _name, config, delegate
            {
                IProjectionStateHandler stateHandler = null;
                try
                {
                    stateHandler = handlerFactory.Create(this.HandlerType, this.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.Running;
            _coreQueue.Publish(createProjectionMessage);
            _coreQueue.Publish(new ProjectionMessage.Projections.Management.Start(_id));
        }
        public ProjectionManager(
            IPublisher inputQueue, IPublisher publisher, IPublisher[] queues, ITimeProvider timeProvider,
            RunProjections runProjections, bool initializeSystemProjections = true)
        {
            if (inputQueue == null)
            {
                throw new ArgumentNullException("inputQueue");
            }
            if (publisher == null)
            {
                throw new ArgumentNullException("publisher");
            }
            if (queues == null)
            {
                throw new ArgumentNullException("queues");
            }
            if (queues.Length == 0)
            {
                throw new ArgumentException("At least one queue is required", "queues");
            }

            _inputQueue = inputQueue;
            _publisher  = publisher;
            _queues     = queues;

            _timeoutSchedulers = CreateTimeoutSchedulers(queues);

            _timeProvider   = timeProvider;
            _runProjections = runProjections;
            _initializeSystemProjections = initializeSystemProjections;

            _writeDispatcher =
                new RequestResponseDispatcher <ClientMessage.WriteEvents, ClientMessage.WriteEventsCompleted>(
                    publisher, v => v.CorrelationId, v => v.CorrelationId, new PublishEnvelope(_inputQueue));
            _readDispatcher =
                new RequestResponseDispatcher
                <ClientMessage.ReadStreamEventsBackward, ClientMessage.ReadStreamEventsBackwardCompleted>(
                    publisher, v => v.CorrelationId, v => v.CorrelationId, new PublishEnvelope(_inputQueue));


            _projectionStateHandlerFactory = new ProjectionStateHandlerFactory();
            _projections     = new Dictionary <string, ManagedProjection>();
            _projectionsMap  = new Dictionary <Guid, string>();
            _publishEnvelope = new PublishEnvelope(_inputQueue, crossThread: true);
        }
 public void setup()
 {
     _state = null;
     _projection = null;
     Given();
     _logged = new List<string>();
     _stateHandlerFactory = new ProjectionStateHandlerFactory();
     _stateHandler = _stateHandlerFactory.Create(
         "JS", _projection, s =>
             {
                 if (!s.StartsWith("P:")) _logged.Add(s);
                 else Console.WriteLine(s);
             }); // skip prelude debug output
     _source = new SourceRecorder();
     _stateHandler.ConfigureSourceProcessingStrategy(_source);
     if (_state != null)
         _stateHandler.Load(_state);
     else
         _stateHandler.Initialize();
 }
        public ProjectionManager(IPublisher coreOutput, ICheckpoint checkpointForStatistics)
        {
            if (coreOutput == null)
            {
                throw new ArgumentNullException("coreOutput");
            }

            _writeDispatcher =
                new RequestResponseDispatcher <ClientMessage.WriteEvents, ClientMessage.WriteEventsCompleted>(
                    coreOutput, v => v.CorrelationId, v => v.CorrelationId);
            _readDispatcher =
                new RequestResponseDispatcher
                <ClientMessage.ReadEventsBackwards, ClientMessage.ReadEventsBackwardsCompleted>(
                    coreOutput, v => v.CorrelationId, v => v.CorrelationId);

            _coreOutput = coreOutput;
            _checkpointForStatistics = checkpointForStatistics;

            _projectionStateHandlerFactory = new ProjectionStateHandlerFactory();
            _projections    = new Dictionary <string, ManagedProjection>();
            _projectionsMap = new Dictionary <Guid, string>();
        }
 public ManagedProjection(
     IPublisher coreQueue, Guid id, string name, ILogger logger,
     RequestResponseDispatcher<ClientMessage.WriteEvents, ClientMessage.WriteEventsCompleted> writeDispatcher,
     RequestResponseDispatcher
         <ClientMessage.ReadStreamEventsBackward, ClientMessage.ReadStreamEventsBackwardCompleted> readDispatcher,
     IPublisher inputQueue, ProjectionStateHandlerFactory projectionStateHandlerFactory)
 {
     if (id == Guid.Empty) throw new ArgumentException("id");
     if (name == null) throw new ArgumentNullException("name");
     if (name == "") throw new ArgumentException("name");
     _coreQueue = coreQueue;
     _id = id;
     _name = name;
     _logger = logger;
     _writeDispatcher = writeDispatcher;
     _readDispatcher = readDispatcher;
     _inputQueue = inputQueue;
     _projectionStateHandlerFactory = projectionStateHandlerFactory;
     _getStateDispatcher =
         new RequestResponseDispatcher
             <CoreProjectionManagementMessage.GetState, CoreProjectionManagementMessage.StateReport>(
             coreQueue, v => v.CorrelationId, v => v.CorrelationId, new PublishEnvelope(_inputQueue));
 }
Exemple #13
0
        public ProjectionManager(IPublisher inputQueue, IPublisher publisher, IPublisher[] queues, ICheckpoint checkpointForStatistics)
        {
            if (inputQueue == null)
            {
                throw new ArgumentNullException("inputQueue");
            }
            if (publisher == null)
            {
                throw new ArgumentNullException("publisher");
            }
            if (queues == null)
            {
                throw new ArgumentNullException("queues");
            }
            if (queues.Length == 0)
            {
                throw new ArgumentException("queues");
            }

            _inputQueue = inputQueue;
            _publisher  = publisher;
            _checkpointForStatistics = checkpointForStatistics;
            _queues = queues;

            _writeDispatcher =
                new RequestResponseDispatcher <ClientMessage.WriteEvents, ClientMessage.WriteEventsCompleted>(
                    publisher, v => v.CorrelationId, v => v.CorrelationId, new PublishEnvelope(_inputQueue));
            _readDispatcher =
                new RequestResponseDispatcher
                <ClientMessage.ReadStreamEventsBackward, ClientMessage.ReadStreamEventsBackwardCompleted>(
                    publisher, v => v.CorrelationId, v => v.CorrelationId, new PublishEnvelope(_inputQueue));


            _projectionStateHandlerFactory = new ProjectionStateHandlerFactory();
            _projections    = new Dictionary <string, ManagedProjection>();
            _projectionsMap = new Dictionary <Guid, string>();
        }
        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);
        }
 public ManagedProjection(
     IPublisher coreQueue, Guid id, int projectionId, string name, bool enabledToRun, ILogger logger,
     RequestResponseDispatcher<ClientMessage.WriteEvents, ClientMessage.WriteEventsCompleted> writeDispatcher,
     RequestResponseDispatcher
         <ClientMessage.ReadStreamEventsBackward, ClientMessage.ReadStreamEventsBackwardCompleted> readDispatcher,
     IPublisher inputQueue, IPublisher output, ProjectionStateHandlerFactory projectionStateHandlerFactory,
     ITimeProvider timeProvider, ISingletonTimeoutScheduler timeoutScheduler = null, bool isSlave = false,
     IPublisher slaveResultsPublisher = null, Guid slaveMasterCorrelationId = default(Guid))
 {
     if (coreQueue == null) throw new ArgumentNullException("coreQueue");
     if (id == Guid.Empty) throw new ArgumentException("id");
     if (name == null) throw new ArgumentNullException("name");
     if (output == null) throw new ArgumentNullException("output");
     if (name == "") throw new ArgumentException("name");
     _coreQueue = coreQueue;
     _id = id;
     _projectionId = projectionId;
     _name = name;
     _enabledToRun = enabledToRun;
     _logger = logger;
     _writeDispatcher = writeDispatcher;
     _readDispatcher = readDispatcher;
     _inputQueue = inputQueue;
     _output = output;
     _projectionStateHandlerFactory = projectionStateHandlerFactory;
     _timeProvider = timeProvider;
     _timeoutScheduler = timeoutScheduler;
     _isSlave = isSlave;
     _slaveResultsPublisher = slaveResultsPublisher;
     _slaveMasterCorrelationId = slaveMasterCorrelationId;
     _getStateDispatcher =
         new RequestResponseDispatcher
             <CoreProjectionManagementMessage.GetState, CoreProjectionManagementMessage.StateReport>(
             coreQueue, v => v.CorrelationId, v => v.CorrelationId, new PublishEnvelope(_inputQueue));
     _getResultDispatcher =
         new RequestResponseDispatcher
             <CoreProjectionManagementMessage.GetResult, CoreProjectionManagementMessage.ResultReport>(
             coreQueue, v => v.CorrelationId, v => v.CorrelationId, new PublishEnvelope(_inputQueue));
     _lastAccessed = _timeProvider.Now;
 }
        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


            Func<IProjectionStateHandler> stateHandlerFactory = 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: s => _logger.Trace(s),
                        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;
                }
            };

            var createProjectionMessage = _isSlave ? 
                (Message) new CoreProjectionManagementMessage.CreateAndPrepareSlave(
                    new PublishEnvelope(_inputQueue), Id, _name, 
                    new ProjectionVersion(_projectionId, _persistedState.Epoch ?? 0, _persistedState.Version ?? 0),
                    config, _slaveResultsPublisher, _slaveMasterCorrelationId, stateHandlerFactory) :
                new CoreProjectionManagementMessage.CreateAndPrepare(
                    new PublishEnvelope(_inputQueue), Id, _name, 
                    new ProjectionVersion(_projectionId, _persistedState.Epoch ?? 0, _persistedState.Version ?? 0),
                    config, HandlerType, Query, stateHandlerFactory);

            //note: set runnign before start as coreProjection.start() can respond with faulted
            _state = ManagedProjectionState.Preparing;
            _coreQueue.Publish(createProjectionMessage);
        }
 public void Setup()
 {
     _stateHandlerFactory = new ProjectionStateHandlerFactory();
 }
Exemple #18
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


            Func <IProjectionStateHandler> stateHandlerFactory = 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: s => _logger.Trace(s),
                        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;
                }
            };

            var createProjectionMessage = _isSlave ?
                                          (Message) new CoreProjectionManagementMessage.CreateAndPrepareSlave(
                new PublishEnvelope(_inputQueue), Id, _name,
                new ProjectionVersion(_projectionId, _persistedState.Epoch ?? 0, _persistedState.Version ?? 0),
                config, _slaveResultsPublisher, _slaveMasterCorrelationId, stateHandlerFactory) :
                                          new CoreProjectionManagementMessage.CreateAndPrepare(
                new PublishEnvelope(_inputQueue), Id, _name,
                new ProjectionVersion(_projectionId, _persistedState.Epoch ?? 0, _persistedState.Version ?? 0),
                config, HandlerType, Query, stateHandlerFactory);

            //note: set runnign before start as coreProjection.start() can respond with faulted
            _state = ManagedProjectionState.Preparing;
            _coreQueue.Publish(createProjectionMessage);
        }
        private void Start(IPublisher coreOutput, ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config)
        {
            if (coreOutput == null) throw new ArgumentNullException("coreOutput");
            if (handlerFactory == null) throw new ArgumentNullException("handlerFactory");
            if (config == null) throw new ArgumentNullException("config");

            if (_coreProjection != null)
                throw new InvalidOperationException("CoreProjection has been already created");

            IProjectionStateHandler stateHandler = null;
            try
            {
                stateHandler = handlerFactory.Create(HandlerType, Query, Console.WriteLine);
                var checkpointStrategyBuilder = new CheckpointStrategy.Builder();
                stateHandler.ConfigureSourceProcessingStrategy(checkpointStrategyBuilder);
                checkpointStrategyBuilder.Validate(this.Mode); // avoid future exceptions in coreprojection
                // constructor can fail if wrong source defintion
                //TODO: revise it
                _coreProjection = new CoreProjection(_name, _id, coreOutput, stateHandler, config, _logger);
            }
            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();
                return;
            }

            //TODO: load configuration from the definition
            _state = ManagedProjectionState.Running;
            //note: set runnign before start as coreProjection.start() can respond with faulted
            _coreProjection.Start();
        }
Exemple #20
0
        private void Start(ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config)
        {
            if (handlerFactory == null) throw new ArgumentNullException("handlerFactory");
            if (config == null) throw new ArgumentNullException("config");

            if (_state == ManagedProjectionState.Running)
                throw new InvalidOperationException("Already started");

            //TODO: load configuration from the definition

            var createProjectionMessage = new ProjectionMessage.CoreService.Management.Create(new PublishEnvelope(_inputQueue), _id, _name, config, delegate
                {
                    IProjectionStateHandler stateHandler = null;
                    try
                    {
                        stateHandler = handlerFactory.Create(this.HandlerType, this.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.Running;
            _coreQueue.Publish(createProjectionMessage);
            _coreQueue.Publish(new ProjectionMessage.Projections.Management.Start(_id));
        }