-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from vtsykun/cron-command-gc
Job and zip's archive GC
- Loading branch information
Showing
6 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Packagist\WebBundle\Cron\Handler; | ||
|
||
use Packagist\WebBundle\Service\DistConfig; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
/** | ||
* Remove unused mirrored package's *.zip archives | ||
*/ | ||
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 __invoke(array $arguments = []) | ||
{ | ||
$keepPeriod = $arguments['period'] ?? self::DEFAULT_PERIOD; | ||
$expireDate = new \DateTime('now', new \DateTimeZone('UTC')); | ||
$expireDate->modify(sprintf('-%d days', $keepPeriod)); | ||
|
||
if (null === $this->distConfig->getDistDir() || !$this->fs->exists($this->distConfig->getDistDir())) { | ||
return []; | ||
} | ||
|
||
$root = realpath($this->distConfig->getDistDir()); | ||
$dir = new \RecursiveDirectoryIterator( | ||
$root, | ||
\FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS | ||
); | ||
|
||
$paths = []; | ||
$filter = new \RecursiveCallbackFilterIterator( | ||
$dir, | ||
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->getMTime() < $expireDate->getTimestamp()) { | ||
$paths[] = $current->getRealPath(); | ||
} | ||
return false; | ||
} | ||
if (is_dir($current->getPathname()) && 0 !== strpos($current->getPathname(), '.')) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
); | ||
|
||
$iterator = new \RecursiveIteratorIterator($filter); | ||
$iterator->rewind(); | ||
if ($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]); | ||
} | ||
} | ||
|
||
return $paths; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/Packagist/WebBundle/Cron/Handler/CleanupJobStorage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Packagist\WebBundle\Cron\Handler; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Packagist\WebBundle\Entity\Job; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Cron command to cleanup jobs storage | ||
*/ | ||
class CleanupJobStorage | ||
{ | ||
private $registry; | ||
private $logger; | ||
|
||
public function __construct(ManagerRegistry $registry, LoggerInterface $logger) | ||
{ | ||
$this->registry = $registry; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function __invoke() | ||
{ | ||
$keepPeriod = $this->selectKeepPeriod($count); | ||
$expireDate = new \DateTime('now', new \DateTimeZone('UTC')); | ||
$expireDate->modify(sprintf('-%d days', $keepPeriod)); | ||
|
||
$rowCount = $this->registry->getRepository(Job::class) | ||
->createQueryBuilder('j') | ||
->delete() | ||
->where('j.createdAt < :createdAt') | ||
->setParameter('createdAt', $expireDate) | ||
->getQuery() | ||
->execute(); | ||
|
||
$this->logger->info(sprintf('Removed %s jobs from storage, since: %s, jobs count: %s', $rowCount, $expireDate->format('c'), $count)); | ||
|
||
return [ | ||
'since' => $expireDate->format('c'), | ||
'rows' => $rowCount, | ||
'count' => $count | ||
]; | ||
} | ||
|
||
protected function selectKeepPeriod(&$count = null) | ||
{ | ||
$count = $this->registry->getRepository(Job::class) | ||
->createQueryBuilder('j') | ||
->resetDQLPart('select') | ||
->select('COUNT(j.id)') | ||
->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters