Skip to content

Commit

Permalink
[DOP-22267] - add removing orphan transfers from scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-lixakov committed Dec 12, 2024
1 parent da40fb5 commit 431ab07
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions syncmaster/scheduler/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ async def main():

while True:
logger.info("Looking at the transfer table...")

await transfer_job_manager.remove_orphan_jobs()
transfers = await transfer_fetcher.fetch_updated_jobs()

if transfers:
Expand All @@ -29,6 +31,7 @@ async def main():
", ".join(str(t.id) for t in transfers),
)
transfer_job_manager.update_jobs(transfers)

transfer_fetcher.last_updated_at = max(t.updated_at for t in transfers)
logger.info("Scheduler state has been updated. Last updated at: %s", transfer_fetcher.last_updated_at)

Expand Down
20 changes: 19 additions & 1 deletion syncmaster/scheduler/transfer_job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from kombu.exceptions import KombuError
from sqlalchemy import select

from syncmaster.backend.services.unit_of_work import UnitOfWork
from syncmaster.db.models import RunType, Status, Transfer
from syncmaster.exceptions.run import CannotConnectToTaskQueueError
from syncmaster.exceptions.transfer import TransferNotFoundError
from syncmaster.scheduler.celery import app as celery
from syncmaster.scheduler.settings import SchedulerAppSettings as Settings
from syncmaster.scheduler.utils import get_async_session
Expand Down Expand Up @@ -47,6 +49,18 @@ def update_jobs(self, transfers: list[Transfer]) -> None:
args=(transfer.id,),
)

async def remove_orphan_jobs(self) -> None:
all_jobs = self.scheduler.get_jobs()
settings = self.settings

async with get_async_session(settings) as session:
transfer_ids = {t.id for t in (await session.execute(select(Transfer))).scalars().all()}

for job in all_jobs:
transfer_id = int(job.id)
if transfer_id not in transfer_ids:
self.scheduler.remove_job(job.id)

@staticmethod
async def send_job_to_celery(transfer_id: int) -> None:
"""
Expand All @@ -61,7 +75,11 @@ async def send_job_to_celery(transfer_id: int) -> None:
async with get_async_session(settings) as session:
unit_of_work = UnitOfWork(session=session, settings=settings)

transfer = await unit_of_work.transfer.read_by_id(transfer_id)
try:
transfer = await unit_of_work.transfer.read_by_id(transfer_id)
except TransferNotFoundError:
return

credentials_source = await unit_of_work.credentials.read(transfer.source_connection_id)
credentials_target = await unit_of_work.credentials.read(transfer.target_connection_id)

Expand Down

0 comments on commit 431ab07

Please sign in to comment.