2025-02-24 19:22:01 +03:00
|
|
|
<?php
|
|
|
|
namespace taskman;
|
|
|
|
use Exception;
|
2025-02-25 18:05:12 +03:00
|
|
|
use Throwable;
|
2025-02-24 19:22:01 +03:00
|
|
|
|
|
|
|
task('config_worker', function(array $args)
|
|
|
|
{
|
|
|
|
if(sizeof($args) != 3)
|
|
|
|
throw new Exception("Config worker args not set");
|
|
|
|
|
|
|
|
$in_file = $args[0];
|
|
|
|
$out_file = $args[1];
|
|
|
|
$err_file = $args[2];
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
list($params, $job) = unserialize(ensure_read($in_file));
|
|
|
|
$result = _config_worker_func($params, $job);
|
|
|
|
ensure_write($out_file, serialize($result));
|
|
|
|
}
|
2025-02-25 18:05:12 +03:00
|
|
|
catch(Throwable $e)
|
2025-02-24 19:22:01 +03:00
|
|
|
{
|
|
|
|
//NOTE: explicitely catching all exceptions and writing to the error file
|
|
|
|
// since under Windows error file stream redirect may work unreliably
|
|
|
|
file_put_contents($err_file, $e->getMessage() . "\n" . $e->getTraceAsString(), FILE_APPEND);
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function _config_worker_run_procs(ConfigFetchParams $params, array $jobs, bool $serial) : array
|
|
|
|
{
|
|
|
|
if($serial)
|
|
|
|
{
|
|
|
|
$results_by_job = array();
|
|
|
|
foreach($jobs as $job)
|
2025-02-27 01:35:16 +03:00
|
|
|
$results_by_job[] = _config_worker_func($params, $job, $serial);
|
2025-02-24 19:22:01 +03:00
|
|
|
return $results_by_job;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-02-27 18:43:59 +03:00
|
|
|
//initializing worker for master process anyway
|
|
|
|
$params->globals->initWorker(true);
|
|
|
|
|
2025-02-27 15:53:16 +03:00
|
|
|
$workers_args = array();
|
|
|
|
foreach($jobs as $job)
|
|
|
|
$workers_args[] = array($params, $job);
|
2025-02-24 19:22:01 +03:00
|
|
|
|
2025-02-27 15:53:16 +03:00
|
|
|
return run_background_gamectl_workers('config_worker', $workers_args);
|
2025-02-24 19:22:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-25 18:05:12 +03:00
|
|
|
//returns [[base_dir, file, cache_file, was_stale, parser_type, error], ...]
|
2025-02-27 01:35:16 +03:00
|
|
|
function _config_worker_func(ConfigFetchParams $params, array $job, bool $is_master_proc = false) : array
|
2025-02-24 19:22:01 +03:00
|
|
|
{
|
|
|
|
$start_time = microtime(true);
|
|
|
|
|
2025-02-27 01:35:16 +03:00
|
|
|
$params->globals->initWorker($is_master_proc);
|
2025-02-24 19:22:01 +03:00
|
|
|
|
|
|
|
list($idx, $start_time, $chunk) = $job;
|
|
|
|
if($params->verbose)
|
2025-02-25 15:48:28 +03:00
|
|
|
config_log("Worker $idx (" . sizeof($chunk) . ") started (".round(microtime(true)-$start_time, 2)." sec)");
|
2025-02-24 19:22:01 +03:00
|
|
|
|
|
|
|
$fast_parser_num = 0;
|
|
|
|
$results = array();
|
|
|
|
foreach($chunk as $file_idx => $chunk_data)
|
|
|
|
{
|
2025-02-25 18:05:12 +03:00
|
|
|
list($base_dir, $file) = $chunk_data;
|
|
|
|
|
2025-02-24 19:22:01 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if($params->verbose && $file_idx > 0 && ($file_idx % 500) == 0)
|
2025-02-25 15:48:28 +03:00
|
|
|
config_log("Worker $idx progress: " . round($file_idx / sizeof($chunk) * 100) . "% ...");
|
2025-02-24 19:22:01 +03:00
|
|
|
|
|
|
|
$cache_file = config_get_cache_path($params->globals, $file);
|
|
|
|
|
|
|
|
$parser_type = null;
|
|
|
|
|
|
|
|
$is_stale = true;
|
|
|
|
if(!$params->force_stale)
|
|
|
|
$is_stale = need_to_regen($cache_file, array($file));
|
|
|
|
|
|
|
|
if($is_stale)
|
|
|
|
_config_invalidate_cache($params, $base_dir, $file, $cache_file, $parser_type);
|
|
|
|
|
2025-02-25 18:05:12 +03:00
|
|
|
$results[] = array($base_dir, $file, $cache_file, $is_stale, $parser_type, null);
|
2025-02-24 19:22:01 +03:00
|
|
|
}
|
2025-02-25 18:05:12 +03:00
|
|
|
catch(Throwable $e)
|
2025-02-24 19:22:01 +03:00
|
|
|
{
|
2025-02-25 18:05:12 +03:00
|
|
|
$results[] = array($base_dir, $file, null, null, null, $e->getMessage());
|
2025-02-24 19:22:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($params->verbose)
|
2025-02-25 15:48:28 +03:00
|
|
|
config_log("Worker $idx done (".round(microtime(true)-$start_time, 2)." sec)");
|
2025-02-24 19:22:01 +03:00
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|