Skip to content

Commit

Permalink
Fix WRSC not having a payload
Browse files Browse the repository at this point in the history
  • Loading branch information
reisingerf committed Nov 4, 2024
1 parent 72d2765 commit 2a5456f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
Workflow,
Library,
LibraryAssociation,
Payload,
State,
Status,
)
Expand Down Expand Up @@ -121,24 +120,24 @@ def handler(event, context):
new_state = State(
status=srv_wrsc.status,
timestamp=srv_wrsc.timestamp,
payload=create_payload_stub_from_wrsc(srv_wrsc)
)
if srv_wrsc.payload:
# handle the payload
new_state.payload = create_payload_stub_from_wrsc(srv_wrsc)

# attempt to transition to new state (will persist new state if successful)
success = wfr_util.transition_to(new_state)
if not success:
logger.warning(f"Could not apply new state: {new_state}")
return None

wfm_wrsc = map_srv_wrsc_to_wfm_wrsc(srv_wrsc)
# Update payload ID
wfm_wrsc.payload.refId = new_state.payload.payload_ref_id
wfm_wrsc = map_srv_wrsc_to_wfm_wrsc(srv_wrsc, new_state)

logger.info(f"{__name__} done.")
return wfm_wrsc


def map_srv_wrsc_to_wfm_wrsc(input_wrsc: srv.WorkflowRunStateChange) -> wfm.WorkflowRunStateChange:
def map_srv_wrsc_to_wfm_wrsc(input_wrsc: srv.WorkflowRunStateChange, new_state: State) -> wfm.WorkflowRunStateChange:
out_wrsc = wfm.WorkflowRunStateChange(
portalRunId=input_wrsc.portalRunId,
timestamp=input_wrsc.timestamp,
Expand All @@ -147,6 +146,18 @@ def map_srv_wrsc_to_wfm_wrsc(input_wrsc: srv.WorkflowRunStateChange) -> wfm.Work
workflowVersion=input_wrsc.workflowVersion,
workflowRunName=input_wrsc.workflowRunName,
linkedLibraries=input_wrsc.linkedLibraries,
payload=input_wrsc.payload, # requires payload ID
)
# NOTE: the srv payload is not quite the same as the wfm payload (it's missing a payload ref id that's assigned by the wfm)
# So, if the new state has a payload, we need to map the service payload to the wfm payload
if new_state.payload:
out_wrsc.payload = map_srv_payload_to_wfm_payload(input_wrsc.payload, new_state.payload.payload_ref_id)
return out_wrsc


def map_srv_payload_to_wfm_payload(input_payload: srv.Payload, ref_id: str) -> wfm.Payload:
out_payload = wfm.Payload(
refId=ref_id,
version=input_payload.version,
data=input_payload.data
)
return out_payload
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from datetime import datetime, timedelta
from typing import List

Expand Down Expand Up @@ -58,6 +59,40 @@ def test_create_wrsc_no_library(self):
# We don't expect any library associations here!
self.assertEqual(0, db_wfr.libraries.count())


def test_create_wrsc_no_payload(self):
"""
python manage.py test workflow_manager_proc.tests.test_create_workflow_run_state.WorkflowSrvUnitTests.test_create_wrsc_no_payload
"""

test_event = {
"portalRunId": "202405012397gatc",
"executionId": "icav2.id.12345",
"timestamp": "2025-05-01T09:25:44Z",
"status": "DRAFT",
"workflowName": "ctTSO500",
"workflowVersion": "4.2.7",
"workflowRunName": "ctTSO500-L000002"
}

logger.info("Test the created WRSC event...")
result_wrsc: WorkflowRunStateChange = create_workflow_run_state.handler(test_event, None)
logger.info(result_wrsc)
self.assertIsNotNone(result_wrsc)
self.assertEqual("ctTSO500-L000002", result_wrsc.workflowRunName)
# We don't expect any library associations here!
self.assertIsNone(result_wrsc.linkedLibraries)
self.assertIsNone(result_wrsc.payload)

logger.info("Test the persisted DB record...")
wfr_qs: QuerySet = WorkflowRun.objects.all()
self.assertEqual(1, wfr_qs.count())
db_wfr: WorkflowRun = wfr_qs.first()
self.assertEqual("ctTSO500-L000002", db_wfr.workflow_run_name)
# We don't expect any library associations here!
self.assertEqual(0, db_wfr.libraries.count())


def test_create_wrsc_library(self):
"""
python manage.py test workflow_manager_proc.tests.test_create_workflow_run_state.WorkflowSrvUnitTests.test_create_wrsc_library
Expand Down

0 comments on commit 2a5456f

Please sign in to comment.