Programming Language PHP
Namespace ZenCodex\ComposerMirror
Class Log
Method/Function warn
Total Examples 4
4 code examples of PHP ZenCodex\ComposerMirror\Log::warn extracted from open source projects
/*
|--------------------------------------------------------------------------
| Main()
|--------------------------------------------------------------------------
|
| 核心采集代码
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = App::getConfig();
// 检测并生成必要目录
file_exists($config->cachedir) or mkdir($config->cachedir, 0777, true);
file_exists($config->distdir) or mkdir($config->distdir, 0777, true);
$signal_handler = function ($signal) {
Log::warn('kill signal, please wait ...');
App::getInstance()->terminated = 1;
};
pcntl_signal(SIGINT, $signal_handler); // Ctrl + C
pcntl_signal(SIGCHLD, $signal_handler);
pcntl_signal(SIGTSTP, $signal_handler); // Ctrl + Z
// 临时 workaround
@exec('rm -f /tmp/composer_*');
if (App::getInstance()->isPush2Cloud) {
// 初始化 producer
$clientHandler = App::getClientHandler();
$stats = $clientHandler->stats();
if (intval($stats['current-jobs-ready']) > 0) {
Log::warn('还有未完成的jobs,继续等待');
sleep(30);
exit();
}
} else {
Log::warn('NOTE: WOULD NOT SYNC TO CLOUD');
}
// STEP 1
$providers = $this->downloadProviders($config);
if (FileUtils::badCountOfProviderPackages() !== 0) {
throw new \RuntimeException('!!! flushFiles => packages.json.new 存在错误,跳过更新 !!!');
}
// STEP 2
$jsonfiles = $this->downloadPackages($config, $providers);
// STEP 3
$this->downloadZipballs($config, $jsonfiles);
// STEP 4
$this->flushFiles($config);
//unset($globals->expiredManager);
Log::warn('wait for 60s....');
sleep(60);
}
/**
* composer.json downloader
*/
public function downloadPackages($config, $providers)
{
$cachedir = $config->cachedir;
$i = 1;
$numberOfProviders = count($providers);
$jsonfiles = [];
$packageObjs = [];
$app = App::getInstance();
$sum = 0;
foreach ($providers as $providerjson) {
$list = json_decode(file_get_contents($providerjson));
if (!$list || empty($list->providers)) {
continue;
}
$list = $list->providers;
// $all = count((array)$list);
// $progressBar = new ProgressBarManager(0, $all);
echo " - Provider {$i}/{$numberOfProviders}:\n";
// $progressBar->setFormat(" - Package: %current%/%max% [%bar%] %percent%%");
foreach ($list as $packageName => $provider) {
$app->terminated and exit();
if (in_array($packageName, self::IGNORE_PACKAGES)) {
Log::warn("Ignore package: $packageName");
continue;
}
// $progressBar->advance();
$sum++;
$url = "$config->packagistUrl/p/$packageName\$$provider->sha256.json";
$cachefile = $cachedir . str_replace("$config->packagistUrl/", '', $url);
if (file_exists($cachefile)) {
FileUtils::touchFile($cachefile, $app->timestamp);
continue;
}
$packageObjs[] = (object) [
'packageName' => $packageName,
'url' => $url,
'sha256' => $provider->sha256,
];
}
$i++;
}
Log::warn("Total packages count = $sum");
// 开始下载
$arrChuncks = array_chunk($packageObjs, $config->maxConnections);
$progressBar = new ProgressBarManager(0, count($packageObjs));
$progressBar->setFormat(' - New Packages: %current%/%max% [%bar%] %percent%%');
$client = new Client([RequestOptions::TIMEOUT => $config->timeout]);
foreach ($arrChuncks as $chunk) {
$requests = [];
foreach ($chunk as $package) {
App::getInstance()->terminated and exit();
$req = new Request('GET', $package->url);
$req->sha256 = $package->sha256;
$req->packageName = $package->packageName;
$requests[] = $req;
}
$pool = new Pool($client, $requests, [
'concurrency' => $config->maxConnections,
'fulfilled' => function ($res, $index) use (&$jsonfiles, &$requests, $progressBar) {
$config = App::getConfig();
$req = $requests[$index];
$cachedir = $config->cachedir;
$progressBar->advance();
if (200 !== $res->getStatusCode() || $req->sha256 !== hash('sha256', (string) $res->getBody())) {
Log::error("\t sha256 wrong => " . $req->getUri());
return;
}
$cachefile = $cachedir . str_replace("$config->packagistUrl/", '', $req->getUri());
// $cachefile2 = $cachedir . '/p/' . $req->packageName . '.json';
$jsonfiles[] = $cachefile;
// if ($glob = glob("{$cachedir}p/$req->packageName\$*")) {
// foreach ($glob as $old) {
// $globals->expiredManager->add($old, time());
// }
// }
FileUtils::storeFile($cachefile, (string) $res->getBody());
App::pushJob2Task($cachefile);
},
'rejected' => function ($reason, $index) use (&$requests, &$progressBar) {
Log::error($requests[$index]->getUri() . ' => failed');
$progressBar->advance();
},
]);
$pool->promise()->wait();
}
return $jsonfiles;
}
/*
|--------------------------------------------------------------------------
| Main()
|--------------------------------------------------------------------------
|
| 核心采集代码
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = App::getConfig();
// 检测并生成必要目录
file_exists($config->cachedir) or mkdir($config->cachedir, 0777, true);
file_exists($config->distdir) or mkdir($config->distdir, 0777, true);
$signal_handler = function ($signal) {
Log::warn('kill signal, please wait ...');
App::getInstance()->terminated = 1;
};
pcntl_signal(SIGINT, $signal_handler); // Ctrl + C
pcntl_signal(SIGCHLD, $signal_handler);
pcntl_signal(SIGTSTP, $signal_handler); // Ctrl + Z
// 临时 workaround
@exec('rm -f /tmp/composer_*');
if (App::getInstance()->isPush2Cloud) {
// 初始化 producer
$clientHandler = App::getClientHandler();
$stats = $clientHandler->stats();
if (intval($stats['current-jobs-ready']) > 0) {
Log::warn('还有未完成的jobs,继续等待');
sleep(30);
exit();
}
} else {
Log::warn('NOTE: WOULD NOT SYNC TO CLOUD');
}
// STEP 1
$providers = $this->downloadProviders($config);
if (FileUtils::badCountOfProviderPackages() !== 0) {
throw new \RuntimeException('!!! flushFiles => packages.json.new 存在错误,跳过更新 !!!');
}
// STEP 2
$jsonfiles = $this->downloadPackages($config, $providers);
// STEP 3
// $this->downloadZipballs($config, $jsonfiles);
// STEP 4
$this->flushFiles($config);
//unset($globals->expiredManager);
Log::warn('wait for 60s....');
sleep(60);
}
public function clearCloudDiffFiles()
{
$config = App::getConfig();
$cloud = new Cloud($config);
$i = $index = $startfrom = 0;
if (file_exists($config->dbdir . Rainbow::DIST_URI_MAP . '.log')) {
$startfrom = intval(file_get_contents($config->dbdir . Rainbow::DIST_URI_MAP . '.log'));
}
$mapUris = file($config->dbdir . Rainbow::DIST_URI_MAP, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($mapUris as $uri) {
$index++;
if ($index < $startfrom) {
continue;
}
$zipFile = $config->distdir . ltrim($uri, '/');
if (!file_exists($zipFile)) {
Log::info('local not found => ' . $zipFile);
$cloud->removeRemoteFile($zipFile);
file_put_contents($config->dbdir . Rainbow::DIST_URI_MAP . '.log', $index);
$i++;
}
}
Log::warn("$i files removed from cloud");
}