18 lines
558 B
Python
18 lines
558 B
Python
from sqlalchemy import desc, select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.config import Settings
|
|
from app.models import CrawlRun
|
|
from app.services.crawler import run_crawl
|
|
|
|
|
|
def get_running_run(db: Session) -> CrawlRun | None:
|
|
return db.scalar(select(CrawlRun).where(CrawlRun.status == "running").order_by(desc(CrawlRun.started_at)).limit(1))
|
|
|
|
|
|
def run_crawl_if_idle(db: Session, settings: Settings) -> tuple[CrawlRun, bool]:
|
|
running = get_running_run(db)
|
|
if running:
|
|
return running, False
|
|
return run_crawl(db, settings), True
|