Skip to content

Commit

Permalink
Add status CANCELED
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaretnikov committed Feb 25, 2024
1 parent 2b77071 commit e778e13
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""add canceled status
Revision ID: 03c839888c82
Revises: 57cd11b949d5
Create Date: 2024-01-29 03:56:36.889909
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "03c839888c82"
down_revision = "57cd11b949d5"
branch_labels = None
depends_on = None


# Migrating from/to VARCHAR having the same length might look strange, but it
# serves a purpose. This will be a no-op in SQLite because it represents Python
# enums as VARCHAR, but it will convert the enum in PostgreSQL to VARCHAR. The
# old type is set to VARCHAR here because you can cast an enum to VARCHAR, which
# is needed for the migration to work. In the end, both DBs will use VARCHAR to
# represent the Python enum, which makes it easier to support both DBs at the
# same time.
def upgrade():
with op.batch_alter_table(
"build",
schema=None,
) as batch_op:
batch_op.alter_column(
"status",
existing_type=sa.VARCHAR(length=9),
type_=sa.VARCHAR(length=9),
existing_nullable=False,
)
if not str(op.get_bind().engine.url).startswith("sqlite"):
op.execute("DROP TYPE IF EXISTS buildstatus")


def downgrade():
op.execute("DELETE FROM build WHERE status = 'CANCELED'")
with op.batch_alter_table(
"build",
schema=None,
) as batch_op:
batch_op.alter_column(
"status",
existing_type=sa.VARCHAR(length=9),
type_=sa.VARCHAR(length=9),
existing_nullable=False,
)
20 changes: 18 additions & 2 deletions conda-store-server/conda_store_server/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def set_build_failed(
db.commit()


def set_build_canceled(
db: Session, build: orm.Build, status_info: typing.Optional[str] = None
):
build.status = schema.BuildStatus.CANCELED
build.status_info = status_info
build.ended_on = datetime.datetime.utcnow()
db.commit()


def set_build_completed(db: Session, conda_store, build: orm.Build):
build.status = schema.BuildStatus.COMPLETED
build.ended_on = datetime.datetime.utcnow()
Expand All @@ -65,7 +74,11 @@ def set_build_completed(db: Session, conda_store, build: orm.Build):


def build_cleanup(
db: Session, conda_store, build_ids: typing.List[str] = None, reason: str = None
db: Session,
conda_store,
build_ids: typing.List[str] = None,
reason: str = None,
is_canceled: bool = False,
):
"""Walk through all builds in BUILDING state and check that they are actively running
Expand Down Expand Up @@ -121,7 +134,10 @@ def build_cleanup(
build,
reason,
)
set_build_failed(db, build)
if is_canceled:
set_build_canceled(db, build)
else:
set_build_failed(db, build)


def build_conda_environment(db: Session, conda_store, build):
Expand Down
1 change: 1 addition & 0 deletions conda-store-server/conda_store_server/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class BuildStatus(enum.Enum):
BUILDING = "BUILDING"
COMPLETED = "COMPLETED"
FAILED = "FAILED"
CANCELED = "CANCELED"


class BuildArtifact(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion conda-store-server/conda_store_server/server/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,9 @@ async def api_put_build_cancel(
tasks.task_cleanup_builds.si(
build_ids=[build_id],
reason=f"""
build {build_id} marked as FAILED due to being canceled from the REST API
build {build_id} marked as CANCELED due to being canceled from the REST API
""",
is_canceled=True,
).apply_async(countdown=5)

return {
Expand Down
9 changes: 7 additions & 2 deletions conda-store-server/conda_store_server/worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,15 @@ def task_update_storage_metrics(self):


@shared_task(base=WorkerTask, name="task_cleanup_builds", bind=True)
def task_cleanup_builds(self, build_ids: typing.List[str] = None, reason: str = None):
def task_cleanup_builds(
self,
build_ids: typing.List[str] = None,
reason: str = None,
is_canceled: bool = False,
):
conda_store = self.worker.conda_store
with conda_store.session_factory() as db:
build_cleanup(db, conda_store, build_ids, reason)
build_cleanup(db, conda_store, build_ids, reason, is_canceled)


"""
Expand Down

0 comments on commit e778e13

Please sign in to comment.