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

59 lines
1.5 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-04-18 09:09:39 +00:00
use Utopia\Validator\Text;
2024-01-24 17:44:29 +00:00
use Utopia\Validator\WhiteList;
2024-02-28 19:01:42 +00:00
use Utopia\Validator\Wildcard;
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-04-18 09:09:39 +00:00
->param('name', '', new Text(100), 'Queue name')
2024-02-28 19:09:10 +00:00
->param('limit', 0, new Wildcard(), '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 19:01:42 +00:00
* @param mixed $limit
* @param Connection $queue
*/
2024-02-28 19:01:42 +00:00
public function action(string $name, mixed $limit, Connection $queue): void
2024-01-04 13:00:25 +00:00
{
2024-02-28 19:01:42 +00:00
if (!$name) {
Console::error('Missing required parameter $name');
return;
}
2024-02-28 19:09:10 +00:00
$limit = (int)$limit;
$queueClient = new Client($name, $queue);
2024-02-28 19:15:59 +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
}