Skip to content

Commit

Permalink
Merge pull request #37 from p-bizouard/notify-every-n
Browse files Browse the repository at this point in the history
🔇 Notify every n failed runs
  • Loading branch information
p-bizouard authored May 26, 2024
2 parents abc2d1c + f78cddb commit 496aa0c
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
31 changes: 31 additions & 0 deletions migrations/Version20240526132652.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240526132652 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE backup_configuration ADD notify_every SMALLINT DEFAULT 1 NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE backup_configuration DROP notify_every');
}
}
6 changes: 5 additions & 1 deletion src/Command/BackupStartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$lock->refresh();
}
} catch (Exception $e) {
$errorMessage = sprintf('General error : %s', $e->getMessage());

$log = new Log();
$log->setLevel(Log::LOG_ERROR);
$log->setMessage(sprintf('General error : %s', $e->getMessage()));
$log->setMessage($errorMessage);

$symfonyStyle->error($errorMessage);

$this->entityManager->persist($log);
$this->entityManager->flush();
Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Admin/BackupConfigurationCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public function configureFields(string $pageName): iterable
->hideOnIndex()
->setHelp('Schedule after this hour'),

IntegerField::new('notifyEvery')
->setHelp('When error occured, notify every X runs. Use 0 to notifications'),

DateTimeField::new('createdAt')->hideOnForm()->hideOnIndex(),
DateTimeField::new('updatedAt')->hideOnForm()->hideOnIndex(),

Expand Down
17 changes: 17 additions & 0 deletions src/Entity/BackupConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ class BackupConfiguration implements Stringable
*/
private ?string $rcloneConfiguration = null;

/**
* @ORM\Column(type="smallint", options={"default" : 1})
*/
private int $notifyEvery = 1;

final public const PERIODICITY_DAILY = 'daily';

final public const TYPE_OS_INSTANCE = 'os-instance';
Expand Down Expand Up @@ -562,4 +567,16 @@ public function setStdErrIgnore(?string $stdErrIgnore): static

return $this;
}

public function getNotifyEvery(): int
{
return $this->notifyEvery;
}

public function setNotifyEvery(int $notifyEvery): static
{
$this->notifyEvery = $notifyEvery;

return $this;
}
}
10 changes: 9 additions & 1 deletion src/EventSubscriber/BackupSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Entity\Backup;
use App\Entity\BackupConfiguration;
use App\Entity\Log;
use App\Repository\BackupRepository;
use App\Service\BackupService;
use App\Service\MailerService;
use Exception;
Expand All @@ -17,6 +18,7 @@ class BackupSubscriber implements EventSubscriberInterface
public function __construct(
private readonly BackupService $backupService,
private readonly MailerService $mailerService,
private readonly BackupRepository $backupRepository,
) {
}

Expand Down Expand Up @@ -100,7 +102,13 @@ public function onFailed(Event $event): void

$this->backupService->log($backup, Log::LOG_ERROR, 'Backup failed');

$this->mailerService->sendFailedBackupReport($backup);
$countFailedBackupsSinceLastSuccess = $this->backupRepository->countFailedBackupsSinceLastSuccess($backup);
$notifyEvery = $backup->getBackupConfiguration()->getNotifyEvery();

// Notify every X failed backups, and do not notify if notifyEvery is 0
if ($backup->getBackupConfiguration()->getNotifyEvery() > 0 && 0 === ($countFailedBackupsSinceLastSuccess + 1) % $notifyEvery) {
$this->mailerService->sendFailedBackupReport($backup);
}
}

public function onEnterAll(Event $event): void
Expand Down
31 changes: 31 additions & 0 deletions src/Repository/BackupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@ public function __construct(ManagerRegistry $managerRegistry)
parent::__construct($managerRegistry, Backup::class);
}

/**
* @return int Return the number of failed backups since the last successful backup
*/
public function countFailedBackupsSinceLastSuccess(Backup $backup): int
{
/** @var Backup */
$lastSuccessBackup = $this->createQueryBuilder('b')
->where("b.currentPlace = 'backuped'")
->andWhere('b.backupConfiguration = :backupConfiguration')
->setParameter('backupConfiguration', $backup->getBackupConfiguration())
->orderBy('b.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();

$qb = $this->createQueryBuilder('b');
$qb->select($qb->expr()->count('b'))
->where("b.currentPlace != 'backuped'")
->andWhere('b.backupConfiguration = :backupConfiguration')
->andWhere('b.id != :currentBackup')
->setParameter('backupConfiguration', $backup->getBackupConfiguration())
->setParameter('currentBackup', $backup->getId());

if (null !== $lastSuccessBackup) {
$qb->andWhere('b.createdAt > :lastSuccessDate')
->setParameter('lastSuccessDate', $lastSuccessBackup->getCreatedAt());
}

return $qb->getQuery()->getSingleScalarResult();
}

// /**
// * @return Backup[] Returns an array of Backup objects
// */
Expand Down

0 comments on commit 496aa0c

Please sign in to comment.