use of com.uber.cadence.PollForDecisionTaskResponse 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);
}
}
use of com.uber.cadence.PollForDecisionTaskResponse in project cadence-client by uber-java.
the class ReplayDecisionTaskHandler method createDecider.
private ReplayDecider createDecider(HistoryHelper historyHelper) throws Exception {
PollForDecisionTaskResponse decisionTask = historyHelper.getDecisionTask();
WorkflowType workflowType = decisionTask.getWorkflowType();
DecisionsHelper decisionsHelper = new DecisionsHelper(decisionTask);
ReplayWorkflow workflow = workflowFactory.getWorkflow(workflowType);
return new ReplayDecider(domain, workflow, historyHelper, decisionsHelper);
}
use of com.uber.cadence.PollForDecisionTaskResponse in project cadence-client by uber-java.
the class TestWorkflowMutableStateImpl method query.
@Override
public QueryWorkflowResponse query(QueryWorkflowRequest queryRequest) throws TException {
QueryId queryId = new QueryId(executionId);
PollForDecisionTaskResponse task = new PollForDecisionTaskResponse().setTaskToken(queryId.toBytes()).setWorkflowExecution(executionId.getExecution()).setWorkflowType(startRequest.getWorkflowType()).setQuery(queryRequest.getQuery());
TaskListId taskListId = new TaskListId(queryRequest.getDomain(), startRequest.getTaskList().getName());
store.sendQueryTask(executionId, taskListId, task);
CompletableFuture<QueryWorkflowResponse> result = new CompletableFuture<>();
queries.put(queryId.getQueryId(), result);
try {
return result.get();
} catch (InterruptedException e) {
return new QueryWorkflowResponse();
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof TException) {
throw (TException) cause;
}
throw new InternalServiceError(Throwables.getStackTraceAsString(cause));
}
}
use of com.uber.cadence.PollForDecisionTaskResponse in project cadence-client by uber-java.
the class TestWorkflowStoreImpl method save.
@Override
public long save(RequestContext ctx) throws InternalServiceError, EntityNotExistsError {
long result;
lock.lock();
boolean historiesEmpty = histories.isEmpty();
try {
ExecutionId executionId = ctx.getExecutionId();
HistoryStore history = histories.get(executionId);
List<HistoryEvent> events = ctx.getEvents();
if (history == null) {
if (events.isEmpty() || events.get(0).getEventType() != EventType.WorkflowExecutionStarted) {
throw new IllegalStateException("No history found for " + executionId);
}
history = new HistoryStore(executionId, lock);
histories.put(executionId, history);
}
history.checkNextEventId(ctx.getInitialEventId());
history.addAllLocked(events, ctx.currentTimeInNanoseconds());
result = history.getNextEventIdLocked();
timerService.updateLocks(ctx.getTimerLocks());
ctx.fireCallbacks(history.getEventsLocked().size());
} finally {
if (historiesEmpty && !histories.isEmpty()) {
// Initially locked in the constructor
timerService.unlockTimeSkipping();
}
lock.unlock();
}
// Push tasks to the queues out of locks
DecisionTask decisionTask = ctx.getDecisionTask();
if (decisionTask != null) {
BlockingQueue<PollForDecisionTaskResponse> decisionsQueue = getDecisionTaskListQueue(decisionTask.getTaskListId());
decisionsQueue.add(decisionTask.getTask());
}
List<ActivityTask> activityTasks = ctx.getActivityTasks();
if (activityTasks != null) {
for (ActivityTask activityTask : activityTasks) {
BlockingQueue<PollForActivityTaskResponse> activitiesQueue = getActivityTaskListQueue(activityTask.getTaskListId());
activitiesQueue.add(activityTask.getTask());
}
}
List<Timer> timers = ctx.getTimers();
if (timers != null) {
for (Timer t : timers) {
timerService.schedule(Duration.ofSeconds(t.getDelaySeconds()), t.getCallback());
}
}
return result;
}
use of com.uber.cadence.PollForDecisionTaskResponse in project cadence-client by uber-java.
the class TestWorkflowService method PollForDecisionTask.
@Override
public PollForDecisionTaskResponse PollForDecisionTask(PollForDecisionTaskRequest pollRequest) throws BadRequestError, InternalServiceError, ServiceBusyError, TException {
PollForDecisionTaskResponse task;
try {
task = store.pollForDecisionTask(pollRequest);
} catch (InterruptedException e) {
return new PollForDecisionTaskResponse();
}
ExecutionId executionId = new ExecutionId(pollRequest.getDomain(), task.getWorkflowExecution());
TestWorkflowMutableState mutableState = getMutableState(executionId);
try {
mutableState.startDecisionTask(task, pollRequest);
return task;
} catch (EntityNotExistsError e) {
if (log.isDebugEnabled()) {
log.debug("Skipping outdated decision task for " + executionId, e);
}
// skip the task
}
return task;
}
Aggregations