1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 06:59:01 +01:00

Minimize filesystem and network access on light startup

This commit is contained in:
Daniil Gentili 2023-01-15 19:09:27 +01:00
parent c5cc1ca3b0
commit ea00c59722
3 changed files with 29 additions and 13 deletions

View File

@ -13,6 +13,10 @@ use Throwable;
use const LOCK_EX;
use const LOCK_NB;
use function Amp\File\move;
use function Amp\File\read;
use function Amp\File\write;
final class GarbageCollector
{
@ -61,18 +65,33 @@ final class GarbageCollector
}
$client = HttpClientBuilder::buildDefault();
$request = new Request(MADELINE_RELEASE_URL);
self::$cleanupLoop = new PeriodicLoop(function () use ($client, $request): bool {
$madelinePhpContents = null;
self::$cleanupLoop = new PeriodicLoop(function () use ($client, $request, &$madelinePhpContents): bool {
try {
$latest = $client->request($request);
Magic::$version_latest = $latest->getBody()->buffer();
if (Magic::$version !== Magic::$version_latest) {
Logger::log('!!!!!!!!!!!!! An update of MadelineProto is required, shutting down worker! !!!!!!!!!!!!!', Logger::FATAL_ERROR);
write(MADELINE_PHAR_VERSION, '');
if (Magic::$isIpcWorker) {
throw new SignalException('!!!!!!!!!!!!! An update of MadelineProto is required, shutting down worker! !!!!!!!!!!!!!');
}
return true;
}
$madelinePhpContents ??= read(MADELINE_PHP);
$contents = $client->request(new Request("https://phar.madelineproto.xyz/phar.php?v=new".\rand(0, PHP_INT_MAX)))
->getBody()
->buffer();
if ($contents !== $madelinePhpContents) {
$unlock = Tools::flock(MADELINE_PHP.'.lock', LOCK_EX);
write(MADELINE_PHP.'.temp.php', $contents);
move(MADELINE_PHP.'.temp.php', MADELINE_PHP);
$unlock();
$madelinePhpContents = $contents;
}
/** @psalm-suppress UndefinedConstant */
foreach (\glob(MADELINE_PHAR_GLOB) as $path) {
$base = \basename($path);

View File

@ -32,9 +32,6 @@ if (!isset($backtrace[0]["file"]) || !in_array(basename($backtrace[0]["file"]),
if (defined("MADELINE_REAL_ROOT")) {
@chdir(MADELINE_REAL_ROOT);
}
if ($contents = file_get_contents("https://phar.madelineproto.xyz/phar.php?v=new".rand(0, PHP_INT_MAX))) {
file_put_contents($backtrace[0]["file"], $contents);
}
Phar::interceptFileFuncs();
Phar::mapPhar("'.$argv[2].'");

View File

@ -65,6 +65,7 @@ class Installer
}
$this->version = (string) \min(81, (int) (PHP_MAJOR_VERSION.PHP_MINOR_VERSION));
\define('MADELINE_PHAR_GLOB', \getcwd().DIRECTORY_SEPARATOR."madeline*-{$this->version}.phar");
\define('MADELINE_PHAR_VERSION', \getcwd().DIRECTORY_SEPARATOR."madeline-{$this->version}.phar.version");
\define('MADELINE_RELEASE_URL', \sprintf(self::RELEASE_TEMPLATE, $this->version));
}
@ -201,23 +202,22 @@ class Installer
*/
public function install()
{
$remote_release = \file_get_contents(MADELINE_RELEASE_URL) ?: null;
$madeline_phar = "madeline-$remote_release.phar";
$madeline_version = "madeline-{$this->version}.phar.version";
if (\file_exists($madeline_version)) {
$local_release = \file_get_contents($madeline_version) ?: null;
if (\file_exists(MADELINE_PHAR_VERSION)) {
$local_release = \file_get_contents(MADELINE_PHAR_VERSION) ?: null;
} else {
\touch($madeline_version);
\touch(MADELINE_PHAR_VERSION);
$local_release = null;
}
\define('HAD_MADELINE_PHAR', !!$local_release);
if (($remote_release === $local_release && \file_exists($madeline_phar)) || $remote_release === null) {
if ($local_release !== null) {
return self::load($local_release);
}
if (!$this->lock($madeline_version)) {
$remote_release = \file_get_contents(MADELINE_RELEASE_URL) ?: null;
$madeline_phar = "madeline-$remote_release.phar";
if (!$this->lock(MADELINE_PHAR_VERSION)) {
\flock($this->lockInstaller, LOCK_EX);
return $this->install();
}