Exemplo n.º 1
0
    async def register_timer(self,
                             name: Optional[str],
                             callback: TIMER_CALLBACK,
                             state: Any,
                             due_time: timedelta,
                             period: timedelta,
                             ttl: Optional[timedelta] = None) -> None:
        """Registers actor timer.

        All timers are stopped when the actor is deactivated as part of garbage collection.

        Args:
            name (str): the name of the timer to register.
            callback (Callable): An awaitable callable which will be called when the timer fires.
            state (Any): An object which will pass to the callback method, or None.
            due_time (datetime.timedelta): the amount of time to delay before the awaitable
                callback is first invoked.
            period (datetime.timedelta): the time interval between invocations
                of the awaitable callback.
            ttl (Optional[datetime.timedelta]): the time interval before the timer stops firing
        """
        name = name or self.__get_new_timer_name()
        timer = ActorTimerData(name, callback, state, due_time, period, ttl)

        req_body = self._runtime_ctx.message_serializer.serialize(
            timer.as_dict())
        await self._runtime_ctx.dapr_client.register_timer(
            self._runtime_ctx.actor_type_info.type_name, self.id.id, name,
            req_body)
Exemplo n.º 2
0
    def test_timer_data(self):
        def test_callback(obj):
            self.assertEqual('called', obj)

        timer = ActorTimerData('timer_name', test_callback, 'called',
                               timedelta(seconds=1), timedelta(seconds=1))
        self.assertEqual(test_callback, timer.callback)
        timer.callback('called')
Exemplo n.º 3
0
 def test_timer_data(self):
     def my_callback(input: Any):
         print(input)
     timer = ActorTimerData(
         'timer_name', my_callback, 'called',
         timedelta(seconds=2), timedelta(seconds=1))
     self.assertEqual('timer_name', timer.timer_name)
     self.assertEqual('my_callback', timer.callback)
     self.assertEqual('called', timer.state)
     self.assertEqual(timedelta(seconds=2), timer.due_time)
     self.assertEqual(timedelta(seconds=1), timer.period)
Exemplo n.º 4
0
    def test_as_dict(self):
        def test_callback(obj):
            self.assertEqual('called', obj)

        timer = ActorTimerData('timer_name', test_callback, 'called',
                               timedelta(seconds=1), timedelta(seconds=1))
        expected = {
            'dueTime': timedelta(seconds=1),
            'period': timedelta(seconds=1),
        }
        self.assertDictEqual(expected, timer.as_dict())
Exemplo n.º 5
0
 def test_as_dict(self):
     def my_callback(input: Any):
         print(input)
     timer = ActorTimerData(
         'timer_name', my_callback, 'called',
         timedelta(seconds=1), timedelta(seconds=1))
     expected = {
         'callback': 'my_callback',
         'data': 'called',
         'dueTime': timedelta(seconds=1),
         'period': timedelta(seconds=1),
     }
     self.assertDictEqual(expected, timer.as_dict())