Esempio n. 1
0
    async def _connect(self, max_attempts: Optional[int] = None) -> None:
        with wrap2span(
                name=AmqpSpan.NAME_PREPARE,
                kind=AmqpSpan.KIND_CLIENT,
                cls=AmqpSpan,
                app=self.app,
        ):
            attempt: int = 0
            while max_attempts is None or attempt < max_attempts:
                attempt += 1
                try:
                    self._conn = _Connection(self, self.cfg)
                    self.app.log_info("Connecting to %s", self._masked_url)
                    await self._conn.connect(self._on_disconnect)
                    self.app.log_info("Connected to %s", self._masked_url)
                    break
                except Exception as err:
                    self.app.log_err(err)
                    if self._conn is not None:
                        try:
                            await self._conn.close()
                        except Exception:  # nosec
                            pass
                    await asyncio.sleep(self.cfg.connect_retry_delay)

            if max_attempts is not None and attempt >= max_attempts:
                raise PrepareError("Could not connect to %s" %
                                   self._masked_url)

            await self._open_channels()

            if self._started:
                await asyncio.gather(*[ch.start() for ch in self._channels])
Esempio n. 2
0
    async def prepare(self) -> None:
        if self.app is None:  # pragma: no cover
            raise UserWarning('Unattached component')

        for i in range(self.cfg.connect_max_attempts):
            try:
                await self._connect()
                return
            except Exception as e:
                self.app.log_err(str(e))
                await asyncio.sleep(self.cfg.connect_retry_delay)
        raise PrepareError("Could not connect to %s" % self._masked_url)
Esempio n. 3
0
 async def _connect(self) -> None:
     for i in range(self.cfg.connect_max_attempts):
         app.log_info("Connecting to %s", masked_url(self.cfg.url))
         try:
             self.pg = await asyncpg.connect(self.cfg.url)
             self._conn_lock = asyncio.Lock()
             app.log_info("Connected to %s", masked_url(self.cfg.url))
             return
         except Exception as e:
             app.log_err(str(e))
             await asyncio.sleep(self.cfg.connect_retry_delay)
     raise PrepareError("Could not connect to %s" %
                        masked_url(self.cfg.url))
Esempio n. 4
0
    async def prepare(self) -> None:
        if self.app is None:  # pragma: no cover
            raise UserWarning('Unattached component')

        for i in range(self.cfg.connect_max_attempts):
            try:
                self.app.log_info("Connecting to %s", self.cfg.dsn)
                with wrap2span(
                        name=OraSpan.NAME_CONNECT,
                        kind=OraSpan.KIND_CLIENT,
                        app=self.app,
                ):
                    await self.app.loop.run_in_executor(None, self._connect)
                self.app.log_info("Connected to %s", self.cfg.dsn)
                return
            except Exception as e:
                self.app.log_err(str(e))
                await asyncio.sleep(self.cfg.connect_retry_delay)
        raise PrepareError("Could not connect to %s" % self.cfg.dsn)
Esempio n. 5
0
 async def init(self) -> None:
     try:
         await self.get_conn(can_reconnect=True)
     except Exception as err:
         raise PrepareError(str(err))