Skip to content

Commit

Permalink
Introduce deleted status to fix dismissed requests behaviour (#123)
Browse files Browse the repository at this point in the history
* introduce deleted status

* qa

* qa
  • Loading branch information
francesconazzaro authored Jul 25, 2024
1 parent ca55b34 commit aa53cac
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
53 changes: 53 additions & 0 deletions alembic/versions/a4e8be715296_add_deleted_as_new_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""add deleted as new status.
Revision ID: a4e8be715296
Revises: d5d4afc97d40
Create Date: 2024-07-25 13:13:11.955119
"""

from sqlalchemy.dialects.postgresql import ENUM

from alembic import op

# revision identifiers, used by Alembic.
revision = "a4e8be715296"
down_revision = "d5d4afc97d40"
branch_labels = None
depends_on = None


old_status_enum = ENUM(
"pending", "in_progress", "completed", "failed", name="status", create_type=False
)
new_status_enum = ENUM(
"pending",
"in_progress",
"completed",
"failed",
"deleted",
name="status",
create_type=False,
)


def upgrade() -> None:
# Add the new status to the enum
op.alter_column(
"system_requests",
"status",
existing_type=old_status_enum,
type_=new_status_enum,
existing_nullable=False,
)


def downgrade() -> None:
# Remove the new status from the enum
op.alter_column(
"system_requests",
"status",
existing_type=new_status_enum,
type_=old_status_enum,
existing_nullable=False,
)
8 changes: 5 additions & 3 deletions cads_broker/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


status_enum = sa.Enum(
"accepted", "running", "failed", "successful", "dismissed", name="status"
"accepted", "running", "failed", "successful", "dismissed", "deleted", name="status"
)
DISMISSED_MESSAGE = os.getenv(
"DISMISSED_MESSAGE", "The request has been dismissed by the system."
Expand Down Expand Up @@ -642,8 +642,10 @@ def set_successful_request(


def set_dismissed_request(request_uid: str, session: sa.orm.Session) -> SystemRequest:
statement = sa.select(SystemRequest).where(SystemRequest.request_uid == request_uid)
request = session.scalars(statement).one()
request = get_request(request_uid=request_uid, session=session)
metadata = dict(request.request_metadata)
metadata.update({"previous_status": request.status})
request.request_metadata = metadata
request.status = "dismissed"
request.response_error = {"reason": "Dismissed by the user"}
session.commit()
Expand Down
12 changes: 11 additions & 1 deletion cads_broker/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,20 @@ def sync_database(self, session: sa.orm.Session) -> None:
for request in dismissed_requests:
if future := self.futures.pop(request.request_uid, None):
future.cancel()
if (
previous_status := request.request_metadata.get(
"previous_status", "accepted"
)
) == "running":
self.qos.notify_end_of_request(
request, session, scheduler=self.internal_scheduler
)
self.queue.pop(request.request_uid, None)
elif previous_status == "accepted":
self.queue.pop(request.request_uid, None)
self.qos.notify_dismission_of_request(
request, session, scheduler=self.internal_scheduler
)
request.status = "deleted"
session.commit()

statement = sa.select(db.SystemRequest).where(
Expand Down
17 changes: 17 additions & 0 deletions cads_broker/qos/QoS.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,23 @@ def pick(self, queue, session):

return request

@locked
def notify_dismission_of_request(self, request, session, scheduler):
"""Notify the dismission of a request."""
limits_list = []
for limit in self.limits_for(request, session):
limit.remove_from_queue(request.request_uid)
limits_list.append(limit)
scheduler.append(
{
"function": database.delete_request_qos_status,
"kwargs": {
"rules": limits_list,
"request_uid": request.request_uid,
},
}
)

@locked
def notify_start_of_request(self, request, session, scheduler):
"""Notify the start of a request.
Expand Down
5 changes: 4 additions & 1 deletion cads_broker/qos/Rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,12 @@ def __init__(self, environment, info, condition, conclusion):
self.queued = set()

def increment(self, request_uid):
self.remove_from_queue(request_uid)
self.value += 1

def remove_from_queue(self, request_uid):
if request_uid in self.queued:
self.queued.remove(request_uid)
self.value += 1

def decrement(self):
if self.value > 0:
Expand Down

0 comments on commit aa53cac

Please sign in to comment.