Events¶
Asyncz emits structured event objects when schedulers, stores, executors, and tasks change state. All event models live in asyncz.events.base, and the event code constants live in asyncz.events.constants.
Event models¶
SchedulerEvent¶
The base event type. It carries:
code: the event codealias: the store or executor alias involved in the event, when relevant
TaskEvent¶
Extends SchedulerEvent with task-specific data:
task_idstore
TaskSubmissionEvent¶
Extends TaskEvent with:
scheduled_run_times: the run times the scheduler submitted to an executor
TaskExecutionEvent¶
Extends TaskEvent with:
scheduled_run_timereturn_valueexceptiontraceback
Common event codes¶
Import codes from asyncz.events.constants and register listeners with scheduler.add_listener(...).
Typical codes include:
SCHEDULER_STARTSCHEDULER_SHUTDOWNTASK_ADDEDTASK_REMOVEDTASK_SUBMITTEDTASK_EXECUTEDTASK_ERRORTASK_MISSED
Adding a listener¶
from datetime import timezone as tz
import logging
from asyncz.events.constants import TASK_ADDED, TASK_REMOVED
from asyncz.schedulers.asyncio import AsyncIOScheduler
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create the scheduler
scheduler = AsyncIOScheduler(timezone=tz.utc)
def my_custom_listener(event):
if not event.exception:
logger.info("All good")
else:
logger.exception("Problem with the task")
# Add event listener
scheduler.add_listener(my_custom_listener, TASK_ADDED | TASK_REMOVED)