Esempio n. 1
0
def exec_query(workflow_client: WorkflowClient,
               qm: QueryMethod,
               args,
               stub_instance: object = None):
    assert stub_instance._execution
    request = QueryWorkflowRequest()
    request.execution = stub_instance._execution
    request.query = WorkflowQuery()
    request.query.query_type = qm.name
    request.query.query_args = args_to_json(args)
    request.domain = workflow_client.domain
    response: QueryWorkflowResponse
    response, err = workflow_client.service.query_workflow(request)
    if err:
        if isinstance(err, QueryFailedError):
            cause = deserialize_exception(err.message)
            raise QueryFailureException(
                query_type=qm.name,
                execution=stub_instance._execution) from cause
        elif isinstance(err, Exception):
            raise err
        else:
            raise Exception(err)
    if response.query_rejected:
        raise QueryRejectedException(response.query_rejected.close_status)
    return json.loads(response.query_result)
 def handle_activity_task_failed(self, event: HistoryEvent):
     attr = event.activity_task_failed_event_attributes
     if self.decider.handle_activity_task_closed(attr.scheduled_event_id):
         future = self.scheduled_activities.get(attr.scheduled_event_id)
         if future:
             self.scheduled_activities.pop(attr.scheduled_event_id)
             # TODO: attr.reason - what should we do with it?
             ex = deserialize_exception(attr.details)
             future.set_exception(ex)
         else:
             raise NonDeterministicWorkflowException(
                 f"Trying to complete activity event {attr.scheduled_event_id} that is not in scheduled_activities")
Esempio n. 3
0
 def wait_for_close_with_workflow_id(self,
                                     workflow_id: str,
                                     run_id: str = None,
                                     workflow_type: str = None):
     while True:
         history_request = create_close_history_event_request(
             self, workflow_id, run_id)
         history_response, err = self.service.get_workflow_execution_history(
             history_request)
         if err:
             raise Exception(err)
         if not history_response.history.events:
             continue
         history_event = history_response.history.events[0]
         if history_event.event_type == EventType.WorkflowExecutionCompleted:
             attributes = history_event.workflow_execution_completed_event_attributes
             return json.loads(attributes.result)
         elif history_event.event_type == EventType.WorkflowExecutionFailed:
             attributes = history_event.workflow_execution_failed_event_attributes
             if attributes.reason == "WorkflowFailureException":
                 exception = deserialize_exception(attributes.details)
                 if isinstance(exception, ActivityFailureException):
                     exception.set_cause()
                 workflow_execution = WorkflowExecution(
                     workflow_id=workflow_id, run_id=run_id)
                 raise WorkflowFailureException(
                     workflow_type=workflow_type,
                     execution=workflow_execution) from exception
             else:
                 details: Dict = json.loads(attributes.details)
                 detail_message = details.get("detailMessage", "")
                 raise WorkflowExecutionFailedException(
                     attributes.reason,
                     details=details,
                     detail_message=detail_message)
         elif history_event.event_type == EventType.WorkflowExecutionTimedOut:
             raise WorkflowExecutionTimedOutException()
         elif history_event.event_type == EventType.WorkflowExecutionTerminated:
             attributes = history_event.workflow_execution_terminated_event_attributes
             raise WorkflowExecutionTerminatedException(
                 reason=attributes.reason,
                 details=attributes.details,
                 identity=attributes.identity)
         elif history_event.event_type == EventType.WorkflowExecutionCanceled:
             raise WorkflowExecutionCanceledException()
         else:
             raise Exception("Unexpected history close event: " +
                             str(history_event))
def test_serialize_deserialize_exception():
    try:
        a()
    except TestException as e:
        ex = e

    details = serialize_exception(ex)
    details_dict = json.loads(details)
    assert details_dict[
        "class"] == "cadence.tests.test_exception_handling.TestException"
    assert details_dict["args"] == ["here"]
    assert details_dict["traceback"]
    assert details_dict["source"] == "cadence-python"

    dex = deserialize_exception(details)
    assert type(dex) == TestException
    assert repr(dex) == repr(ex)
    assert dex.__traceback__
def test_deserialize_unknown_exception():
    details_dict = {"class": "java.lang.Exception"}
    details = json.dumps(details_dict)
    exception = deserialize_exception(details)
    assert isinstance(exception, ExternalException)
    assert exception.details == details_dict
Esempio n. 6
0
 def get_cause(self):
     if self.cause:
         return deserialize_exception(self.cause)
     else:
         return None
Esempio n. 7
0
 def set_cause(self):
     if self.cause:
         cause_ex = deserialize_exception(self.cause)
         self.__cause__ = cause_ex