Search code examples
pythonmicroservicesminosperiodic

How to add a periodic task to a Service in minos?


I want to add a method to my minos.cqrs.Service to be executed every day at 9:00 AM. How can I do that?

Here is my current code:

from minos.cqrs import Service


class MyService(Service):

    async def task(self) -> None:
        print("Running periodic task...")

Solution

  • To add a periodic task to a minos.cqrs.Service class, you can create a standard handling method and decorate it with the @enroute.periodic.event decorator from minos.networks and pass it a valid cron expression as argument (0 9 * * * in your case).

    Here is an example:

    from minos.cqrs import Service
    from minos.networks import Request, enroute
    
    
    class MyService(Service):
    
        @enroute.periodic.event("0 9 * * *")
        async def task(self, request: Request) -> None:
            print("Running periodic task...")