This commit is contained in:
anhduy-tech
2026-02-09 16:56:55 +07:00
parent d6eec950e9
commit c2efa46260
37 changed files with 199 additions and 121 deletions

View File

@@ -35,7 +35,6 @@ def scan_and_run_due_jobs():
if uninitialized_jobs.exists():
#logger.info(f"Found {uninitialized_jobs.count()} uninitialized jobs. Calculating next run time...")
# Lấy timezone của dự án
tz = pytz.timezone(settings.TIME_ZONE)
for job in uninitialized_jobs:
@@ -69,12 +68,19 @@ def scan_and_run_due_jobs():
# BƯỚC 2: Quét và chạy các job đến hạn như bình thường
#logger.info("Scanning for due batch jobs...")
# Lấy các job cần chạy (có next_run_at không null và đã đến hạn)
due_jobs = Batch_Job.objects.filter(is_active=True, next_run_at__lte=now)
active_jobs = Batch_Job.objects.filter(is_active=True)
# Tách riêng job chạy mỗi phút (* * * * *)
every_minute_jobs = active_jobs.filter(cron_schedule="* * * * *")
# Các job bình thường (không phải * * * * *), kiểm tra next_run_at
normal_due_jobs = active_jobs.filter(next_run_at__lte=now).exclude(cron_schedule="* * * * *")
# Gộp hai QuerySet bằng union (hoặc |)
due_jobs = every_minute_jobs | normal_due_jobs
if not due_jobs.exists():
#logger.info("-> No due jobs found at this time.")
logger.info("-> No due jobs found at this time.")
return
#logger.info(f"-> Found {due_jobs.count()} due jobs to run.")
@@ -159,4 +165,3 @@ def start():
# Chạy tác vụ quét job mỗi 5 giây
scheduler.add_job(scan_and_run_due_jobs, 'interval', seconds=5, id='scan_due_jobs_job', replace_existing=True)
scheduler.start()
#logger.info("APScheduler started... Jobs will be scanned every 5 seconds.")