Scheduler¶
Ravyn internally uses Asyncz to perform its tasks if need and the main object used is the
RavynScheduler.
Can you use this externally? Very unlikely. This serves only for the purpose of explaining how and what the object does as internally, by the time of this writting, Ravyn does not allow to pass specific schedulers, instead, uses this one out of the box.
RavynScheduler¶
The object that is instantiated when an Ravyn application is created and the scheduler is enabled.
Ravyn also defaults the scheduler_class to the
Asyncz AsyncIOScheduler if nothing is provided.
Warning
By the time this document was written, Ravyn was in version 0.5.0 and it did not allow
passing different RavynScheduler instance on instantiation time but that might change
in the future. In other words, this is just an explanation of how does the object work
internally.
Parameters¶
- app - Ravyn instance.
- scheduler_class - An instance of a scheduler.
- tasks - A dictinary str, str mapping the tasks of the application to be executed.
- timezone - The timezone instance.
- configurations - A dictionary with extra configurations to be passed to the scheduler.
Example¶
This is how Ravyn usually works. Let us assume:
- You have some tasks living inside a file
src/accounts/tasks.py. - You want to pass some extra configurations to the scheduler.
from loguru import logger
from asyncz.schedulers import AsyncIOScheduler
from asyncz.triggers import IntervalTrigger
from ravyn.contrib.schedulers.asyncz.config import AsynczConfig
from ravyn.contrib.schedulers.asyncz.decorator import scheduler
from ravyn import Ravyn
# Run every 2 minutes
@scheduler(trigger=IntervalTrigger(minutes=2))
def send_message() -> None:
logger.info("Message sent after 2 minutes")
# Run every 20 minutes
@scheduler(trigger=IntervalTrigger(minutes=20))
def send_longer_message() -> None:
logger.info("Message sent after 20 minutes")
# Create the extra configurations
configurations = (
{
"asyncz.stores.mongo": {"type": "mongodb"},
"asyncz.stores.default": {"type": "redis", "database": "0"},
"asyncz.executors.pool": {
"max_workers": "20",
"class": "asyncz.executors.pool:ThreadPoolExecutor",
},
"asyncz.executors.default": {"class": "asyncz.executors.asyncio:AsyncIOExecutor"},
"asyncz.task_defaults.coalesce": "false",
"asyncz.task_defaults.max_instances": "3",
"asyncz.task_defaults.timezone": "UTC",
},
)
# Start Ravyn with the scheduler
# Pass the configurations
# Enable the scheduler
# AsyncIOScheduler Is the default scheduler of Ravyn but
# we pass here for example purposes
app = Ravyn(
scheduler_config=AsynczConfig(
scheduler_class=AsyncIOScheduler,
configurations=configurations,
tasks={
"send_message": "src.accounts.tasks",
"send_longer_message": "src.accounts.tasks",
},
),
enable_scheduler=True,
)
As stated above, this is just an illustration on how Ravyn operates with Asyncz (from the version 0.5.0 onwards) and shows how you can use your usual configurations of Asyncz directly with the framework
Functionalities¶
RavynScheduler does the heavy lifting for you.
- Registers the tasks in the Asyncz scheduler based on the
tasksparameters. - Registers the Ravyn events
startupandshutdownautomatically on app instantiation.