Search in sources :

Example 1 with RespondQueryTaskCompletedRequest

use of com.uber.cadence.RespondQueryTaskCompletedRequest in project cadence-client by uber-java.

the class ReplayDecisionTaskHandler method handleDecisionTaskImpl.

private Result handleDecisionTaskImpl(DecisionTaskWithHistoryIterator decisionTaskIterator) throws Throwable {
    HistoryHelper historyHelper = new HistoryHelper(decisionTaskIterator);
    ReplayDecider decider = createDecider(historyHelper);
    PollForDecisionTaskResponse decisionTask = historyHelper.getDecisionTask();
    if (decisionTask.isSetQuery()) {
        RespondQueryTaskCompletedRequest queryCompletedRequest = new RespondQueryTaskCompletedRequest();
        queryCompletedRequest.setTaskToken(decisionTask.getTaskToken());
        try {
            byte[] queryResult = decider.query(decisionTask.getQuery());
            queryCompletedRequest.setQueryResult(queryResult);
            queryCompletedRequest.setCompletedType(QueryTaskCompletedType.COMPLETED);
        } catch (Throwable e) {
            // TODO: Appropriate exception serialization.
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            queryCompletedRequest.setErrorMessage(sw.toString());
            queryCompletedRequest.setCompletedType(QueryTaskCompletedType.FAILED);
        }
        return new DecisionTaskHandler.Result(null, null, queryCompletedRequest, null);
    } else {
        decider.decide();
        DecisionsHelper decisionsHelper = decider.getDecisionsHelper();
        List<Decision> decisions = decisionsHelper.getDecisions();
        byte[] context = decisionsHelper.getWorkflowContextDataToReturn();
        if (log.isTraceEnabled()) {
            WorkflowExecution execution = decisionTask.getWorkflowExecution();
            log.trace("WorkflowTask startedEventId=" + decisionTask.getStartedEventId() + ", WorkflowID=" + execution.getWorkflowId() + ", RunID=" + execution.getRunId() + " completed with " + WorkflowExecutionUtils.prettyPrintDecisions(decisions));
        } else if (log.isDebugEnabled()) {
            WorkflowExecution execution = decisionTask.getWorkflowExecution();
            log.debug("WorkflowTask startedEventId=" + decisionTask.getStartedEventId() + ", WorkflowID=" + execution.getWorkflowId() + ", RunID=" + execution.getRunId() + " completed with " + decisions.size() + " new decisions");
        }
        RespondDecisionTaskCompletedRequest completedRequest = new RespondDecisionTaskCompletedRequest();
        completedRequest.setTaskToken(decisionTask.getTaskToken());
        completedRequest.setDecisions(decisions);
        completedRequest.setExecutionContext(context);
        return new DecisionTaskHandler.Result(completedRequest, null, null, null);
    }
}
Also used : PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) Decision(com.uber.cadence.Decision) StringWriter(java.io.StringWriter) RespondQueryTaskCompletedRequest(com.uber.cadence.RespondQueryTaskCompletedRequest) WorkflowExecution(com.uber.cadence.WorkflowExecution) RespondDecisionTaskCompletedRequest(com.uber.cadence.RespondDecisionTaskCompletedRequest) PrintWriter(java.io.PrintWriter)

Example 2 with RespondQueryTaskCompletedRequest

use of com.uber.cadence.RespondQueryTaskCompletedRequest in project cadence-client by uber-java.

the class WorkflowWorker method queryWorkflowExecution.

public byte[] queryWorkflowExecution(WorkflowExecution execution, String queryType, byte[] args) throws Exception {
    Iterator<HistoryEvent> history = WorkflowExecutionUtils.getHistory(service, domain, execution);
    DecisionTaskWithHistoryIterator historyIterator = new ReplayDecisionTaskWithHistoryIterator(execution, history);
    WorkflowQuery query = new WorkflowQuery();
    query.setQueryType(queryType).setQueryArgs(args);
    historyIterator.getDecisionTask().setQuery(query);
    DecisionTaskHandler.Result result = handler.handleDecisionTask(historyIterator);
    if (result.getQueryCompleted() != null) {
        RespondQueryTaskCompletedRequest r = result.getQueryCompleted();
        return r.getQueryResult();
    }
    throw new RuntimeException("Query returned wrong response: " + result);
}
Also used : WorkflowQuery(com.uber.cadence.WorkflowQuery) RespondQueryTaskCompletedRequest(com.uber.cadence.RespondQueryTaskCompletedRequest) HistoryEvent(com.uber.cadence.HistoryEvent)

Aggregations

RespondQueryTaskCompletedRequest (com.uber.cadence.RespondQueryTaskCompletedRequest)2 Decision (com.uber.cadence.Decision)1 HistoryEvent (com.uber.cadence.HistoryEvent)1 PollForDecisionTaskResponse (com.uber.cadence.PollForDecisionTaskResponse)1 RespondDecisionTaskCompletedRequest (com.uber.cadence.RespondDecisionTaskCompletedRequest)1 WorkflowExecution (com.uber.cadence.WorkflowExecution)1 WorkflowQuery (com.uber.cadence.WorkflowQuery)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1