def start_app(self): # Make up a DB URI using a named temporary file. self.tempfile = NamedTemporaryFile() uri = 'sqlite:///{}'.format(self.tempfile.name) from eventsourcing.example.interface.flaskapp import IntegerSequencedItem # Close application, importing the module constructed # the application, which will leave event handlers subscribed. close_example_application() # Setup tables. datastore = SQLAlchemyDatastore( settings=SQLAlchemySettings(uri=uri), tables=(IntegerSequencedItem,), ) datastore.setup_connection() datastore.setup_tables() datastore.close_connection() # Run uwsgi. path_to_uwsgi = join(path_to_virtualenv, 'bin', 'uwsgi') assert os.path.exists(path_to_uwsgi), path_to_uwsgi cmd = [path_to_uwsgi] if path_to_virtualenv is not None: cmd += ['-H', path_to_virtualenv] cmd += ['--master'] cmd += ['--processes', '4'] cmd += ['--threads', '2'] cmd += ['--wsgi-file', path_to_flaskwsgi] cmd += ['--http', ':{}'.format(self.port)] pythonpath = ':'.join(os.getenv('PYTHONPATH', '').split(':') + [path_to_eventsourcing]) return Popen(cmd, env={ 'PYTHONPATH': pythonpath, 'DB_URI': uri })
def start_app(self): # Make up a DB URI using a named temporary file. self.tempfile = NamedTemporaryFile() uri = "sqlite:///{}".format(self.tempfile.name) from eventsourcing.example.interface.flaskapp import IntegerSequencedItem # Close application, importing the module constructed # the application, which will leave event handlers subscribed. close_example_application() # Setup tables. datastore = SQLAlchemyDatastore(settings=SQLAlchemySettings(uri=uri), tables=(IntegerSequencedItem, )) datastore.setup_connection() datastore.setup_tables() datastore.close_connection() # Run uwsgi. path_to_uwsgi = shutil.which("uwsgi") if not os.path.exists(path_to_uwsgi): raise AssertionError("Can't find uwsgi: %s" % path_to_uwsgi) cmd = [path_to_uwsgi] if path_to_virtualenv is not None: cmd += ["-H", path_to_virtualenv] cmd += ["--master"] cmd += ["--processes", "4"] cmd += ["--threads", "2"] cmd += ["--wsgi-file", path_to_flaskapp] cmd += ["--http", ":{}".format(self.port)] pythonpath = ":".join( os.getenv("PYTHONPATH", "").split(":") + [path_to_eventsourcing]) return Popen(cmd, env={"PYTHONPATH": pythonpath, "DB_URI": uri})
def start_app(self): # Make up a DB URI using a named temporary file. self.tempfile = NamedTemporaryFile() uri = "sqlite:///{}".format(self.tempfile.name) from eventsourcing.example.interface.flaskapp import IntegerSequencedItem # Close application, importing the module constructed # the application, which will leave event handlers subscribed. close_example_application() # Setup tables. datastore = SQLAlchemyDatastore(settings=SQLAlchemySettings(uri=uri), tables=(IntegerSequencedItem, )) datastore.setup_connection() datastore.setup_tables() datastore.close_connection() # Run uwsgi. if path_to_virtualenv: path_to_uwsgi = join(path_to_virtualenv, "bin", "uwsgi") assert os.path.exists(path_to_uwsgi), path_to_uwsgi else: # In a container, without a virtualenv? path_to_uwsgi = "/usr/local/bin/uwsgi" # Todo: Maybe use shutil.which, after dropping support for Python 2.7. cmd = [path_to_uwsgi] if path_to_virtualenv is not None: cmd += ["-H", path_to_virtualenv] cmd += ["--master"] cmd += ["--processes", "4"] cmd += ["--threads", "2"] cmd += ["--wsgi-file", path_to_flaskwsgi] cmd += ["--http", ":{}".format(self.port)] pythonpath = ":".join( os.getenv("PYTHONPATH", "").split(":") + [path_to_eventsourcing]) return Popen(cmd, env={"PYTHONPATH": pythonpath, "DB_URI": uri})
class SimpleApplication(object): persist_event_type = None def __init__(self, name='', persistence_policy=None, persist_event_type=None, uri=None, pool_size=5, session=None, cipher_key=None, sequenced_item_class=None, stored_event_record_class=None, setup_table=True, contiguous_record_ids=True, pipeline_id=-1, notification_log_section_size=None): self.notification_log_section_size = notification_log_section_size self.name = name or type(self).__name__.lower() # Setup cipher (optional). self.setup_cipher(cipher_key) # Setup connection to database. self.setup_datastore(session, uri, pool_size) # Setup the event store. self.sequenced_item_class = sequenced_item_class self.stored_event_record_class = stored_event_record_class self.contiguous_record_ids = contiguous_record_ids self.application_id = uuid_from_application_name(self.name) self.pipeline_id = pipeline_id self.setup_event_store() # Setup notifications. self.notification_log = RecordManagerNotificationLog( self.event_store.record_manager, section_size=self.notification_log_section_size) # Setup an event sourced repository. self.setup_repository() # Setup a persistence policy. self.persistence_policy = persistence_policy if self.persistence_policy is None: self.setup_persistence_policy(persist_event_type or type(self).persist_event_type) # Setup table in database. if setup_table and not session: self.setup_table() def change_pipeline(self, pipeline_id): self.pipeline_id = pipeline_id self.event_store.record_manager.pipeline_id = pipeline_id @property def session(self): return self.datastore.session def setup_cipher(self, cipher_key): cipher_key = decode_random_bytes(cipher_key or os.getenv('CIPHER_KEY', '')) self.cipher = AESCipher(cipher_key) if cipher_key else None def setup_datastore(self, session, uri, pool_size=5): self.datastore = SQLAlchemyDatastore( settings=SQLAlchemySettings(uri=uri, pool_size=pool_size), session=session, ) def setup_event_store(self): # Construct event store. self.event_store = self.construct_event_store(self.application_id, self.pipeline_id) def construct_event_store(self, application_id, pipeline_id): return construct_sqlalchemy_eventstore( sequenced_item_class=self.sequenced_item_class, session=self.datastore.session, cipher=self.cipher, record_class=self.stored_event_record_class, contiguous_record_ids=self.contiguous_record_ids, application_id=application_id, pipeline_id=pipeline_id, ) def setup_repository(self, **kwargs): event_store = self.event_store self.repository = self.construct_repository(event_store, **kwargs) def construct_repository(self, event_store, **kwargs): return EventSourcedRepository(event_store=event_store, **kwargs) def setup_persistence_policy(self, persist_event_type): self.persistence_policy = PersistencePolicy( event_store=self.event_store, event_type=persist_event_type) def setup_table(self): # Setup the database table using event store's record class. self.datastore.setup_table( self.event_store.record_manager.record_class) def drop_table(self): # Setup the database table using event store's record class. self.datastore.drop_table(self.event_store.record_manager.record_class) def close(self): # Close the persistence policy. if self.persistence_policy: self.persistence_policy.close() # Close database connection. self.datastore.close_connection() def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.close()
class SimpleApplication(object): def __init__(self, persist_event_type=None, uri=None, session=None, cipher_key=None, stored_event_record_class=None, setup_table=True, contiguous_record_ids=True): # Setup cipher (optional). self.setup_cipher(cipher_key) # Setup connection to database. self.setup_datastore(session, uri) # Setup the event store. self.stored_event_record_class = stored_event_record_class self.contiguous_record_ids = contiguous_record_ids self.setup_event_store() # Setup notifications. self.notification_log = RecordManagerNotificationLog( self.event_store.record_manager, section_size=20, ) # Setup an event sourced repository. self.setup_repository() # Setup a persistence policy. self.setup_persistence_policy(persist_event_type) # Setup table in database. if setup_table: self.setup_table() def setup_cipher(self, cipher_key): cipher_key = decode_random_bytes(cipher_key or os.getenv('CIPHER_KEY', '')) self.cipher = AESCipher(cipher_key) if cipher_key else None def setup_datastore(self, session, uri): self.datastore = SQLAlchemyDatastore( settings=SQLAlchemySettings(uri=uri), session=session, ) def setup_event_store(self): # Construct event store. self.event_store = construct_sqlalchemy_eventstore( session=self.datastore.session, cipher=self.cipher, record_class=self.stored_event_record_class, contiguous_record_ids=self.contiguous_record_ids, ) def setup_repository(self, **kwargs): self.repository = EventSourcedRepository( event_store=self.event_store, **kwargs ) def setup_persistence_policy(self, persist_event_type): self.persistence_policy = PersistencePolicy( event_store=self.event_store, event_type=persist_event_type ) def setup_table(self): # Setup the database table using event store's record class. self.datastore.setup_table( self.event_store.record_manager.record_class ) def drop_table(self): # Setup the database table using event store's record class. self.datastore.drop_table( self.event_store.record_manager.record_class ) def close(self): # Close the persistence policy. self.persistence_policy.close() # Close database connection. self.datastore.close_connection() def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.close()