Skip to content

Decorator

The decorator is a wrapper provided by Asyncz to Esmerald that allows you to declare tasks anywhere and import them into the EsmeraldScheduler upont instantiation.

In the end, the scheduler is a wrapper on te top of the Task object but not the same as the task decorator. The latter is to be used outside of the Esmerald context.

Parameters

  • name - Textual description of the task.

    Default: None

  • trigger - An instance of a trigger class.

    Default: None

  • id - Explicit identifier for the task.

    Default: None

  • mistrigger_grace_time - Seconds after the designated runtime that the task is still allowed to be run (or None to allow the task to run no matter how late it is).

    Default: None

  • coalesce - Run once instead of many times if the scheduler determines that the task should be run more than once in succession.

    Default: None

  • max_instances - Maximum number of concurrently running instances allowed for this task.

    Default: None

  • next_run_time - When to first run the task, regardless of the trigger (pass None to add the task as paused).

    Default: None

  • store - Alias of the task store to store the task in.

    Default: default

  • executor - Alias of the executor to run the task with.

    Default: default

  • replace_existing - True to replace an existing task with the same id (but retain the number of runs from the existing one).

    Default: None

  • args - List of positional arguments to call func with.

    Default: None

  • kwargs - Dict of keyword arguments to call func with.

    Default: None

  • is_enabled - True if the the task to be added to the scheduler.

    Default: None

Examples

Using the decorator is quite straightforward and clear. You can place a task anywhere in your application, declare the decorator on the top of the task and then import it inside your Esmerald instance.

Let us assume:

  • You have some tasks living inside a file src/accounts/tasks.py.
  • You want to import them inside your Esmerald application.
from esmerald import Esmerald
from loguru import logger

from esmerald.contrib.schedulers.asyncz.decorator import scheduler
from asyncz.triggers import CronTrigger, IntervalTrigger


# Run every 2 minutes
@scheduler(trigger=IntervalTrigger(minutes=2))
def send_message():
    logger.info("Message sent after 2 minutes")


# Run every 20 minutes
@scheduler(trigger=IntervalTrigger(minutes=20))
def send_longer_message():
    logger.info("Message sent after 20 minutes")


# Run every mon,wed,fri
@scheduler(trigger=CronTrigger(day_of_week="mon,wed,fri", hour=1, minute=2))
def send_cron_message():
    logger.info("Message sent every Monday, Wednesday and Friday")


# Start Esmerald with the scheduler
# Enable the scheduler
# we pass here for example purposes
app = Esmerald(
    scheduler_tasks={
        "send_message": "src.accounts.tasks",
        "send_longer_message": "src.accounts.tasks",
        "send_cron_message": "src.accounts.tasks",
    },
    enable_scheduler=True,
)