Exemplo n.º 1
0
    async def start(self, ctx: Context):
        if isinstance(self.commit_executor, str):
            self.commit_executor = await ctx.request_resource(Executor, self.commit_executor)
        elif self.commit_executor is None:
            self.commit_executor = ThreadPoolExecutor(self.commit_executor_workers)
            ctx.add_teardown_callback(self.commit_executor.shutdown)

        for resource_name, context_attr, bind, factory, ready_callback in self.session_factories:
            if ready_callback:
                retval = ready_callback(bind, factory)
                if isawaitable(retval):
                    await retval

            engine = bind if isinstance(bind, Engine) else bind.engine
            ctx.add_resource(engine, resource_name)
            ctx.add_resource(factory, resource_name)
            ctx.add_resource_factory(partial(self.create_session, factory=factory),
                                     [Session], resource_name, context_attr)
            logger.info('Configured SQLAlchemy session maker (%s / ctx.%s; dialect=%s)',
                        resource_name, context_attr, bind.dialect.name)

        await yield_()

        for resource_name, context_attr, bind, factory, ready_callback in self.session_factories:
            if isinstance(bind, Engine):
                bind.dispose()

            logger.info('SQLAlchemy session maker (%s / ctx.%s) shut down', resource_name,
                        context_attr)
Exemplo n.º 2
0
    async def start(self, ctx: Context):
        if isinstance(self.tls_context, str):
            self.tls_context = await ctx.request_resource(
                SSLContext, self.tls_context)

        self._smtp = SMTP(hostname=self.host,
                          port=self.port,
                          tls_context=self.tls_context,
                          loop=ctx.loop,
                          timeout=self.timeout)
        ctx.add_teardown_callback(self._smtp.close)
Exemplo n.º 3
0
    def create_session(self, ctx: Context, factory: sessionmaker) -> Session:
        async def teardown_session(exception: Optional[BaseException]) -> None:
            try:
                if exception is None and session.is_active:
                    await call_in_executor(session.commit, executor=self.commit_executor)
            finally:
                del session.info['ctx']
                session.close()

        session = factory(info={'ctx': ctx})
        ctx.add_teardown_callback(teardown_session, pass_exception=True)
        return session
Exemplo n.º 4
0
    async def start(self, ctx: Context) -> None:
        """Resolve Asphalt resource references."""
        if self._session is None:

            def close_session():
                self._session.close()

            self._session = ClientSession()
            ctx.add_teardown_callback(close_session)
        elif isinstance(self._session, str):
            self._session = await ctx.request_resource(ClientSession,
                                                       self._session)
Exemplo n.º 5
0
    def create_async_session(self, ctx: Context) -> AsyncSession:
        async def teardown_session(exception: BaseException | None) -> None:
            try:
                if session.in_transaction():
                    if exception is None:
                        await session.commit()
                    else:
                        await session.rollback()
            finally:
                await session.close()

        session: AsyncSession = self.sessionmaker()
        ctx.add_teardown_callback(teardown_session, pass_exception=True)
        return session
Exemplo n.º 6
0
    async def start(self, ctx: Context) -> None:
        if isinstance(self.store, str):
            self.store = await ctx.request_resource(FeedStateStore, self.store)

        if isinstance(self.session, str):
            self.session = await ctx.request_resource(ClientSession,
                                                      self.session)
        elif self.session is None:
            self.session = ClientSession()
            ctx.add_teardown_callback(self.session.close)

        if self.store is not None:
            state = await self.store.load_state(self.state_id)
            self.__setstate__(state)

        if self.interval:
            loop_task = ctx.loop.create_task(self.loop_update())
            ctx.add_teardown_callback(loop_task.cancel)
Exemplo n.º 7
0
    def create_session(self, ctx: Context) -> Session:
        async def teardown_session(exception: BaseException | None) -> None:
            try:
                if session.in_transaction():
                    context = copy_context()
                    if exception is None:
                        await get_running_loop().run_in_executor(
                            self.commit_executor, context.run, session.commit)
                    else:
                        await get_running_loop().run_in_executor(
                            self.commit_executor, context.run,
                            session.rollback)
            finally:
                session.close()

        session = self.sessionmaker()
        ctx.add_teardown_callback(teardown_session, pass_exception=True)
        return session
Exemplo n.º 8
0
    async def start(self,
                    ctx: Context) -> AsyncGenerator[None, Exception | None]:
        if self.ready_callback:
            retval = self.ready_callback(self.bind, self.sessionmaker)
            if isawaitable(retval):
                await retval

        ctx.add_resource(self.engine, self.resource_name)
        ctx.add_resource(self.sessionmaker, self.resource_name)
        if isinstance(self.engine, AsyncEngine):
            ctx.add_resource_factory(
                self.create_async_session,
                [AsyncSession],
                self.resource_name,
            )
        else:
            self.commit_executor = ThreadPoolExecutor(
                self.commit_executor_workers)
            ctx.add_teardown_callback(self.commit_executor.shutdown)

            ctx.add_resource_factory(
                self.create_session,
                [Session],
                self.resource_name,
            )

        logger.info(
            "Configured SQLAlchemy resources (%s; dialect=%s, driver=%s)",
            self.resource_name,
            self.bind.dialect.name,
            self.bind.dialect.driver,
        )

        yield

        if isinstance(self.bind, Engine):
            self.bind.dispose()
        elif isinstance(self.bind, AsyncEngine):
            await self.bind.dispose()

        logger.info("SQLAlchemy resources (%s) shut down", self.resource_name)