|
Cat Feeder
1.0.0
The Cat feeder project
|
The crons module provides background task scheduling and management capabilities using APScheduler. It consists of two main classes that work together to schedule and execute recurring tasks in the backend server.
Location: backend/src/libs/crons/
Key Components:
BackgroundTasks - Core scheduler wrapper for APSchedulerCrons - High-level task manager with predefined maintenance jobscrons_constants.py - Configuration constants loaded from TOMLSee crons_architecture.puml for visual representation.
A final class that wraps APScheduler's BackgroundScheduler to provide safe, non-crashing task scheduling.
Purpose: Provides a robust interface for scheduling background tasks with error handling and graceful degradation.
Key Features:
safe_* methodsConstructor:
Key Methods:
| Method | Description |
|---|---|
safe_add_task() | Add a task without crashing on errors |
safe_start() | Start the scheduler safely |
safe_pause() | Pause the scheduler |
safe_resume() | Resume a paused scheduler |
safe_stop() | Stop the scheduler and optionally wait for tasks |
add_task() | Direct task addition (can raise exceptions) |
Task Configuration:
A final class that manages predefined background tasks for server maintenance.
Purpose: Orchestrates scheduled maintenance tasks like token cleanup, verification cleanup, and OAuth token renewal.
Key Features:
Constructor:
Predefined Tasks:
| Task | Description | Enabled By | Interval Constant |
|---|---|---|---|
check_actions() | General action checking | Always | CHECK_ACTIONS_INTERVAL |
clean_expired_tokens() | Remove expired auth tokens | CLEAN_TOKENS | CLEAN_TOKENS_INTERVAL |
clean_expired_verification_nodes() | Remove expired verification entries | CLEAN_VERIFICATION | CLEAN_VERIFICATION_INTERVAL |
renew_oaths() | Renew OAuth tokens | RENEW_OATH_TOKENS | RENEW_OATH_TOKENS_INTERVAL |
_test_hello_world() | Test cron (debug) | ENABLE_TEST_CRONS | TEST_CRONS_INTERVAL |
_test_current_date() | Test cron with date (debug) | ENABLE_TEST_CRONS | TEST_CRONS_INTERVAL |
Configuration is loaded from config.toml using the _get_toml_variable() function.
Key Constants:
The module leverages APScheduler's BackgroundScheduler:
Trigger Types:
interval - Execute at fixed time intervals (most common)cron - Execute using cron-like scheduling expressionsdate - Execute once at a specific datetimeJob Management:
scheduler.add_job()Job instance for monitoring/manipulationSafe Methods Pattern: All critical operations have safe_* variants that catch exceptions and return status codes:
Exception Handling:
ValueError - Invalid function, trigger, or parametersRuntimeError - Scheduler state issuesSchedulerAlreadyRunningError - Attempting to start running schedulerSchedulerNotRunningError - Attempting to stop/pause inactive schedulerInitialization:
BackgroundTasks creates BackgroundScheduler instanceCrons retrieves/creates BackgroundTasks from RuntimeManagerinject_crons() adds all enabled tasksExecution:
SQL instancedisplay_tty.DispShutdown:
Crons.__del__() called on shutdownBackgroundTasks.__del__() calls safe_stop()wait=True)safe_* methods in production codeENABLE_TEST_CRONS before deploying new tasksapscheduler - Background scheduling enginedisplay_tty - Logging functionalitybackend.src.libs.core - FinalClass, RuntimeManagerbackend.src.libs.sql - Database operationsbackend.src.libs.utils - OAuth and server utilitiesbackend.src.libs.boilerplates - Non-HTTP boilerplate operations