async def run_forever(self) -> NoReturn:
        """Run the periodic function forever. This method is equivalent to start, but it keeps waiting until infinite.

        :return: This method never returns.
        """
        now = current_datetime()
        await asyncio.sleep(self._crontab.next(now))

        while True:
            now = current_datetime()
            await asyncio.gather(asyncio.sleep(self._crontab.next(now)),
                                 self.run_once(now))
Exemple #2
0
    def __init__(
        self,
        id: int,
        topic: str,
        partition: int,
        data_bytes: bytes,
        retry: int = 0,
        created_at: Optional[datetime] = None,
        updated_at: Optional[datetime] = None,
        data_cls: Type[Model] = Model,
        callback_lookup: Optional[Callable] = None,
        exception: Optional[Exception] = None,
    ):
        if created_at is None or updated_at is None:
            now = current_datetime()
            if created_at is None:
                created_at = now
            if updated_at is None:
                updated_at = now

        self.id = id
        self.topic = topic
        self.partition = partition
        self.data_bytes = data_bytes
        self.data_cls = data_cls
        self.retry = retry
        self.created_at = created_at
        self.updated_at = updated_at
        self.callback_lookup = callback_lookup
        self.exception = exception
    async def test_run_once(self) -> None:
        now = current_datetime()
        await self.periodic.run_once(now)
        self.assertEqual(1, self.fn_mock.call_count)

        observed = self.fn_mock.call_args.args[0]
        self.assertIsInstance(observed, ScheduledRequest)
        self.assertEqual(now, (await observed.content()).scheduled_at)
Exemple #4
0
 def test_eq(self):
     data_bytes = self.message.avro_bytes
     now = current_datetime()
     one = BrokerHandlerEntry(1,
                              "AddOrder",
                              0,
                              data_bytes,
                              1,
                              created_at=now,
                              updated_at=now)
     two = BrokerHandlerEntry(1,
                              "AddOrder",
                              0,
                              data_bytes,
                              1,
                              created_at=now,
                              updated_at=now)
     self.assertEqual(one, two)
Exemple #5
0
    def test_constructor_extended(self):
        data_bytes = self.message.avro_bytes
        now = current_datetime()

        entry = BrokerHandlerEntry(1,
                                   "AddOrder",
                                   0,
                                   data_bytes,
                                   1,
                                   created_at=now,
                                   updated_at=now)
        self.assertEqual(1, entry.id)
        self.assertEqual("AddOrder", entry.topic)
        self.assertEqual(0, entry.partition)
        self.assertEqual(data_bytes, entry.data_bytes)
        self.assertEqual(1, entry.retry)
        self.assertEqual(now, entry.created_at)
        self.assertEqual(now, entry.updated_at)
    async def run_once(self, now: Optional[datetime] = None) -> None:
        """Run the periodic function one time.

        :param now: An optional datetime expressing the current datetime.
        :return: This method does not return anything.
        """
        if now is None:
            now = current_datetime()

        request = ScheduledRequest(now)
        logger.debug("Running periodic task...")
        try:
            self._running = True
            with suppress(asyncio.CancelledError):
                response = self._fn(request)
                if isawaitable(response):
                    await response
        except ResponseException as exc:
            logger.warning(f"Raised an application exception: {exc!s}")
        except Exception as exc:
            logger.exception(f"Raised a system exception: {exc!r}")
        finally:
            self._running = False
Exemple #7
0
 def test_scheduled_at(self):
     now = current_datetime()
     content = ScheduledRequestContent(now)
     self.assertEqual(now, content.scheduled_at)
Exemple #8
0
 def test_eq(self):
     self.assertEqual(self.request, ScheduledRequest(self.now))
     self.assertNotEqual(self.request, ScheduledRequest(current_datetime()))
Exemple #9
0
 def setUp(self) -> None:
     self.now = current_datetime()
     self.request = ScheduledRequest(self.now)