Hello,
Thank you for your help.
I developed a solution that allows me to run several jobs at the same time.
Here is a little extract for those who are interested.
1- I create several configs using a loop to have multiple config
// In bootstrap.php
if (Configure::check('Queue.default')) {
Util::multipleJobs(Configure::read('Queue.default'));
}
// In Util.php
public static function multipleJobs(array $queue): void
{
if (PHP_SAPI === 'cli') {
$configs = [
'videos' => [
'name' => 'videos',
'count' => 10,
],
'tags' => [
'name' => 'tags',
'count' => 5,
'storeFailedJobs' => false,
],
];
foreach ($configs as $config) {
foreach (range(1, $config['count']) as $number) {
$name = sprintf('%s%d', $config['name'], $number);
$queue['queue'] = $name;
if (isset($config['storeFailedJobs'])) {
$queue['storeFailedJobs'] = $config['storeFailedJobs'];
}
Configure::write(sprintf('Queue.%s', $name), $queue);
}
}
}
}
1- I created the workers command to launch several workers at the same time
bin/cake queue workers --config videos
public function execute(Arguments $args, ConsoleIo $io): int
{
$config = (string)$args->getOption('config');
/**
* Queue::getConfigs('videos') returns an array of configs for videos
* ['videos1', 'videos2', 'videos3', 'videos4', ...]
*/
foreach (Queue::getConfigs($config) as $config) {
$name = sprintf('queue worker --config %s', $config);
$command = sprintf('bin/cake %s', $name);
$io->out(sprintf('Run Command: %s', $command));
$PIDs = $this->getPIDs($name);
if ($PIDs) {
$io->warning('Worker Already Running!');
while (count($PIDs) > 1) {
$pid = array_shift($PIDs);
$this->killProcess($pid);
}
continue;
}
$process = Process::fromShellCommandline($command);
$process->setTimeout(null);
$process->start();
sleep(1);
}
}
3- Save each job in the queue with a different config.
public static function nextConfig(): string
{
$queue = self::getInstance();
$next = (string)array_shift($queue->configs);
if ($next) {
$queue->configs[] = $next;
}
return $next;
}
while ($videos = $this->whileQuery($query, $args, $identifier, ['limit' => $limit])) {
foreach ($videos as $video) {
$data = ['id' => $video->id];
$options = ['config' => Queue::nextConfig()];
QueueManager::push(VideosTranslateJob::class, $data, $options);
}
}
This works for me, if anyone has any ideas to improve the system please don’t hesitate.