358 lines
10 KiB
PHP
358 lines
10 KiB
PHP
<?php
|
|
namespace taskman;
|
|
use Exception;
|
|
|
|
require_once(__DIR__ . '/task.inc.php');
|
|
require_once(__DIR__ . '/scan.inc.php');
|
|
require_once(__DIR__ . '/cache.inc.php');
|
|
require_once(__DIR__ . '/pack.inc.php');
|
|
require_once(__DIR__ . '/util.inc.php');
|
|
require_once(__DIR__ . '/msgpack.inc.php');
|
|
|
|
class ConfigGlobals
|
|
{
|
|
public array $base_dirs = array();
|
|
public ?string $worker_init_fn = null;
|
|
public string $base_class = '\ConfBase';
|
|
public string $build_dir;
|
|
|
|
function __construct(array $base_dirs, string $build_dir, ?string $worker_init_fn = null)
|
|
{
|
|
$this->base_dirs = array_map(fn($path) => normalize_path($path), $base_dirs);
|
|
$this->build_dir = $build_dir;
|
|
$this->worker_init_fn = $worker_init_fn;
|
|
}
|
|
|
|
function initWorker(bool $is_master_proc)
|
|
{
|
|
$GLOBALS['CONFIG_GLOBALS'] = $this;
|
|
|
|
if($this->worker_init_fn !== null)
|
|
{
|
|
$fn = $this->worker_init_fn;
|
|
$fn($is_master_proc);
|
|
}
|
|
}
|
|
}
|
|
|
|
enum ConfigUpdateMode : int
|
|
{
|
|
case Full = 1;
|
|
case RelativeToBundle = 2;
|
|
case ChangedOnly = 3;
|
|
}
|
|
|
|
class ConfigManager
|
|
{
|
|
private ConfigGlobals $globals;
|
|
private int $workers_num;
|
|
|
|
private ConfigCache $cache;
|
|
private ?ConfigCacheFileMap $file_map;
|
|
|
|
function __construct(ConfigGlobals $globals, int $workers_num)
|
|
{
|
|
$this->globals = $globals;
|
|
$this->workers_num = $workers_num;
|
|
|
|
$this->cache = new ConfigCache($globals);
|
|
$this->file_map = null;
|
|
}
|
|
|
|
function getGlobals() : ConfigGlobals
|
|
{
|
|
return $this->globals;
|
|
}
|
|
|
|
function getCache() : ConfigCache
|
|
{
|
|
return $this->cache;
|
|
}
|
|
|
|
function getArtifactFilesSpec() : array
|
|
{
|
|
return [$this->globals->base_dirs, ['.js']];
|
|
}
|
|
|
|
function getFileMap() : ConfigCacheFileMap
|
|
{
|
|
if($this->file_map === null)
|
|
{
|
|
$map = $this->_tryLoadMap();
|
|
if($map === null)
|
|
$map = $this->_makeMap($this->scanFiles(extension: '.js'));
|
|
$this->file_map = $map;
|
|
}
|
|
return $this->file_map;
|
|
}
|
|
|
|
function updateCache(
|
|
ConfigUpdateMode $update_mode,
|
|
ConfigDirFiles $input_files = null,
|
|
?string $result_bundle_file = null,
|
|
bool $verbose = false
|
|
) : ConfigCacheUpdateResult
|
|
{
|
|
config_log("Updating cache, mode {$update_mode->value}...");
|
|
|
|
if($input_files === null && $update_mode === ConfigUpdateMode::ChangedOnly)
|
|
throw new Exception("input_files argument is required for ChangedOnly mode");
|
|
|
|
if($input_files === null)
|
|
$input_files = $this->scanFiles(extension: '.js', verbose: $verbose);
|
|
|
|
$added_files = [];
|
|
$removed_files = [];
|
|
$fs_cache_map = $this->_checkFileMap(
|
|
$update_mode,
|
|
$input_files,
|
|
$added_files,
|
|
$removed_files
|
|
);
|
|
|
|
$affected_files = $this->_getAffectedFiles(
|
|
$update_mode,
|
|
$result_bundle_file,
|
|
$fs_cache_map,
|
|
$input_files,
|
|
$removed_files
|
|
);
|
|
|
|
//NOTE: at this poine taking into account only config files
|
|
$affected_files->filter(fn($file) => str_ends_with($file, '.conf.js'));
|
|
|
|
$update_params = new ConfigCacheUpdateParams(
|
|
globals: $this->globals,
|
|
affected_files: $affected_files,
|
|
verbose: $verbose,
|
|
max_workers: $this->workers_num
|
|
);
|
|
$update_result = config_cache_update($update_params);
|
|
|
|
$this->cache->clear();
|
|
|
|
//TODO: traverse all affected files and update file map
|
|
foreach($affected_files as $file)
|
|
{
|
|
$cache_entry = $this->cache->getOrLoadByPath($file);
|
|
$fs_cache_map->updateDepsForEntry($cache_entry);
|
|
}
|
|
|
|
if($affected_files->count() > 0 || $added_files || $removed_files)
|
|
{
|
|
$fs_cache_map->update($added_files, $removed_files);
|
|
$this->_saveMap($fs_cache_map);
|
|
}
|
|
|
|
return $update_result;
|
|
}
|
|
|
|
private function _checkFileMap(ConfigUpdateMode $update_mode, ConfigDirFiles $input_files, array &$added_files, array &$removed_files) : ConfigCacheFileMap
|
|
{
|
|
$fs_cache_map = $this->file_map;
|
|
//NOTE: if there's no map so far we need to create one
|
|
if($fs_cache_map === null)
|
|
{
|
|
//let's re-use the input files
|
|
if($update_mode === ConfigUpdateMode::Full || $update_mode === ConfigUpdateMode::RelativeToBundle)
|
|
$tmp_files = $input_files;
|
|
else
|
|
$tmp_files = $this->scanFiles(extension: '.js');
|
|
|
|
$fs_cache_map = $this->_makeMap($tmp_files);
|
|
$this->file_map = $fs_cache_map;
|
|
}
|
|
|
|
if($update_mode === ConfigUpdateMode::Full || $update_mode === ConfigUpdateMode::RelativeToBundle)
|
|
{
|
|
list($added_files, $removed_files) = $fs_cache_map->compare($input_files->getAllFiles());
|
|
config_log("File map added: ".count($added_files).", removed: ".count($removed_files));
|
|
}
|
|
|
|
return $fs_cache_map;
|
|
}
|
|
|
|
private function _getAffectedFiles(ConfigUpdateMode $update_mode, ?string $result_bundle_file, ConfigCacheFileMap $fs_cache_map, ConfigDirFiles $input_files, array $removed_files) : ConfigDirFiles
|
|
{
|
|
$affected_files = null;
|
|
|
|
if($update_mode === ConfigUpdateMode::Full)
|
|
{
|
|
$affected_files = $input_files;
|
|
}
|
|
else if($update_mode === ConfigUpdateMode::RelativeToBundle)
|
|
{
|
|
if($result_bundle_file === null)
|
|
throw new Exception("result_bundle_file argument is required");
|
|
|
|
$affected_files = $this->newDirFiles();
|
|
|
|
foreach($input_files->getMap() as $base_dir => $files)
|
|
{
|
|
foreach($files as $file)
|
|
{
|
|
if(need_to_regen($result_bundle_file, [$file]))
|
|
{
|
|
$affected_files->add($base_dir, $file);
|
|
|
|
$affected_by_file = $fs_cache_map->getAffectedFiles($file);
|
|
foreach($affected_by_file as $dep)
|
|
$affected_files->addFile($dep, unique: true);
|
|
}
|
|
}
|
|
}
|
|
|
|
//if there were removed files we need to rebuild affected files
|
|
foreach($removed_files as $file)
|
|
{
|
|
if(!str_ends_with($file, '.conf.js'))
|
|
{
|
|
$affected_by_file = $fs_cache_map->getAffectedFiles($file);
|
|
foreach($affected_by_file as $dep)
|
|
$affected_files->addFile($dep, unique: true);
|
|
}
|
|
else
|
|
{
|
|
//TODO: in case config file was removed do we actually need to rebuild all configs?
|
|
}
|
|
}
|
|
}
|
|
else
|
|
throw new Exception("Unknown update mode: {$update_mode->value}");
|
|
|
|
return $affected_files;
|
|
}
|
|
|
|
function newDirFiles() : ConfigDirFiles
|
|
{
|
|
return new ConfigDirFiles([], $this->globals->base_dirs);
|
|
}
|
|
|
|
function parseFile(string $file) : ConfigParseResult
|
|
{
|
|
return config_parse($this->globals->base_dirs, $file);
|
|
}
|
|
|
|
function scanFiles(string $extension = '.conf.js', bool $verbose = false) : ConfigDirFiles
|
|
{
|
|
return config_scan_files($this->globals->base_dirs, $extension, $verbose);
|
|
}
|
|
|
|
private function _getMapPath() : string
|
|
{
|
|
return $this->globals->build_dir . '/file_map.data';
|
|
}
|
|
|
|
private function _tryLoadMap() : ?ConfigCacheFileMap
|
|
{
|
|
if(!is_file($this->_getMapPath()))
|
|
return null;
|
|
return ConfigCacheFileMap::unserialize(ensure_read($this->_getMapPath()));
|
|
}
|
|
|
|
private function _makeMap(ConfigDirFiles $files) : ConfigCacheFileMap
|
|
{
|
|
config_log("Creating file map");
|
|
$map = new ConfigCacheFileMap();
|
|
$map->update($files->getAllFiles(), []);
|
|
$this->_saveMap($map);
|
|
return $map;
|
|
}
|
|
|
|
private function _saveMap(ConfigCacheFileMap $map)
|
|
{
|
|
ensure_write($this->_getMapPath(), ConfigCacheFileMap::serialize($map));
|
|
}
|
|
}
|
|
|
|
function config_log($msg)
|
|
{
|
|
echo "[CFG] $msg\n";
|
|
}
|
|
|
|
function config_get_cache_path(ConfigGlobals $globals, string $file) : string
|
|
{
|
|
return config_get_tmp_build_path($globals, $file . '.cache');
|
|
}
|
|
|
|
function config_get_cache_id_path(ConfigGlobals $globals, int $id) : string
|
|
{
|
|
return config_get_tmp_build_path($globals, $id . '.id');
|
|
}
|
|
|
|
function config_get_cache_strid_path(ConfigGlobals $globals, string $strid) : string
|
|
{
|
|
return config_get_tmp_build_path($globals, $strid . '.strid');
|
|
}
|
|
|
|
function config_get_cache_payload_path(ConfigGlobals $globals, string $file) : string
|
|
{
|
|
return config_get_tmp_build_path($globals, $file . '.payload');
|
|
}
|
|
|
|
function config_load(ConfigGlobals $globals, string $conf_path, ?string $conf_dir = null) : object
|
|
{
|
|
$conf_dir ??= config_map_base_dir($globals->base_dirs, $conf_path);
|
|
|
|
list($conf_id, $_) = config_ensure_header($conf_dir, $conf_path);
|
|
if(!$conf_id)
|
|
throw new Exception("Bad conf id: {$conf_id}");
|
|
|
|
$pres = config_parse(array($conf_dir), $conf_path);
|
|
if($pres->error !== 0)
|
|
throw new Exception("Error({$pres->error}) while loading JSON from {$conf_path}:\n" . $pres->error_descr);
|
|
|
|
return config_load_from_kv_array($globals, $conf_dir, $conf_path, $pres->parsed_arr, $conf_id);
|
|
}
|
|
|
|
function config_load_from_kv_array(
|
|
ConfigGlobals $globals, string $conf_dir, string $file,
|
|
array $arr, ?int $id = null) : object
|
|
{
|
|
if(!isset($arr['class']) || !isset($arr['class'][0]))
|
|
throw new Exception("Class is not set in file '$file'.");
|
|
|
|
$id ??= config_file2id($conf_dir, $file);
|
|
|
|
$cnf = null;
|
|
try
|
|
{
|
|
list($klass, $class_id, $norm_arr) = config_apply_class_normalization($arr);
|
|
|
|
if(!class_exists($klass))
|
|
throw new Exception("No such class '$klass'");
|
|
$cnf = new $klass;
|
|
if(!is_a($cnf, $globals->base_class))
|
|
throw new Exception("'$klass' is not subclass of '".ltrim($globals->base_class, '\\')."'");
|
|
|
|
$cnf->import($norm_arr);
|
|
}
|
|
catch(Exception $e)
|
|
{
|
|
throw new Exception($e->getMessage() . " in file '{$file}'"/* . $e->getTraceAsString()*/);
|
|
}
|
|
|
|
$cnf->id = $id;
|
|
$cnf->strid = config_file2strid($conf_dir, $file);
|
|
|
|
return $cnf;
|
|
}
|
|
|
|
function config_bench_load(ConfigGlobals $globals, string $file)
|
|
{
|
|
$base_dir = config_map_base_dir($globals->base_dirs, $file);
|
|
|
|
list($conf_id, $_) = config_ensure_header($base_dir, $file);
|
|
if(!$conf_id)
|
|
throw new Exception("Bad conf id: {$conf_id}");
|
|
$t = microtime(true);
|
|
$parse_res = config_parse($globals->base_dirs, $file);
|
|
if($parse_res->error !== 0)
|
|
throw new Exception("Error({$parse_res->error}) while loading JSON from {$file}:\n" . $parse_res->error_descr);
|
|
config_log("Parse: " . (microtime(true) - $t));
|
|
$t = microtime(true);
|
|
$config = config_load_from_kv_array($globals, $base_dir, $file, $parse_res->parsed_arr, $conf_id);
|
|
config_log("Load: " . (microtime(true) - $t));
|
|
}
|
|
|