Improving startup times for before and after task dependencies
Publish PHP Package / docker (push) Successful in 6s Details

This commit is contained in:
Pavel Shevaev 2025-02-24 19:04:26 +03:00
parent 71205818be
commit 37c915e72e
1 changed files with 46 additions and 44 deletions

View File

@ -48,6 +48,10 @@ class TaskmanTask
private $is_running = false; private $is_running = false;
private $has_run = array(); private $has_run = array();
private $args = 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()) function __construct(\Closure $func, $name, $props = array())
{ {
@ -60,25 +64,6 @@ class TaskmanTask
$this->props = $props; $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() function getName()
{ {
return $this->name; return $this->name;
@ -104,7 +89,14 @@ class TaskmanTask
return $this->args; 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", ""); $alias = $this->getPropOr("alias", "");
if(is_array($alias)) if(is_array($alias))
@ -143,8 +135,8 @@ class TaskmanTask
if(!$TASKMAN_NO_DEPS) if(!$TASKMAN_NO_DEPS)
{ {
run_many($this->_getBeforeDeps()); run_many($this->before_deps);
run_many($this->_getDeps()); run_many($this->getDeps());
} }
$TASKMAN_CURRENT_TASK = $this; $TASKMAN_CURRENT_TASK = $this;
@ -155,7 +147,7 @@ class TaskmanTask
array_pop($TASKMAN_STACK); array_pop($TASKMAN_STACK);
if(!$TASKMAN_NO_DEPS) if(!$TASKMAN_NO_DEPS)
run_many($this->_getAfterDeps()); run_many($this->after_deps);
msg_sys("***** ".str_repeat('-', $level)."task '" . $this->getName() . "' done(" . msg_sys("***** ".str_repeat('-', $level)."task '" . $this->getName() . "' done(" .
round(microtime(true)-$bench,2) . '/' .round(microtime(true)-$TASKMAN_START_TIME,2) . " sec.) *****\n"); round(microtime(true)-$bench,2) . '/' .round(microtime(true)-$TASKMAN_START_TIME,2) . " sec.) *****\n");
@ -175,34 +167,26 @@ class TaskmanTask
return $task_result; 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(); if($this->deps === null)
foreach(get_tasks() as $task_obj) $this->deps = $this->_parseDeps();
{ return $this->deps;
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;
} }
private function _getDeps() private function _parseDeps() : array
{ {
$deps = $this->getPropOr('deps', ""); $deps = $this->getPropOr("deps", "");
if(is_array($deps)) if(is_array($deps))
return $deps; return $deps;
else if($deps && is_string($deps)) else if($deps && is_string($deps))
@ -280,7 +264,25 @@ function _collect_tasks()
} }
foreach($TASKMAN_TASKS as $task) 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() function _get_task_candidates()
@ -302,7 +304,7 @@ function _get_task_candidates()
return $cands; return $cands;
} }
function get_task($task) function get_task(string $task) : TaskmanTask
{ {
global $TASKMAN_TASKS; global $TASKMAN_TASKS;
global $TASKMAN_TASK_ALIASES; global $TASKMAN_TASK_ALIASES;
@ -387,7 +389,7 @@ function task($name)
$TASKMAN_CLOSURES[$name] = $args; $TASKMAN_CLOSURES[$name] = $args;
} }
function get_tasks() function get_tasks() : array
{ {
global $TASKMAN_TASKS; global $TASKMAN_TASKS;
return $TASKMAN_TASKS; return $TASKMAN_TASKS;