I'm trying to avoid cron and do the job with symfony scheduler. I'm almost there (tested writing some text to a file every 20 seconds), but I'm having hard time with understanding where to put the db queries.
I thought that it would be reasonable to fetch the db results in the message by autowiring the Entity Manager
//src/Scheduler/Message/CheckReceiptStatus.php
namespace App\Scheduler\Message;
use App\Entity\OnlineReceipt;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Collections\Criteria;
class CheckReceiptStatus
{
public function __construct(
private EntityManagerInterface $entityManager,
) {}
public function getErroredReceipts()
{
$expressionBuilder = Criteria::expr();
$expression = $expressionBuilder->notIn('status', ['SUCCESS', 'ERROR']);
$receipts_with_no_status = $this->entityManager->getRepository(OnlineReceipt::class)->matching(new Criteria($expression));
return $receipts_with_no_status;
}
}
Then in the handler I planned to make some db inserts. Instead of writing into file (tested the scheduler/message code that way) I would have some doctrine queries.
// src/Scheduler/Handler/CheckReceiptStatusHandler.php
namespace App\Scheduler\Handler;
use App\Scheduler\Message\CheckReceiptStatus;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
class CheckReceiptStatusHandler
{
public function __construct(
private readonly KernelInterface $kernel,
){}
public function __invoke(CheckReceiptStatus $status) : void
{
$path = $this->kernel->getProjectDir().'/var/log/cron_test.log';
file_put_contents($path, $status->getErroredReceipts(), FILE_APPEND);
}
}
However, I see an error notice in my scheduler code that reads that new CheckReceiptStatus()
expects 1 argument.
// src/Scheduler/ReceiptTaskScheduler.php
namespace App\Scheduler;
use App\Scheduler\Message\CheckReceiptStatus;
use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;
#[AsSchedule('default')]
class ReceiptTaskScheduler implements ScheduleProviderInterface
{
public function getSchedule(): Schedule
{
$schedule = new Schedule();
$schedule->add(RecurringMessage::every("20 seconds", new CheckReceiptStatus()));
return $schedule;
}
}
Since I don't pass anything to the message class (as I plan to get results there) I am not sure what argument is expected. There is definitely something wrong with my autowiring and logic.
new
and now through the service container