/** * Returns a cloned task with compiled input data * * @param TaskSchema $task * @return TaskSchema */ public function compileTaskInput(TaskSchema $task) { $compiled = clone $task; $input = $task->getInput(); // Parse special input types if (strlen($input) > 1) { $prefix = $input[0]; switch ($prefix) { case self::PREFIX_VARIABLE: $compiled->setInput($this->parseVariable(substr($input, 1))); break; case self::PREFIX_RESULT: $compiled->setInput($this->parseResult(substr($input, 1))); break; case self::PREFIX_STRING: $compiled->setInput(substr($input, 1)); break; default: break; } } // Pass-through the input factory if ($task->getInputFactory()) { $compiled->setInput(call_user_func_array($task->getInputFactory(), [$compiled->getInput(), $this->history, $this->memory_pool])); } return $compiled; }
/** * Execute a task * * @param TaskSchema $task * @param WorkEvent $event */ protected function executeTask(TaskSchema $task, WorkEvent $event) { $memory_pool = $this->getWorkflow()->getJailMemoryPool() ? JailedMemoryPool::jail($this->getMemoryPool(), ':' . $event->getExecutionId()) : $this->getMemoryPool(); $class = $task->getClass(); /** @var TaskInterface $obj */ $obj = new $class($memory_pool, $event->getInput(), $this->getAuxPayload()); if (!$obj instanceof TaskInterface) { throw new \DomainException("Class for task " . $task->getActivityName() . " is not a TaskInterface"); } $obj->execute($event); }
/** * Get the count of a task in the history in a given state * * @param TaskSchema $task * @param HistoryItemState $state * @return int */ public function countTask(TaskSchema $task, HistoryItemState $state) { $cache_key = 'T:' . $task->getActivityName() . ':' . $task->getActivityVersion() . ':' . $state->key(); if (array_key_exists($cache_key, $this->completion_cache)) { return $this->completion_cache[$cache_key]; } $count = 0; foreach ($this->history as $history_item) { /** @var WorkflowHistoryItem $history_item */ if ($task->getActivityName() == $history_item->getActivityName() && $task->getActivityVersion() == $history_item->getActivityVersion() && $history_item->getState() == $state) { $count++; } } $this->completion_cache[$cache_key] = $count; return $count; }
/** * Get all task schemas in the workflow * * @return TaskSchema[] */ public function getTasks() { $schemas = $this->getSchemaProperty('tasks', null, true); $tasks = []; foreach ($schemas as $task_key => $schema) { $parts = TaskSchema::fromKey($task_key); $tasks[] = TaskSchema::fromArray($schema, $parts->getActivityName(), $parts->getActivityVersion()); } return $tasks; }
/** * Create a task schema from an array of schema properties * * @param array $arr * @param string $name * @param string $version * @return TaskSchema */ public static function fromArray(array $arr, $name, $version) { $vars = ['requires' => [], 'retry' => 0, 'class' => null, 'input' => null, 'input_factory' => null, 'tasklist' => null, 'control' => null, 'schedule_to_close_timeout' => null, 'schedule_to_start_timeout' => null, 'start_to_close_timeout' => null, 'heartbeat_timeout' => null]; $schema = new TaskSchema(); $schema->setActivityName($name); $schema->setActivityVersion($version); foreach ($vars as $key => $default) { $fn = self::snakeToCamel('set_' . $key); $value = array_key_exists($key, $arr) ? $arr[$key] : $default; if (is_array($default) && !is_array($value)) { if ($value === null) { $value = []; } else { $value = [$value]; } } $schema->{$fn}($value); } return $schema; }
public function testCounts() { $history = new WorkflowHistory(); $history->add($this->createHistoryItem('test-activity', '1', HistoryItemState::COMPLETED(), 'alpha')); $history->add($this->createHistoryItem('test-activity', '1', HistoryItemState::COMPLETED(), 'bravo')); $history->add($this->createHistoryItem('test-activity', '2', HistoryItemState::COMPLETED(), 'charlie')); $history->add($this->createHistoryItem('test-activity', '1', HistoryItemState::SCHEDULED(), 'delta')); $history->add($this->createHistoryItem('other-activity', '1', HistoryItemState::COMPLETED(), 'echo')); $history->add($this->createHistoryItem('other-activity', '1', HistoryItemState::RUNNING(), 'foxtrot')); $inspector = new HistoryInspector($history); $schema_test = TaskSchema::fromKey('test-activity/1'); $schema_other = TaskSchema::fromKey('other-activity/1'); $this->assertEquals(0, $inspector->countTask($schema_test, HistoryItemState::RUNNING())); $this->assertEquals(0, $inspector->countTask($schema_test, HistoryItemState::FAILED())); $this->assertEquals(2, $inspector->countTask($schema_test, HistoryItemState::COMPLETED())); $this->assertEquals(1, $inspector->countTask($schema_test, HistoryItemState::SCHEDULED())); $this->assertEquals(1, $inspector->countTask($schema_other, HistoryItemState::COMPLETED())); $this->assertEquals(1, $inspector->countTask($schema_other, HistoryItemState::RUNNING())); $this->assertEquals(0, $inspector->countTask($schema_other, HistoryItemState::SCHEDULED())); $this->assertEquals(0, $inspector->countTask($schema_other, HistoryItemState::FAILED())); $this->assertEquals(1, $inspector->countControl('alpha', HistoryItemState::COMPLETED())); $this->assertEquals(1, $inspector->countControl('bravo', HistoryItemState::COMPLETED())); $this->assertEquals(1, $inspector->countControl('charlie', HistoryItemState::COMPLETED())); $this->assertEquals(0, $inspector->countControl('delta', HistoryItemState::COMPLETED())); $this->assertEquals(1, $inspector->countControl('echo', HistoryItemState::COMPLETED())); $this->assertEquals(0, $inspector->countControl('foxtrot', HistoryItemState::COMPLETED())); $this->assertTrue($inspector->haveOpenActivities()); $alpha = new TaskSchema(); $alpha->setActivityName('test-activity')->setActivityVersion('1')->setControl('alpha'); $this->assertEquals(2, $inspector->countTask($alpha, HistoryItemState::COMPLETED())); $this->assertEquals(1, $inspector->countTask($alpha, HistoryItemState::SCHEDULED())); $omega = new TaskSchema(); $omega->setActivityName('test-activity')->setActivityVersion('10'); $this->assertEquals(0, $inspector->countTask($omega, HistoryItemState::COMPLETED())); $this->assertTrue($inspector->hasTaskBeenScheduled($alpha)); $this->assertFalse($inspector->hasTaskBeenScheduled($omega)); }
/** * Check if we meet all activity requirements * * @param array $requirements * @return bool */ protected function meetsActivityRequirements(array $requirements) { foreach ($requirements as $activity) { $schema = TaskSchema::fromKey($activity); if ($this->history_inspector->countTask($schema, HistoryItemState::COMPLETED()) < 1) { return false; } } return true; }