我如何 运行 来自 laravel 个作业的有限数量的后台进程?
How do I run a limited number of background processes from laravel jobs?
我想 运行 来自 laravel 个作业的 ffmpeg 进程,但不要同时处理太多。我似乎无法做到正确。无论我为 $process_limit 设置什么 - 它一次只有 运行s 一个,并且中间有很长的延迟。也许我使用 public $timeout 错误。也许 retryUntil()。我不知道。
<?php
namespace FuquIo\LaravelFfmpeg;
use Cocur\BackgroundProcess\BackgroundProcess;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
class RenderMpeg4ToWebmJob implements ShouldQueue{
use Dispatchable, InteractsWithQueue, Queueable;
public $timeout = 3600;
/**
* @var string
*/
private $input_file;
/**
* @var array
*/
private $map;
/**
* Create a new job instance.
*
* @param array $map
* @param string $input_file
*/
public function __construct(array $map, string $input_file){
//
$this->map = $map;
$this->input_file = $input_file;
}
/**
* Execute the job.
*
* @return void
* @throws \Exception
*/
public function handle(){
$almost_timeout = $this->timeout - 100;
$map = $this->map;
$input_file = $this->input_file;
$cmds = '(' . implode('; ', config('fuqu-ffmpeg.command')) . ')';
$cmds = str_replace(array_keys($map), array_values($map), $cmds);
Log::debug($cmds);
$process_limit = config(ServiceProvider::SHORT_NAME .'.process_limit');
Redis::funnel('ffmpeg')->limit($process_limit)->then(
function () use ($cmds, $input_file, $almost_timeout){
$process = new BackgroundProcess($cmds);
$process->run();
if(!$process->isRunning()){
throw new \Exception('Unable to execute file processing command on ' . $input_file);
}
/**
* This doesn't prevent an additional
* background process from spawning
* but it does give a head start
*/
$slept = 0;
do{
sleep(10);
$slept += 10;
}while($process->isRunning() and ($slept < $almost_timeout));
}, function (){
// Could not obtain lock...
return $this->release(100);
});
}
/**
* Rather than doing x tries,
* just keep trying until.
*
* @return \DateTime
*/
public function retryUntil(){
return now()->addDays(1);
}
}
原来问题中的代码确实有效。我的问题在 bg 过程中。处理以数字结尾的文件时遇到问题。我想我会留下代码...可能对某人有用。
我想 运行 来自 laravel 个作业的 ffmpeg 进程,但不要同时处理太多。我似乎无法做到正确。无论我为 $process_limit 设置什么 - 它一次只有 运行s 一个,并且中间有很长的延迟。也许我使用 public $timeout 错误。也许 retryUntil()。我不知道。
<?php
namespace FuquIo\LaravelFfmpeg;
use Cocur\BackgroundProcess\BackgroundProcess;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
class RenderMpeg4ToWebmJob implements ShouldQueue{
use Dispatchable, InteractsWithQueue, Queueable;
public $timeout = 3600;
/**
* @var string
*/
private $input_file;
/**
* @var array
*/
private $map;
/**
* Create a new job instance.
*
* @param array $map
* @param string $input_file
*/
public function __construct(array $map, string $input_file){
//
$this->map = $map;
$this->input_file = $input_file;
}
/**
* Execute the job.
*
* @return void
* @throws \Exception
*/
public function handle(){
$almost_timeout = $this->timeout - 100;
$map = $this->map;
$input_file = $this->input_file;
$cmds = '(' . implode('; ', config('fuqu-ffmpeg.command')) . ')';
$cmds = str_replace(array_keys($map), array_values($map), $cmds);
Log::debug($cmds);
$process_limit = config(ServiceProvider::SHORT_NAME .'.process_limit');
Redis::funnel('ffmpeg')->limit($process_limit)->then(
function () use ($cmds, $input_file, $almost_timeout){
$process = new BackgroundProcess($cmds);
$process->run();
if(!$process->isRunning()){
throw new \Exception('Unable to execute file processing command on ' . $input_file);
}
/**
* This doesn't prevent an additional
* background process from spawning
* but it does give a head start
*/
$slept = 0;
do{
sleep(10);
$slept += 10;
}while($process->isRunning() and ($slept < $almost_timeout));
}, function (){
// Could not obtain lock...
return $this->release(100);
});
}
/**
* Rather than doing x tries,
* just keep trying until.
*
* @return \DateTime
*/
public function retryUntil(){
return now()->addDays(1);
}
}
原来问题中的代码确实有效。我的问题在 bg 过程中。处理以数字结尾的文件时遇到问题。我想我会留下代码...可能对某人有用。