Skip to content

Commit

Permalink
Merge branch 'master' into composer-proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed Jan 28, 2023
2 parents 657fffb + ea0b09e commit 62372d6
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 71 deletions.
107 changes: 81 additions & 26 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 14 additions & 16 deletions src/Cron/Handler/CleanupDistDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,37 @@

namespace Packeton\Cron\Handler;

use Okvpn\Bundle\CronBundle\Attribute\AsCron;
use Packeton\Service\DistConfig;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
* Remove unused mirrored package's *.zip archives
*/
#[AsCron('40 1 */2 * *')]
class CleanupDistDir
{
const DEFAULT_PERIOD = 60; // 60 days

private $fs;
private $distConfig;
private $logger;

public function __construct(DistConfig $distConfig, LoggerInterface $logger)
{
$this->fs = new Filesystem();
$this->distConfig = $distConfig;
$this->logger = $logger;
public function __construct(
private readonly DistConfig $distConfig,
private readonly LoggerInterface $logger,
private readonly Filesystem $fs,
) {
}

public function __invoke(array $arguments = [])
public function __invoke(array $arguments = []): array
{
$keepPeriod = $arguments['period'] ?? self::DEFAULT_PERIOD;
$expireDate = new \DateTime('now', new \DateTimeZone('UTC'));
$expireDate->modify(sprintf('-%d days', $keepPeriod));
$expireDate->modify(\sprintf('-%d days', $keepPeriod));

if (null === $this->distConfig->getDistDir() || !$this->fs->exists($this->distConfig->getDistDir())) {
return [];
}

$root = realpath($this->distConfig->getDistDir());
$root = \realpath($this->distConfig->getDistDir());
$dir = new \RecursiveDirectoryIterator(
$root,
\FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS
Expand All @@ -49,13 +47,13 @@ function (\SplFileInfo $current) use (&$paths, $expireDate) {
if (!$current->getRealPath()) {
return false;
}
if ($current->isFile() && preg_match('/[a-f0-9]{40}\.zip$/', $current->getFilename())) {
if ($current->isFile() && \preg_match('/[a-f0-9]{40}\.zip$/', $current->getFilename())) {
if ($current->getMTime() < $expireDate->getTimestamp()) {
$paths[] = $current->getRealPath();
}
return false;
}
if (is_dir($current->getPathname()) && 0 !== strpos($current->getPathname(), '.')) {
if (\is_dir($current->getPathname()) && !\str_starts_with($current->getPathname(), '.')) {
return true;
}

Expand All @@ -66,14 +64,14 @@ function (\SplFileInfo $current) use (&$paths, $expireDate) {
$iterator = new \RecursiveIteratorIterator($filter);
$iterator->rewind();
if ($paths) {
$this->logger->info(sprintf('Unused %s *.zip archives was found', count($paths)), ['paths' => $paths]);
$this->logger->info(\sprintf('Unused %s *.zip archives was found', \count($paths)), ['paths' => $paths]);
}

foreach ($paths as $path) {
try {
$this->fs->remove($path);
} catch (\Exception $exception) {
$this->logger->warning(sprintf('Unable to delete the file "%s", cause %s', $path, $exception->getMessage()), ['path' => $path, 'e' => $exception]);
$this->logger->warning(\sprintf('Unable to delete the file "%s", cause %s', $path, $exception->getMessage()), ['path' => $path, 'e' => $exception]);
}
}

Expand Down
40 changes: 17 additions & 23 deletions src/Cron/Handler/CleanupJobStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@
namespace Packeton\Cron\Handler;

use Doctrine\Persistence\ManagerRegistry;
use Okvpn\Bundle\CronBundle\Attribute\AsCron;
use Packeton\Entity\Job;
use Psr\Log\LoggerInterface;

/**
* Cron command to cleanup jobs storage
*/
#[AsCron('49 0 * * *')]
class CleanupJobStorage
{
private $registry;
private $logger;

public function __construct(ManagerRegistry $registry, LoggerInterface $logger)
{
$this->registry = $registry;
$this->logger = $logger;
public function __construct(
private readonly ManagerRegistry $registry,
private readonly LoggerInterface $logger
) {
}

public function __invoke()
public function __invoke(): array
{
$keepPeriod = $this->selectKeepPeriod($count);
$expireDate = new \DateTime('now', new \DateTimeZone('UTC'));
$expireDate->modify(sprintf('-%d days', $keepPeriod));
$expireDate->modify(\sprintf('-%d days', $keepPeriod));

$rowCount = $this->registry->getRepository(Job::class)
->createQueryBuilder('j')
Expand All @@ -36,7 +35,7 @@ public function __invoke()
->getQuery()
->execute();

$this->logger->info(sprintf('Removed %s jobs from storage, since: %s, jobs count: %s', $rowCount, $expireDate->format('c'), $count));
$this->logger->info(\sprintf('Removed %s jobs from storage, since: %s, jobs count: %s', $rowCount, $expireDate->format('c'), $count));

return [
'since' => $expireDate->format('c'),
Expand All @@ -45,7 +44,7 @@ public function __invoke()
];
}

protected function selectKeepPeriod(&$count = null)
protected function selectKeepPeriod(&$count = null): int
{
$count = $this->registry->getRepository(Job::class)
->createQueryBuilder('j')
Expand All @@ -54,17 +53,12 @@ protected function selectKeepPeriod(&$count = null)
->getQuery()
->getSingleScalarResult();

switch ($count) {
case $count > 60000:
return 2;
case $count > 40000:
return 5;
case $count > 25000:
return 10;
case $count > 10000:
return 21;
}

return 60;
return match ($count) {
$count > 60000 => 2,
$count > 40000 => 5,
$count > 25000 => 10,
$count > 10000 => 21,
default => 60,
};
}
}
8 changes: 3 additions & 5 deletions src/Cron/Handler/ScheduleHookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace Packeton\Cron\Handler;

use Okvpn\Bundle\CronBundle\CronServiceInterface;
use Packeton\Webhook\HookBus;

class ScheduleHookHandler
class ScheduleHookHandler implements CronServiceInterface
{
private $hookBus;

public function __construct(HookBus $hookBus)
public function __construct(private readonly HookBus $hookBus)
{
$this->hookBus = $hookBus;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Cron/WorkerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function handle(ScheduleEnvelope $envelope, StackInterface $stack): Sched
return $stack->next()->handle($envelope, $stack);
}

$envelopeData = serialize($envelope->without(WorkerStamp::class));
$envelopeData = \serialize($envelope->without(WorkerStamp::class));
$this->jobScheduler->publish(WorkerStamp::JOB_NAME, [
'envelope' => $envelopeData
]);
Expand Down
9 changes: 9 additions & 0 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ class Kernel extends BaseKernel
configureRoutes as traitConfigureRoutes;
}

public function __construct(string $environment, bool $debug)
{
if (\class_exists(DebugClassLoader::class)) {
DebugClassLoader::disable();
}

parent::__construct($environment, $debug);
}

private function configureContainer(ContainerConfigurator $container, LoaderInterface $loader, ContainerBuilder $builder): void
{
$configDir = $this->getConfigDir();
Expand Down

0 comments on commit 62372d6

Please sign in to comment.