/** * Process a workflow decision, sending the reason back to the workflow engine * * @param Decision $decision */ public function processDecision(Decision $decision) { switch ($decision->getWorkflowResult()) { // Complete a workflow case WorkflowResult::COMPLETE(): $class = 'RespondDecisionCompleteCommand'; $event = new CompletingWorkflowEvent($this->getWorkflow(), $decision->getExecutionId(), $decision->getResult()); $this->dispatch(Event::DECISION_COMPLETE, $event); $this->getWorkflow()->onWorkflowSuccess($event); $this->getWorkflow()->onWorkflowComplete($event); break; // Fail a workflow // Fail a workflow case WorkflowResult::FAIL(): $class = 'RespondDecisionFailedCommand'; $event = new FailingWorkflowEvent($this->getWorkflow(), $decision->getExecutionId(), $decision->getReason()); $this->dispatch(Event::DECISION_FAIL, $event); $this->getWorkflow()->onWorkflowFailed($event); $this->getWorkflow()->onWorkflowComplete($event); break; // Send workflow commands // Send workflow commands case WorkflowResult::COMMAND(): $class = 'RespondDecisionScheduleCommand'; foreach ($decision->getScheduledTasks() as $task) { $this->dispatch(Event::DECISION_SCHEDULE, new SchedulingTaskEvent($task)); } break; // Unsupported response // Unsupported response default: throw new UnexpectedValueException("Unknown workflow result: " . $decision->getWorkflowResult()->key()); } $this->runCommand($class, ['decision' => $decision]); }
public function testActivityFail() { Conf::init(__DIR__ . '/../../../../config/'); $memory_pool = new RedisMemoryPool('decider-tests', 60, Conf::get('redis')); $decider = new Decider(); $decider->setWorkflow(new YamlWorkflow(__DIR__ . '/../Resources/TestSchema.yml')); $decider->setMemoryPool($memory_pool); // Workflow started - $event1 = new DecisionEvent(); $decider->processDecisionEvent($event1); $this->assertCount(1, $event1->getDecision()->getScheduledTasks()); $this->assertEquals(WorkflowResult::COMMAND(), $event1->getDecision()->getWorkflowResult()); $task = $event1->getDecision()->getScheduledTasks()[0]; $this->assertEquals('alpha', $task->getControl()); // Task 1 failed - $alpha = new WorkflowHistoryItem('1'); $alpha->setActivityName('test-activity')->setActivityVersion('1'); $alpha->setTimeScheduled(new \DateTime('2014-10-10 10:01:00')); $alpha->setTimeStarted(new \DateTime('2014-10-10 10:00:00')); $alpha->setTimeEnded(new \DateTime('2014-10-10 10:04:00')); $alpha->setState(HistoryItemState::FAILED()); $alpha->setErrorMessage('Test failure'); $alpha->setControl('alpha')->setInput('alpha')->setResult(">.<"); $event2 = new DecisionEvent(); $event2->getHistory()->add($alpha); $decider->processDecisionEvent($event2); $this->assertCount(0, $event2->getDecision()->getScheduledTasks()); $this->assertEquals(WorkflowResult::FAIL(), $event2->getDecision()->getWorkflowResult()); }
public function __construct($token, $execution_id) { $this->decision_token = $token; $this->execution_id = $execution_id; $this->workflow_result = WorkflowResult::COMMAND(); }
/** * Process the decision event, generating a decision response (included in the DecisionEvent) * * @param DecisionEvent $event */ public function processDecisionEvent(DecisionEvent $event) { $history = $event->getHistory(); $decision = $event->getDecision(); $tasks = $this->getWorkflow()->getTasks(); // Create a memory pool jailed to this execution $memory_pool = $this->getWorkflow()->getJailMemoryPool() ? JailedMemoryPool::jail($this->getMemoryPool(), ':' . $event->getExecutionId()) : $this->getMemoryPool(); // Check if we need to execute any task events /** @var WorkflowHistoryItem $history_item */ foreach ($history as $history_item) { $id = $history_item->getEventId(); if ($history_item->getState() == HistoryItemState::COMPLETED()) { if ($memory_pool->get('history:' . $id . ':completed') === null) { $this->runTaskDecider($history_item, $decision, $memory_pool); $memory_pool->set('history:' . $id . ':completed', 1); } } } // Check if we need to fail if ($this->getFailOnActivityFailure() && $history->hasActivityFailure() || $history->hasWorkflowFailed()) { $decision->setWorkflowResult(WorkflowResult::FAIL()); $decision->setReason(implode(", ", $history->getErrorMessages())); return; } // Check if we need to schedule $parser = new InputParser($history, $memory_pool); $scheduler = new Scheduler($history, $memory_pool); foreach ($tasks as $task) { if ($scheduler->canScheduleTask($task)) { $decision->scheduledTask($parser->compileTaskInput($task)); } } // Check if we need to complete if (count($decision->getScheduledTasks()) == 0 && !$scheduler->haveOpenActivities()) { $decision->setWorkflowResult(WorkflowResult::COMPLETE()); } }