-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement sync proxy metadata command #224
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*/5 * * * * /app/bin/console repman:proxy:sync-releases | ||
*/6 * * * * /app/bin/console repman:proxy:sync-metadata | ||
0 */2 * * * /app/bin/console repman:security:update-db |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buddy\Repman\Command; | ||
|
||
use Buddy\Repman\Service\Downloader; | ||
use Buddy\Repman\Service\Proxy; | ||
use Buddy\Repman\Service\Proxy\ProxyRegister; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Lock\LockFactory; | ||
|
||
final class ProxySyncMetadataCommand extends Command | ||
{ | ||
public const LOCK_TTL = 60; | ||
public const LOCK_NAME = 'proxy_metadata'; | ||
|
||
private ProxyRegister $register; | ||
private Downloader $downloader; | ||
private LockFactory $lockFactory; | ||
|
||
public function __construct(ProxyRegister $register, Downloader $downloader, LockFactory $lockFactory) | ||
{ | ||
$this->register = $register; | ||
$this->downloader = $downloader; | ||
$this->lockFactory = $lockFactory; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('repman:proxy:sync-metadata') | ||
->setDescription('Sync proxy metadata with origins') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$lock = $this->lockFactory->createLock(self::LOCK_NAME, self::LOCK_TTL); | ||
if (!$lock->acquire()) { | ||
return 0; | ||
} | ||
|
||
try { | ||
$this->register->all()->forEach(function (Proxy $proxy) use ($lock): void { | ||
$proxy->syncMetadata(); | ||
$lock->refresh(); | ||
}); | ||
} finally { | ||
$lock->release(); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,8 +92,9 @@ public function download(string $package, string $version): void | |
|
||
foreach ($this->decodeMetadata($package) as $packageData) { | ||
$lastDist = $packageData['dist'] ?? $lastDist; | ||
if ($version === $packageData['version']) { | ||
$this->filesystem->write($this->distPath($package, $lastDist['reference'], $lastDist['type']), $this->downloader->getContents($lastDist['url']) | ||
$path = $this->distPath($package, $lastDist['reference'], $lastDist['type']); | ||
if ($version === $packageData['version'] && !$this->filesystem->has($path)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added check if version already exist (without this |
||
$this->filesystem->write($path, $this->downloader->getContents($lastDist['url']) | ||
->getOrElseThrow(new \RuntimeException(sprintf('Failed to download file from %s', $lastDist['url']))) | ||
); | ||
break; | ||
|
@@ -110,6 +111,39 @@ public function removeDist(string $package): void | |
$this->filesystem->deleteDir(sprintf('%s/dist/%s', $this->name, $package)); | ||
} | ||
|
||
public function syncMetadata(): void | ||
{ | ||
foreach ($this->filesystem->listContents($this->name) as $dir) { | ||
if (!in_array($dir['basename'], ['p', 'p2'], true)) { | ||
continue; | ||
} | ||
|
||
$this->syncPackagesMetadata(array_filter( | ||
$this->filesystem->listContents($dir['path'], true), | ||
fn (array $file) => $file['type'] === 'file' && $file['extension'] === 'json') | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param mixed[] $files | ||
*/ | ||
private function syncPackagesMetadata(array $files): void | ||
{ | ||
foreach ($files as $file) { | ||
$url = sprintf('%s://%s', parse_url($this->url, PHP_URL_SCHEME), $file['path']); | ||
// todo: what if proxy do not return `Last-Modified` header? | ||
if ($this->downloader->getLastModified($url)->getOrElse(0) > ($file['timestamp'] ?? time())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. slow performance, maybe we can speed this up with @reactphp 😎 |
||
$metadata = $this->downloader->getContents($url)->getOrNull(); | ||
if ($metadata === null) { | ||
continue; | ||
} | ||
|
||
$this->filesystem->put($file['path'], $metadata); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,14 @@ | |
|
||
final class FakeDownloader implements Downloader | ||
{ | ||
// todo: remove this and allow only for in-memory content to explicit control | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, this class is to magical, while rewriting the tests, I was able to be surprised more than once, because the file system is not defined in the code |
||
private string $basePath; | ||
|
||
/** | ||
* @var mixed[] | ||
*/ | ||
private array $content = []; | ||
|
||
public function __construct() | ||
{ | ||
$this->basePath = __DIR__.'/../Resources'; | ||
|
@@ -23,6 +29,10 @@ public function __construct() | |
*/ | ||
public function getContents(string $url, array $headers = [], callable $notFoundHandler = null): Option | ||
{ | ||
if (isset($this->content[$url])) { | ||
return Option::some($this->content[$url]['content']); | ||
} | ||
|
||
$path = $this->basePath.parse_url($url, PHP_URL_PATH); | ||
|
||
if (file_exists($path)) { | ||
|
@@ -35,4 +45,29 @@ public function getContents(string $url, array $headers = [], callable $notFound | |
|
||
return Option::none(); | ||
} | ||
|
||
/** | ||
* @return Option<int> | ||
*/ | ||
public function getLastModified(string $url): Option | ||
{ | ||
if (isset($this->content[$url])) { | ||
return Option::some($this->content[$url]['timestamp']); | ||
} | ||
|
||
$path = $this->basePath.parse_url($url, PHP_URL_PATH); | ||
if (file_exists($path)) { | ||
return Option::some((int) filemtime($path)); | ||
} | ||
|
||
return Option::none(); | ||
} | ||
|
||
public function addContent(string $url, ?string $content, int $timestamp = null): void | ||
{ | ||
$this->content[$url] = [ | ||
'timestamp' => $timestamp ?? time(), | ||
'content' => $content, | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buddy\Repman\Tests\Functional\Command; | ||
|
||
use Buddy\Repman\Command\ProxySyncMetadataCommand; | ||
use Buddy\Repman\Service\Proxy\ProxyRegister; | ||
use Buddy\Repman\Tests\Doubles\FakeDownloader; | ||
use Buddy\Repman\Tests\Functional\FunctionalTestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\Lock\LockFactory; | ||
use Symfony\Component\Lock\Store\FlockStore; | ||
|
||
final class ProxySyncMetadataCommandTest extends FunctionalTestCase | ||
{ | ||
private FakeDownloader $downloader; | ||
private LockFactory $lockFactory; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->downloader = new FakeDownloader(); | ||
$this->lockFactory = new LockFactory(new FlockStore()); | ||
} | ||
|
||
public function testMetadataSynchronization(): void | ||
{ | ||
$lock = $this->lockFactory->createLock(ProxySyncMetadataCommand::LOCK_NAME); | ||
self::assertFalse($lock->isAcquired()); | ||
|
||
$commandTester = new CommandTester($this->prepareCommand()); | ||
$result = $commandTester->execute([]); | ||
|
||
self::assertEquals($result, 0); | ||
|
||
// test if lock was released | ||
self::assertTrue($lock->acquire()); | ||
$lock->release(); | ||
} | ||
|
||
public function testJobLocking(): void | ||
{ | ||
$lock = $this->lockFactory->createLock(ProxySyncMetadataCommand::LOCK_NAME); | ||
$lock->acquire(); | ||
self::assertTrue($lock->isAcquired()); | ||
|
||
$commandTester = new CommandTester($this->prepareCommand()); | ||
$result = $commandTester->execute([]); | ||
|
||
self::assertEquals($result, 0); | ||
self::assertTrue($lock->isAcquired()); | ||
$lock->release(); | ||
} | ||
|
||
private function prepareCommand(): ProxySyncMetadataCommand | ||
{ | ||
return new ProxySyncMetadataCommand( | ||
$this->container()->get(ProxyRegister::class), | ||
$this->downloader, | ||
$this->lockFactory | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buddy\Repman\Tests\Unit; | ||
|
||
use Buddy\Repman\Service\Dist; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DistTest extends TestCase | ||
{ | ||
public function testVersionWithSlash(): void | ||
{ | ||
$dist = new Dist('repo', 'package', 'dev-master/feature', '123456', 'zip'); | ||
|
||
self::assertEquals(md5('dev-master/feature'), $dist->version()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more coverage 💪 |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will allow to write cleaner and faster tests