AzuraCast/src/Sync/Task/RotateLogsTask.php

135 lines
3.8 KiB
PHP
Raw Normal View History

<?php
2021-07-19 07:53:45 +02:00
declare(strict_types=1);
namespace App\Sync\Task;
use App\Doctrine\ReloadableEntityManagerInterface;
use App\Entity;
use App\Environment;
use App\Radio\Adapters;
2021-03-31 18:42:24 +02:00
use League\Flysystem\StorageAttributes;
2020-01-30 00:59:08 +01:00
use Psr\Log\LoggerInterface;
2022-04-11 05:57:23 +02:00
use Supervisor\SupervisorInterface;
use Symfony\Component\Finder\Finder;
2022-05-08 20:05:02 +02:00
use Throwable;
2020-11-30 10:55:30 +01:00
class RotateLogsTask extends AbstractTask
{
public function __construct(
2021-04-23 07:24:12 +02:00
protected Environment $environment,
protected Adapters $adapters,
2022-04-11 05:57:23 +02:00
protected SupervisorInterface $supervisor,
2021-04-23 07:24:12 +02:00
protected Entity\Repository\SettingsRepository $settingsRepo,
protected Entity\Repository\StorageLocationRepository $storageLocationRepo,
ReloadableEntityManagerInterface $em,
2021-04-23 07:24:12 +02:00
LoggerInterface $logger
) {
parent::__construct($em, $logger);
}
public static function getSchedulePattern(): string
{
return '34 * * * *';
}
2020-07-08 09:03:50 +02:00
public function run(bool $force = false): void
{
// Rotate logs for individual stations.
foreach ($this->iterateStations() as $station) {
$this->logger->info(
2022-01-08 06:32:59 +01:00
'Rotating logs for station.',
['station' => (string)$station]
);
2022-01-08 06:32:59 +01:00
try {
$this->rotateStationLogs($station);
2022-05-08 20:05:02 +02:00
} catch (Throwable $e) {
2022-01-08 06:32:59 +01:00
$this->logger->error($e->getMessage(), [
'station' => (string)$station,
]);
}
}
// Rotate the automated backups.
$settings = $this->settingsRepo->readSettings();
$copiesToKeep = $settings->getBackupKeepCopies();
if ($copiesToKeep > 0) {
$backupStorageId = (int)$settings->getBackupStorageLocation();
if ($backupStorageId > 0) {
$storageLocation = $this->storageLocationRepo->findByType(
Entity\Enums\StorageLocationTypes::Backup,
$backupStorageId
);
if ($storageLocation instanceof Entity\StorageLocation) {
$this->rotateBackupStorage($storageLocation, $copiesToKeep);
}
}
}
}
protected function rotateBackupStorage(
Entity\StorageLocation $storageLocation,
int $copiesToKeep
): void {
$fs = $storageLocation->getFilesystem();
2021-03-31 18:42:24 +02:00
$iterator = $fs->listContents('', false)->filter(
function (StorageAttributes $attrs) {
return 0 === stripos($attrs->path(), 'automatic_backup');
}
);
$backupsByTime = [];
foreach ($iterator as $backup) {
2021-03-31 18:42:24 +02:00
/** @var StorageAttributes $backup */
$backupsByTime[$backup->lastModified()] = $backup->path();
}
if (count($backupsByTime) <= $copiesToKeep) {
return;
}
krsort($backupsByTime);
2021-06-08 08:40:49 +02:00
foreach (array_slice($backupsByTime, $copiesToKeep) as $backupToDelete) {
$fs->delete($backupToDelete);
$this->logger->info(sprintf('Deleted automated backup: "%s"', $backupToDelete));
}
}
/**
* Rotate logs that are not automatically rotated (currently Liquidsoap only).
*
* @param Entity\Station $station
*
*/
public function rotateStationLogs(Entity\Station $station): void
{
2020-01-30 00:59:08 +01:00
$this->cleanUpIcecastLog($station);
}
2020-01-30 00:59:08 +01:00
protected function cleanUpIcecastLog(Entity\Station $station): void
{
$config_path = $station->getRadioConfigDir();
$finder = new Finder();
$finder
->files()
->in($config_path)
->name('icecast_*.log.*')
->date('before 1 month ago');
2019-09-04 20:00:51 +02:00
foreach ($finder as $file) {
$file_path = $file->getRealPath();
2021-07-19 07:53:45 +02:00
if ($file_path) {
@unlink($file_path);
}
}
}
}