-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add task to db and tests for migrations
- Loading branch information
1 parent
27bb0bd
commit c96c914
Showing
9 changed files
with
204 additions
and
44 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""add task | ||
Revision ID: 7dc9a3441d07 | ||
Revises: 6a59d47fe978 | ||
Create Date: 2024-04-02 04:04:09.759025 | ||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "7dc9a3441d07" | ||
down_revision: Union[str, None] = "6a59d47fe978" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"task", | ||
sa.Column("id", sa.Uuid(), nullable=False), | ||
sa.Column( | ||
"status", | ||
sa.Enum("FAILED", "CREATED", "RUNNING", "COMPLETED", name="taskstatus"), | ||
nullable=False, | ||
), | ||
sa.Column("config", postgresql.JSONB(astext_type=sa.Text()), nullable=False), | ||
sa.Column("result", postgresql.JSONB(astext_type=sa.Text()), nullable=True), | ||
sa.Column("dataset_id", sa.Uuid(), nullable=False), | ||
sa.Column("raised_exception_name", sa.String(), nullable=True), | ||
sa.Column( | ||
"failure_reason", | ||
sa.Enum( | ||
"MEMORY_LIMIT_EXCEEDED", | ||
"TIME_LIMIT_EXCEEDED", | ||
"WORKER_KILLED_BY_SIGNAL", | ||
"OTHER", | ||
name="taskfailurereason", | ||
), | ||
nullable=True, | ||
), | ||
sa.Column("traceback", sa.String(), nullable=True), | ||
sa.Column("created_at", sa.TIMESTAMP(), nullable=False), | ||
sa.Column("updated_at", sa.TIMESTAMP(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["dataset_id"], | ||
["dataset.id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
# ADJUSTED! See: https://github.com/sqlalchemy/alembic/issues/278#issuecomment-907283386 | ||
def downgrade() -> None: | ||
op.drop_table("task") | ||
|
||
taskfailurereason = sa.Enum(name="taskfailurereason") | ||
taskfailurereason.drop(op.get_bind(), checkfirst=True) | ||
|
||
taskstatus = sa.Enum(name="taskstatus") | ||
taskstatus.drop(op.get_bind(), checkfirst=True) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from enum import StrEnum, auto | ||
import typing | ||
from uuid import UUID, uuid4 | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
from app.db import ORMBase | ||
from app.db.session import ORMBaseModel | ||
from sqlalchemy import ForeignKey | ||
from sqlalchemy.orm import relationship | ||
from app.domain.file.dataset import DatasetModel | ||
from app.domain.task import OneOfTaskConfig, OneOfTaskResult | ||
|
||
from sqlalchemy.dialects.postgresql import JSONB | ||
|
||
if typing.TYPE_CHECKING: | ||
from app.domain.file.dataset import DatasetORM | ||
|
||
|
||
class TaskStatus(StrEnum): | ||
FAILED = auto() | ||
CREATED = auto() | ||
RUNNING = auto() | ||
COMPLETED = auto() | ||
|
||
|
||
class TaskFailureReason(StrEnum): | ||
MEMORY_LIMIT_EXCEEDED = auto() | ||
TIME_LIMIT_EXCEEDED = auto() | ||
WORKER_KILLED_BY_SIGNAL = auto() | ||
OTHER = auto() | ||
|
||
|
||
class TaskORM(ORMBase): | ||
__tablename__ = "task" | ||
id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4) | ||
|
||
status: Mapped[TaskStatus] | ||
config: Mapped[OneOfTaskConfig] = mapped_column(JSONB) | ||
result: Mapped[OneOfTaskResult | None] = mapped_column(JSONB, default=None) | ||
|
||
dataset_id: Mapped[UUID] = mapped_column(ForeignKey("dataset.id"), nullable=False) | ||
dataset: Mapped["DatasetORM"] = relationship( | ||
"DatasetORM", back_populates="related_tasks" | ||
) | ||
|
||
# Only if task failed | ||
raised_exception_name: Mapped[str | None] = mapped_column(default=None) | ||
failure_reason: Mapped[TaskFailureReason | None] = mapped_column(default=None) | ||
traceback: Mapped[str | None] = mapped_column(default=None) | ||
|
||
|
||
class TaskModel(ORMBaseModel): | ||
id: UUID | ||
status: TaskStatus | ||
config: OneOfTaskConfig | ||
result: OneOfTaskResult | None | ||
dataset: DatasetModel | ||
|
||
raised_exception_name: str | None | ||
failure_reason: TaskFailureReason | None | ||
traceback: str | 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
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