Skip to content

Commit

Permalink
Fix scheduler not running (#1166)
Browse files Browse the repository at this point in the history
Co-authored-by: Melroy van den Berg <[email protected]>
  • Loading branch information
BentiGorlich and melroy89 authored Oct 9, 2024
1 parent 853e111 commit f1db652
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 37 deletions.
14 changes: 14 additions & 0 deletions src/Message/ClearDeadMessagesMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Message;

use App\Message\Contracts\SchedulerInterface;

class ClearDeadMessagesMessage implements SchedulerInterface
{
public function __construct()
{
}
}
14 changes: 14 additions & 0 deletions src/Message/ClearDeletedUserMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Message;

use App\Message\Contracts\SchedulerInterface;

class ClearDeletedUserMessage implements SchedulerInterface
{
public function __construct()
{
}
}
9 changes: 9 additions & 0 deletions src/Message/Contracts/SchedulerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Message\Contracts;

interface SchedulerInterface extends MessageInterface
{
}
30 changes: 30 additions & 0 deletions src/MessageHandler/ClearDeadMessagesHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\MessageHandler;

use App\Message\ClearDeadMessagesMessage;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\ResultSetMapping;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
class ClearDeadMessagesHandler
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger,
) {
}

public function __invoke(ClearDeadMessagesMessage $message): void
{
$this->logger->info('Clearing dead messages');
$sql = 'DELETE FROM messenger_messages WHERE queue_name = :queue_name';
$this->entityManager->createNativeQuery($sql, new ResultSetMapping())
->setParameter('queue_name', 'dead')
->getResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace App\Scheduler\Handlers;
namespace App\MessageHandler;

use App\Message\ClearDeletedUserMessage;
use App\Message\DeleteUserMessage;
use App\Scheduler\Messages\ClearDeletedUserMessage;
use App\Service\UserManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
Expand Down
25 changes: 0 additions & 25 deletions src/Scheduler/DeleteUserTaskProvider.php

This file was deleted.

38 changes: 38 additions & 0 deletions src/Scheduler/MbinTaskProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Scheduler;

use App\Message\ClearDeadMessagesMessage;
use App\Message\ClearDeletedUserMessage;
use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;
use Symfony\Contracts\Cache\CacheInterface;

#[AsSchedule]
class MbinTaskProvider implements ScheduleProviderInterface
{
private ?Schedule $schedule = null;

public function __construct(
private readonly CacheInterface $cache
) {
}

public function getSchedule(): Schedule
{
if (null === $this->schedule) {
$this->schedule = (new Schedule())
->add(
RecurringMessage::every('1 day', new ClearDeletedUserMessage()),
RecurringMessage::every('1 day', new ClearDeadMessagesMessage()),
)
->stateful($this->cache);
}

return $this->schedule;
}
}
9 changes: 0 additions & 9 deletions src/Scheduler/Messages/ClearDeletedUserMessage.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Service/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
use App\Event\User\UserFollowEvent;
use App\Exception\UserCannotBeBanned;
use App\Factory\UserFactory;
use App\Message\ClearDeletedUserMessage;
use App\Message\DeleteImageMessage;
use App\Message\DeleteUserMessage;
use App\Message\UserCreatedMessage;
use App\Message\UserUpdatedMessage;
use App\MessageHandler\ClearDeletedUserHandler;
use App\Repository\ImageRepository;
use App\Repository\ReputationRepository;
use App\Repository\UserFollowRepository;
use App\Repository\UserFollowRequestRepository;
use App\Repository\UserRepository;
use App\Scheduler\Messages\ClearDeletedUserMessage;
use App\Security\EmailVerifier;
use App\Service\ActivityPub\KeysGenerator;
use Doctrine\ORM\EntityManagerInterface;
Expand Down

0 comments on commit f1db652

Please sign in to comment.