Skip to content

Commit

Permalink
models: refactor WorkflowStatus to RunStatus
Browse files Browse the repository at this point in the history
models: refactor interactive session types
  • Loading branch information
audrium authored and Diego Rodriguez committed Oct 15, 2020
1 parent ff73981 commit 4cb7348
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
18 changes: 9 additions & 9 deletions reana_server/rest/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
from flask import current_app as app
from flask import jsonify, request, stream_with_context
from flask_login import current_user
from reana_commons.config import INTERACTIVE_SESSION_TYPES
from reana_commons.errors import REANAValidationError
from reana_commons.operational_options import validate_operational_options
from reana_db.database import Session
from reana_db.models import Workflow, WorkflowStatus
from reana_db.models import Workflow, RunStatus, InteractiveSessionType
from reana_db.utils import _get_workflow_with_uuid_or_name
from webargs import fields, validate
from webargs.flaskparser import use_kwargs
Expand Down Expand Up @@ -390,7 +389,7 @@ def create_workflow(): # noqa
).result()
if git_data:
Workflow.update_workflow_status(
Session, response["workflow_id"], WorkflowStatus.queued
Session, response["workflow_id"], RunStatus.queued
)
current_workflow_submission_publisher.publish_workflow_submission(
user_id=str(user.id_),
Expand Down Expand Up @@ -895,17 +894,17 @@ def start_workflow(workflow_id_or_name): # noqa
workflow.type_, parameters["operational_options"]
)
if "restart" in parameters:
if workflow.status not in [WorkflowStatus.finished, WorkflowStatus.failed]:
if workflow.status not in [RunStatus.finished, RunStatus.failed]:
raise ValueError("Only finished or failed workflows can be restarted.")
workflow = clone_workflow(
workflow, parameters.get("reana_specification", None)
)
elif workflow.status != WorkflowStatus.created:
elif workflow.status != RunStatus.created:
raise ValueError(
"Workflow {} is already {} and cannot be started "
"again.".format(workflow.get_full_workflow_name(), workflow.status.name)
)
Workflow.update_workflow_status(Session, workflow.id_, WorkflowStatus.queued)
Workflow.update_workflow_status(Session, workflow.id_, RunStatus.queued)
current_workflow_submission_publisher.publish_workflow_submission(
user_id=str(user.id_),
workflow_id_or_name=workflow.get_full_workflow_name(),
Expand All @@ -915,7 +914,7 @@ def start_workflow(workflow_id_or_name): # noqa
"message": "Workflow submitted.",
"workflow_id": workflow.id_,
"workflow_name": workflow.name,
"status": WorkflowStatus.queued.name,
"status": RunStatus.queued.name,
"run_number": workflow.run_number,
"user": str(user.id_),
}
Expand Down Expand Up @@ -1853,13 +1852,14 @@ def open_interactive_session(workflow_id_or_name, interactive_session_type): #
else:
user = get_user_from_token(request.args.get("access_token"))

if interactive_session_type not in INTERACTIVE_SESSION_TYPES:
if interactive_session_type not in InteractiveSessionType.__members__:
return (
jsonify(
{
"message": "Interactive session type {0} not found, try "
"with one of: {1}".format(
interactive_session_type, INTERACTIVE_SESSION_TYPES
interactive_session_type,
[e.name for e in InteractiveSessionType],
)
}
),
Expand Down
16 changes: 8 additions & 8 deletions reana_server/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from invenio_accounts.models import SessionActivity
from reana_commons.config import SHARED_VOLUME_PATH
from reana_db.database import Session
from reana_db.models import Workflow, WorkflowStatus, InteractiveSession
from reana_db.models import Workflow, RunStatus, InteractiveSession


class REANAStatus:
Expand Down Expand Up @@ -55,9 +55,9 @@ def __init__(self, from_=None, until=None, user=None):
def get_active(self):
"""Get the number of active interactive sessions."""
non_active_statuses = [
WorkflowStatus.stopped,
WorkflowStatus.deleted,
WorkflowStatus.failed,
RunStatus.stopped,
RunStatus.deleted,
RunStatus.failed,
]
active_interactive_sessions = (
Session.query(InteractiveSession)
Expand Down Expand Up @@ -225,7 +225,7 @@ def stuck_workflows(self):
inactivity_threshold = datetime.now() - timedelta(hours=12)
number = (
Session.query(Workflow)
.filter(Workflow.status == WorkflowStatus.running)
.filter(Workflow.status == RunStatus.running)
.filter(Workflow.run_started_at <= inactivity_threshold)
.filter(Workflow.updated <= inactivity_threshold)
.count()
Expand All @@ -242,10 +242,10 @@ def git_workflows(self):
def get_status(self):
"""Get status summary for REANA workflows."""
return {
"running": self.get_workflows_by_status(WorkflowStatus.running),
"finished": self.get_workflows_by_status(WorkflowStatus.finished),
"running": self.get_workflows_by_status(RunStatus.running),
"finished": self.get_workflows_by_status(RunStatus.finished),
"stuck": self.stuck_workflows(),
"queued": self.get_workflows_by_status(WorkflowStatus.queued),
"queued": self.get_workflows_by_status(RunStatus.queued),
"restarts": self.restarted_workflows(),
"git_source": self.git_workflows(),
}
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ python-dateutil==2.8.1 # via alembic, bravado, bravado-core, kubernetes
python-editor==1.0.4 # via alembic
pytz==2020.1 # via babel, bravado-core, celery, fs
pyyaml==5.3.1 # via bravado, bravado-core, kubernetes, reana-commons, swagger-spec-validator
reana-commons[kubernetes]==0.8.0a3 # via reana-db, reana-server (setup.py)
reana-db==0.8.0a4 # via reana-server (setup.py)
reana-commons[kubernetes]==0.8.0a4 # via reana-db, reana-server (setup.py)
reana-db==0.8.0a5 # via reana-server (setup.py)
redis==3.5.3 # via invenio-accounts, invenio-celery
requests-oauthlib==1.1.0 # via flask-oauthlib, invenio-oauth2server, invenio-oauthclient, kubernetes
requests==2.20.0 # via bravado, kubernetes, reana-server (setup.py), requests-oauthlib
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
history = open("CHANGES.rst").read()

tests_require = [
"pytest-reana>=0.7.0.dev20200707,<0.8.0",
"pytest-reana>=0.8.0a1,<0.9.0",
]

extras_require = {
Expand Down Expand Up @@ -48,8 +48,8 @@
install_requires = [
"marshmallow>2.13.0,<=2.20.1",
"pyOpenSSL==17.5.0",
"reana-commons[kubernetes]>=0.8.0a3,<0.9.0",
"reana-db>=0.8.0a4,<0.9.0",
"reana-commons[kubernetes]>=0.8.0a4,<0.9.0",
"reana-db>=0.8.0a5,<0.9.0",
"requests==2.20.0",
"rfc3987==1.3.7",
"strict-rfc3339==0.7",
Expand Down
5 changes: 2 additions & 3 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
from flask import url_for
from mock import Mock, patch
from pytest_reana.test_utils import make_mock_api_client
from reana_commons.config import INTERACTIVE_SESSION_TYPES
from reana_db.models import User
from reana_db.models import User, InteractiveSessionType

from reana_server.utils import (
_create_and_associate_local_user,
Expand Down Expand Up @@ -447,7 +446,7 @@ def test_move_files(app, default_user, _get_user_mock):

@pytest.mark.parametrize(
("interactive_session_type", "expected_status_code"),
[(int_session_type, 200) for int_session_type in INTERACTIVE_SESSION_TYPES]
[(int_session_type.name, 200) for int_session_type in InteractiveSessionType]
+ [("wrong-interactive-type", 404)],
)
def test_open_interactive_session(
Expand Down

0 comments on commit 4cb7348

Please sign in to comment.