From 907459b75ee9aaa9ef028f641e14d1f875b7729a Mon Sep 17 00:00:00 2001 From: Alputer Date: Mon, 18 Nov 2024 11:51:36 +0100 Subject: [PATCH] fix(rest): detect session status from the pod state (#611) Closes reanahub/reana-ui#408 --- reana_workflow_controller/k8s.py | 16 ++++++++++++++++ reana_workflow_controller/rest/workflows.py | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/reana_workflow_controller/k8s.py b/reana_workflow_controller/k8s.py index f7032c4f..e4a246b5 100644 --- a/reana_workflow_controller/k8s.py +++ b/reana_workflow_controller/k8s.py @@ -11,6 +11,7 @@ from reana_commons.config import ( REANA_WORKFLOW_UMASK, REANA_RUNTIME_SESSIONS_KUBERNETES_NODE_LABEL, + REANA_RUNTIME_KUBERNETES_NAMESPACE, ) from reana_commons.k8s.api_client import ( current_k8s_appsv1_api_client, @@ -479,3 +480,18 @@ def delete_dask_dashboard_ingress(cluster_name, workflow_id): plural="middlewares", name=f"replacepath-{workflow_id}", ) + + +def check_pod_status_by_prefix( + pod_name_prefix, namespace=REANA_RUNTIME_KUBERNETES_NAMESPACE +): + """Check if there is a Pod in the given namespace whose name starts with the specified prefix. We assume that there exists 0 or 1 pod with a given prefix.""" + try: + pods = current_k8s_corev1_api_client.list_namespaced_pod(namespace=namespace) + + for pod in pods.items: + if pod.metadata.name.startswith(pod_name_prefix): + return pod.status.phase + return "Not Found" + except ApiException as e: + return f"Error: {e.reason}" diff --git a/reana_workflow_controller/rest/workflows.py b/reana_workflow_controller/rest/workflows.py index 1ee83fa1..13983f47 100644 --- a/reana_workflow_controller/rest/workflows.py +++ b/reana_workflow_controller/rest/workflows.py @@ -22,6 +22,7 @@ from webargs import fields, validate from webargs.flaskparser import use_args, use_kwargs from reana_commons.config import WORKFLOW_TIME_FORMAT +from reana_commons.utils import build_unique_component_name from reana_db.database import Session from reana_db.models import RunStatus, User, UserWorkflow, Workflow, WorkflowResource from reana_db.utils import ( @@ -48,6 +49,8 @@ use_paginate_args, ) +from reana_workflow_controller.k8s import check_pod_status_by_prefix + START = "start" STOP = "stop" DELETED = "deleted" @@ -398,7 +401,20 @@ def get_workflows(args, paginate=None): # noqa if int_session: workflow_response["session_type"] = int_session.type_.name workflow_response["session_uri"] = int_session.path + int_session_pod_name_prefix = build_unique_component_name( + "run-session", int_session.workflow[0].id_ + ) + if int_session.status == RunStatus.created: + pod_status = check_pod_status_by_prefix( + pod_name_prefix=int_session_pod_name_prefix + ) + if pod_status == "Running": + int_session.status = RunStatus.running + db_session = Session.object_session(int_session) + db_session.commit() + workflow_response["session_status"] = int_session.status.name + # Skip workflow if type is interactive and there is no session elif type_ == "interactive": continue