How to do some setup before starting any entrypoint


#1

From @xqliang on Thu Oct 12 2017 07:22:45 GMT+0000 (UTC)

I want to do some process setup before starting any entrypoint, such as loading data from database to local memory via nameko-sqlalchemy DependencyProvider.

But the nameko-sqlalchemy is a DependencyProvider, and I can’t create a new DependencyProvider rely on it.

Can you introducing an initialization class method, which will be execute after all DependencyProvider started and before starting any EntryPoint?

Copied from original issue: https://github.com/nameko/nameko/issues/482


#2

From @mattbennett on Thu Oct 12 2017 21:31:25 GMT+0000 (UTC)

DependencyProvider.setup or DependencyProvider.start is the place to do this. You can subclass the nameko-sqlalchemy DependencyProvider to implement or override these methods:

from nameko_sqlalchemy import DatabaseSession

class MemoryLoadingDatabaseSession(DatabaseSession):
    def start(self):
        session_cls = sessionmaker(bind=self.engine)
        session = session_cls()
        ...  # load data into memory
        session.close()