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); }
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); }