Skip to content

Commit

Permalink
fix(stop): store engine logs of stopped workflow (#563)
Browse files Browse the repository at this point in the history
Closes #560
  • Loading branch information
mdonadoni committed Feb 1, 2024
1 parent 817b019 commit 199c163
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
8 changes: 3 additions & 5 deletions reana_workflow_controller/rest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
REANA_GITLAB_HOST,
PREVIEWABLE_MIME_TYPE_PREFIXES,
)
from reana_workflow_controller.consumer import _update_workflow_status
from reana_workflow_controller.errors import (
REANAExternalCallError,
REANAWorkflowControllerError,
Expand Down Expand Up @@ -135,11 +136,8 @@ def _start_workflow_db(workflow, parameters):

def stop_workflow(workflow):
"""Stop a given workflow."""
if workflow.status == RunStatus.running:
kwrm = KubernetesWorkflowRunManager(workflow)
kwrm.stop_batch_workflow_run()
workflow.status = RunStatus.stopped
Session.add(workflow)
if workflow.can_transition_to(RunStatus.stopped):
_update_workflow_status(workflow, RunStatus.stopped, logs="")
Session.commit()
else:
message = ("Workflow {id_} is not running.").format(id_=workflow.id_)
Expand Down
25 changes: 15 additions & 10 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,19 +823,20 @@ def test_start_already_started_workflow(

@pytest.mark.parametrize(
"current_status, expected_status, expected_http_status_code, "
"k8s_stop_call_count",
"k8s_stop_call_count, should_update_logs",
[
(RunStatus.created, RunStatus.created, 409, 0),
(RunStatus.running, RunStatus.stopped, 200, 1),
(RunStatus.failed, RunStatus.failed, 409, 0),
(RunStatus.finished, RunStatus.finished, 409, 0),
(RunStatus.created, RunStatus.created, 409, 0, False),
(RunStatus.running, RunStatus.stopped, 200, 1, True),
(RunStatus.failed, RunStatus.failed, 409, 0, False),
(RunStatus.finished, RunStatus.finished, 409, 0, False),
],
)
def test_stop_workflow(
current_status,
expected_status,
expected_http_status_code,
k8s_stop_call_count,
should_update_logs,
app,
default_user,
yadage_workflow_with_name,
Expand All @@ -847,10 +848,13 @@ def test_stop_workflow(
sample_serial_workflow_in_db.status = current_status
session.add(sample_serial_workflow_in_db)
session.commit()
workflow_engine_logs = "these are the logs of workflow-engine"
with mock.patch(
"reana_workflow_controller.workflow_run_manager."
"current_k8s_batchv1_api_client"
) as stop_workflow_mock:
"reana_workflow_controller.consumer.current_k8s_batchv1_api_client"
) as batch_api_mock, mock.patch(
"reana_workflow_controller.consumer._get_workflow_engine_pod_logs"
) as get_logs_mock:
get_logs_mock.return_value = workflow_engine_logs
res = client.put(
url_for(
"statuses.set_workflow_status",
Expand All @@ -861,9 +865,10 @@ def test_stop_workflow(
assert sample_serial_workflow_in_db.status == expected_status
assert res.status_code == expected_http_status_code
assert (
stop_workflow_mock.delete_namespaced_job.call_count
== k8s_stop_call_count
batch_api_mock.delete_namespaced_job.call_count == k8s_stop_call_count
)
if should_update_logs:
assert workflow_engine_logs in sample_serial_workflow_in_db.logs


def test_set_workflow_status_unauthorized(
Expand Down

0 comments on commit 199c163

Please sign in to comment.