appwrite/src/Appwrite/Platform/Tasks/QueueRetry.php

68 lines
1.9 KiB
PHP
Raw Normal View History

2024-01-04 13:00:25 +00:00
<?php
namespace Appwrite\Platform\Tasks;
2024-01-24 17:44:29 +00:00
use Appwrite\Event\Event;
2024-01-04 13:00:25 +00:00
use Utopia\CLI\Console;
use Utopia\Platform\Action;
use Utopia\Queue\Client;
use Utopia\Queue\Connection;
2024-02-28 18:21:35 +00:00
use Utopia\Validator\Integer;
2024-01-24 17:44:29 +00:00
use Utopia\Validator\WhiteList;
2024-01-04 13:00:25 +00:00
2024-01-24 17:54:52 +00:00
class QueueRetry extends Action
2024-01-04 13:00:25 +00:00
{
public static function getName(): string
{
return 'queue-retry';
2024-01-04 13:00:25 +00:00
}
public function __construct()
{
$this
->desc('Retry failed jobs from a specific queue identified by the name parameter')
2024-01-24 17:44:29 +00:00
->param('name', '', new WhiteList([
Event::DATABASE_QUEUE_NAME,
Event::DELETE_QUEUE_NAME,
Event::AUDITS_QUEUE_NAME,
Event::MAILS_QUEUE_NAME,
Event::FUNCTIONS_QUEUE_NAME,
Event::USAGE_QUEUE_NAME,
Event::WEBHOOK_CLASS_NAME,
Event::CERTIFICATES_QUEUE_NAME,
Event::BUILDS_QUEUE_NAME,
Event::MESSAGING_QUEUE_NAME,
Event::MIGRATIONS_QUEUE_NAME,
Event::HAMSTER_CLASS_NAME
]), 'Queue name')
2024-02-28 18:21:35 +00:00
->param('limit', 0, new Integer(true), 'jobs limit', true)
2024-01-04 13:00:25 +00:00
->inject('queue')
2024-02-28 18:21:35 +00:00
->callback(fn ($name, $limit, $queue) => $this->action($name, $limit, $queue));
2024-01-04 13:00:25 +00:00
}
/**
* @param string $name The name of the queue to retry jobs from
2024-02-28 18:21:35 +00:00
* @param int $limit
* @param Connection $queue
*/
2024-02-28 18:21:35 +00:00
public function action(string $name, int $limit, Connection $queue): void
2024-01-04 13:00:25 +00:00
{
if (!$name) {
Console::error('Missing required parameter $name');
return;
}
$queueClient = new Client($name, $queue);
2024-01-04 13:00:25 +00:00
if ($queueClient->countFailedJobs() === 0) {
Console::error('No failed jobs found.');
2024-01-04 13:00:25 +00:00
return;
}
2024-01-24 17:44:29 +00:00
Console::log('Retrying failed jobs...');
2024-02-28 18:21:35 +00:00
$queueClient->retry($limit);
2024-01-04 13:00:25 +00:00
}
2024-01-04 13:04:51 +00:00
}