| 1 |
import logging |
| 2 |
import sys |
| 3 |
from typing import Any |
| 4 |
|
| 5 |
from django.core.management.base import BaseCommand, CommandParser |
| 6 |
|
| 7 |
from ...cron import DjangoCronScheduler |
| 8 |
|
| 9 |
|
| 10 |
class Command(BaseCommand): |
| 11 |
""" |
| 12 |
Starts the RQ cron scheduler with Django-RQ integration. |
| 13 |
|
| 14 |
Example usage: |
| 15 |
python manage.py rqcron cron_config.py |
| 16 |
python manage.py rqcron myapp.cron_jobs --logging-level DEBUG |
| 17 |
""" |
| 18 |
|
| 19 |
help = "Starts the RQ cron scheduler" |
| 20 |
|
| 21 |
def add_arguments(self, parser: CommandParser) -> None: |
| 22 |
# Positional argument for config file/module |
| 23 |
parser.add_argument('config_path', help='Path to cron configuration file or module path') |
| 24 |
|
| 25 |
# Optional logging level |
| 26 |
parser.add_argument( |
| 27 |
'--logging-level', |
| 28 |
'-l', |
| 29 |
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 30 |
default='INFO', |
| 31 |
help='Set logging level (default: INFO)', |
| 32 |
) |
| 33 |
|
| 34 |
def handle(self, *args: Any, **options: Any) -> None: |
| 35 |
"""Main command handler.""" |
| 36 |
config_path: str = options['config_path'] |
| 37 |
logging_level: int = getattr(logging, options['logging_level']) |
| 38 |
|
| 39 |
# Create Django cron scheduler |
| 40 |
scheduler = DjangoCronScheduler(logging_level=logging_level) |
| 41 |
|
| 42 |
try: |
| 43 |
# Load configuration from file |
| 44 |
self.stdout.write(f"Loading cron configuration from {config_path}") |
| 45 |
scheduler.load_config_from_file(config_path) |
| 46 |
|
| 47 |
# Start the scheduler |
| 48 |
job_count = len(scheduler.get_jobs()) |
| 49 |
self.stdout.write(self.style.SUCCESS(f'Starting cron scheduler with {job_count} jobs...')) |
| 50 |
|
| 51 |
scheduler.start() |
| 52 |
|
| 53 |
except KeyboardInterrupt: |
| 54 |
self.stdout.write('\nShutting down cron scheduler...') |
| 55 |
sys.exit(0) |