|
|
@ -209,3 +209,43 @@ def run_crawl_pipeline(run_id, force=False): |
|
|
run.ads_evaluated_count = len(new_eval_ids) |
|
|
run.ads_evaluated_count = len(new_eval_ids) |
|
|
run.save() |
|
|
run.save() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task |
|
|
|
|
|
def check_and_schedule_crawl_tasks(): |
|
|
|
|
|
""" |
|
|
|
|
|
Periodic master task executed every minute by Celery Beat. |
|
|
|
|
|
Evaluates all active CrawlTasks and dispatches run_crawl_pipeline if: |
|
|
|
|
|
- Current time falls within start_hour and end_hour window |
|
|
|
|
|
- Time elapsed since the last run is >= interval_minutes (or has never run) |
|
|
|
|
|
""" |
|
|
|
|
|
now_dt = timezone.now() |
|
|
|
|
|
now_time = timezone.localtime(now_dt).time() |
|
|
|
|
|
|
|
|
|
|
|
active_tasks = CrawlTask.objects.filter(is_active=True) |
|
|
|
|
|
for task in active_tasks: |
|
|
|
|
|
# Check start_hour and end_hour window (handles overnight windows like 22:00 to 06:00) |
|
|
|
|
|
in_window = False |
|
|
|
|
|
if task.start_hour <= task.end_hour: |
|
|
|
|
|
in_window = task.start_hour <= now_time <= task.end_hour |
|
|
|
|
|
else: |
|
|
|
|
|
in_window = now_time >= task.start_hour or now_time <= task.end_hour |
|
|
|
|
|
|
|
|
|
|
|
if not in_window: |
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
# Check last run |
|
|
|
|
|
last_run = CrawlRun.objects.filter(crawl_task=task).order_by('-started_at').first() |
|
|
|
|
|
if last_run: |
|
|
|
|
|
# If a run is currently in progress, skip scheduling another |
|
|
|
|
|
if last_run.status == 'RUNNING': |
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
elapsed_minutes = (now_dt - last_run.started_at).total_seconds() / 60.0 |
|
|
|
|
|
if elapsed_minutes < task.interval_minutes: |
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
# Create new run and trigger background pipeline |
|
|
|
|
|
run = CrawlRun.objects.create(crawl_task=task, status='RUNNING') |
|
|
|
|
|
run_crawl_pipeline.delay(str(run.id)) |
|
|
|
|
|
|
|
|
|
|
|
|