From 37c915e72eed9ff40efc3ccf6a4d3a026173cb2b Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Mon, 24 Feb 2025 19:04:26 +0300 Subject: [PATCH] Improving startup times for before and after task dependencies --- taskman.inc.php | 90 +++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/taskman.inc.php b/taskman.inc.php index 19c6803..2a27f60 100644 --- a/taskman.inc.php +++ b/taskman.inc.php @@ -48,6 +48,10 @@ class TaskmanTask private $is_running = false; private $has_run = array(); private $args = array(); + private $deps = null; + private $aliases = null; + private $before_deps = array(); + private $after_deps = array(); function __construct(\Closure $func, $name, $props = array()) { @@ -60,25 +64,6 @@ class TaskmanTask $this->props = $props; } - function validate() - { - try - { - foreach($this->_getBeforeDeps() as $dep_task) - get_task($dep_task); - - foreach($this->_getDeps() as $dep_task) - get_task($dep_task); - - foreach($this->_getAfterDeps() as $dep_task) - get_task($dep_task); - } - catch(Exception $e) - { - throw new Exception("Task '{$this->name}' validation error: " . $e->getMessage()); - } - } - function getName() { return $this->name; @@ -104,7 +89,14 @@ class TaskmanTask return $this->args; } - function getAliases() + function getAliases() : array + { + if($this->aliases === null) + $this->aliases = $this->_parseAliases(); + return $this->aliases; + } + + function _parseAliases() : array { $alias = $this->getPropOr("alias", ""); if(is_array($alias)) @@ -143,8 +135,8 @@ class TaskmanTask if(!$TASKMAN_NO_DEPS) { - run_many($this->_getBeforeDeps()); - run_many($this->_getDeps()); + run_many($this->before_deps); + run_many($this->getDeps()); } $TASKMAN_CURRENT_TASK = $this; @@ -155,7 +147,7 @@ class TaskmanTask array_pop($TASKMAN_STACK); if(!$TASKMAN_NO_DEPS) - run_many($this->_getAfterDeps()); + run_many($this->after_deps); msg_sys("***** ".str_repeat('-', $level)."task '" . $this->getName() . "' done(" . round(microtime(true)-$bench,2) . '/' .round(microtime(true)-$TASKMAN_START_TIME,2) . " sec.) *****\n"); @@ -175,34 +167,26 @@ class TaskmanTask return $task_result; } - private function _getBeforeDeps() + function addBeforeDep($task) { - return $this->_collectRelatedTasks("before"); + $this->before_deps[] = $task; } - private function _getAfterDeps() + function addAfterDep($task) { - return $this->_collectRelatedTasks("after"); + $this->after_deps[] = $task; } - private function _collectRelatedTasks($prop_name) + function getDeps() : array { - $arr = array(); - foreach(get_tasks() as $task_obj) - { - if($this->getName() == $task_obj->getName()) - continue; - - $value = $task_obj->getPropOr($prop_name, ""); - if($value == $this->getName() || in_array($value, $this->getAliases())) - $arr[] = $task_obj; - } - return $arr; + if($this->deps === null) + $this->deps = $this->_parseDeps(); + return $this->deps; } - private function _getDeps() + private function _parseDeps() : array { - $deps = $this->getPropOr('deps', ""); + $deps = $this->getPropOr("deps", ""); if(is_array($deps)) return $deps; else if($deps && is_string($deps)) @@ -280,7 +264,25 @@ function _collect_tasks() } foreach($TASKMAN_TASKS as $task) - $task->validate(); + { + try + { + $before = $task->getPropOr("before", ""); + if($before) + get_task($before)->addBeforeDep($task); + + $after = $task->getPropOr("after", ""); + if($after) + get_task($after)->addAfterDep($task); + + foreach($task->getDeps() as $dep_task) + get_task($dep_task); + } + catch(Exception $e) + { + throw new Exception("Task '{$task->getName()}' validation error: " . $e->getMessage()); + } + } } function _get_task_candidates() @@ -302,7 +304,7 @@ function _get_task_candidates() return $cands; } -function get_task($task) +function get_task(string $task) : TaskmanTask { global $TASKMAN_TASKS; global $TASKMAN_TASK_ALIASES; @@ -387,7 +389,7 @@ function task($name) $TASKMAN_CLOSURES[$name] = $args; } -function get_tasks() +function get_tasks() : array { global $TASKMAN_TASKS; return $TASKMAN_TASKS;