Ejemplo n.º 1
0
def test_get_task_run_state(patch_posts, cloud_api, runner_token):
    query_resp = {
        "task_run_by_pk": {
            "serialized_state": {
                "type": "Pending",
                "_result": {
                    "type": "SafeResult",
                    "value": "42",
                    "result_handler": {
                        "type": "JSONResultHandler"
                    },
                },
                "message": None,
                "__version__": "0.3.3+310.gd19b9b7.dirty",
                "cached_inputs": None,
            },
        }
    }

    post = patch_posts([dict(data=query_resp)])

    client = Client()
    state = client.get_task_run_state(task_run_id="72-salt")
    assert isinstance(state, Pending)
    assert state.result == "42"
    assert state.message is None
Ejemplo n.º 2
0
def create_prefect_flow_run(flow_name: str, project_name: str, task_refs: List,
                            params: Mapping) -> str:
    """Creates new prefect flow run for given flow id, parameters, task references
    and API server URL to send GraphQL requests to.
    Returns results value and state from a Prefect flow run.
    """

    try:
        flow_run = StartFlowRun(flow_name=flow_name,
                                project_name=project_name,
                                parameters=params)
        flow_run_id = flow_run.run()
        client = Client()
        while True:
            time.sleep(10)
            flow_run_info = client.get_flow_run_info(flow_run_id)
            flow_state = flow_run_info.state
            task_runs_info = flow_run_info.task_runs
            if flow_state.is_finished():
                task_res_locs = {}
                for task_run in task_runs_info:
                    # Return ref if ref string is a substring of any task slug
                    ref = next((ref_str for ref_str in task_refs
                                if ref_str in task_run.task_slug), None)
                    if ref:
                        task_id = task_run.id
                        task_state = client.get_task_run_state(task_id)
                        task_res_locs[ref] = task_state._result.location
                task_results = {}
                for ref, loc in task_res_locs.items():
                    local_res = LocalResult()
                    result = local_res.read(loc)
                    task_results[ref] = result.value
                return task_results, flow_state, task_res_locs
    except ValueError as err:
        raise err