-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77afeb3
commit 4e2f269
Showing
46 changed files
with
1,154 additions
and
558 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from collections.abc import Generator | ||
from contextlib import contextmanager | ||
|
||
from sqlalchemy.orm import Session, sessionmaker | ||
|
||
from core.util.log import LoggerMixin | ||
|
||
|
||
class Job(LoggerMixin, ABC): | ||
def __init__(self, session_maker: sessionmaker[Session]): | ||
self._session_maker = session_maker | ||
|
||
@contextmanager | ||
def session(self) -> Generator[Session, None, None]: | ||
with self._session_maker() as session: | ||
yield session | ||
|
||
@contextmanager | ||
def transaction(self) -> Generator[Session, None, None]: | ||
with self._session_maker.begin() as session: | ||
yield session | ||
|
||
@abstractmethod | ||
def run(self) -> None: | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import annotations | ||
|
||
import celery | ||
from sqlalchemy.orm import Session, sessionmaker | ||
from sqlalchemy.pool import NullPool | ||
|
||
from core.model import SessionManager | ||
from core.service.container import Services, container_instance | ||
from core.util.log import LoggerMixin | ||
|
||
|
||
class Task(celery.Task, LoggerMixin): | ||
_session_maker = None | ||
|
||
@property | ||
def session_maker(self) -> sessionmaker[Session]: | ||
if self._session_maker is None: | ||
engine = SessionManager.engine(poolclass=NullPool) | ||
maker = sessionmaker(bind=engine) | ||
SessionManager.setup_event_listener(maker) | ||
self._session_maker = maker | ||
return self._session_maker | ||
|
||
@property | ||
def services(self) -> Services: | ||
return container_instance() | ||
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from __future__ import annotations | ||
|
||
from celery import shared_task | ||
from sqlalchemy import select | ||
from sqlalchemy.orm import Session, sessionmaker | ||
|
||
from core.celery.job import Job | ||
from core.celery.task import Task | ||
from core.model import Collection | ||
|
||
|
||
class CollectionDeleteJob(Job): | ||
def __init__(self, session_maker: sessionmaker[Session], collection_id: int): | ||
super().__init__(session_maker) | ||
self.collection_id = collection_id | ||
|
||
@staticmethod | ||
def collection(session: Session, collection_id: int) -> Collection | None: | ||
return ( | ||
session.execute(select(Collection).where(Collection.id == collection_id)) | ||
.scalars() | ||
.one_or_none() | ||
) | ||
|
||
@staticmethod | ||
def collection_name(collection: Collection) -> str: | ||
return f"{collection.name}/{collection.protocol} ({collection.id})" | ||
|
||
def run(self) -> None: | ||
with self.transaction() as session: | ||
collection = self.collection(session, self.collection_id) | ||
if collection is None: | ||
self.log.error( | ||
f"Collection with id {self.collection_id} not found. Unable to delete." | ||
) | ||
return | ||
|
||
self.log.info(f"Deleting collection {self.collection_name(collection)}") | ||
collection.delete() | ||
|
||
|
||
@shared_task(key="high", bind=True) | ||
def collection_delete(task: Task, collection_id: int) -> None: | ||
CollectionDeleteJob(task.session_maker, collection_id).run() |
Oops, something went wrong.