Queue callbacks
New in version 14.0.0
Callbacks implement AOECrawler, see
Queue callbacks implement an interface.
Besides URLs, the queue can hold callbacks: instead of crawling a URL, the worker calls a class of your extension. This is useful for work that should run in the background with the crawler, e.g. crawling external files. The result is stored with the queue entry and shown in the Crawler log.
Implement the callback
A callback implements AOECrawler. It is a
service, so it gets its dependencies by constructor injection:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Crawler;
use AOE\Crawler\Queue\QueueCallbackInterface;
use MyVendor\MyExtension\Service\FileIndexer;
final readonly class ReindexFileCallback implements QueueCallbackInterface
{
public function __construct(
private FileIndexer $fileIndexer,
) {
}
public function execute(array $parameters): mixed
{
return $this->fileIndexer->reindex((int) $parameters['fileUid']);
}
}
The return value is stored JSON encoded as the result of the queue entry.
With autoconfigure: true in the Configuration/Services.yaml of your
extension, which is the default, the callback is registered automatically.
Classes that are not services are created with
General, without constructor arguments.
Add the callback to the queue
Add a queue entry with AOECrawler.
Inject the Queue into your own service:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Service;
use AOE\Crawler\Queue\QueueEntryWriter;
use MyVendor\MyExtension\Crawler\ReindexFileCallback;
final readonly class FileQueue
{
public function __construct(
private QueueEntryWriter $queueEntryWriter,
) {
}
public function reindexLater(int $fileUid, int $pageId): void
{
$this->queueEntryWriter->addCallback(
ReindexFileCallback::class,
['fileUid' => $fileUid],
$pageId,
);
}
}
add takes these arguments:
$callbackClass Name - The class implementing
Queue.Callback Interface $parameters- Handed over to
execute. They are stored JSON encoded, so they must be JSON encodable.() $pageId - The page the queue entry is shown with in the Crawler log,
0by default. $scheduled- The Unix timestamp to execute the callback at,
0(the default) for now. $setId - The set the queue entry belongs to,
0by default. Give several queue entries the same set id to show them together in the Crawler log.
The queue entry is dispatched to the message bus like a URL, and processed by the worker, see Message bus instead of the Crawler process handling.