Before I dig into the issue, I am curious why you start it from a controller if it needs to be run every 10 minutes? https://laravel.com/docs/9.x/scheduling#main-content
Question about queues
Hello, I'm trying to create a queue that pings a list of ip addresses stored in my DB (I need to do it every 10 minutes, so i'll use a cron).
Here, I dispatch the job in my controller for each IP addresses :
public function ping()
{
$pings = Ping::all();
foreach($pings as $ping) {
dispatch(new PingJob($ping));
}
}
And here is the job (I do it using curl) :
class PingJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $ping;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Ping $ping)
{
$this->ping = $ping;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$ping = $this->ping;
$url= $ping->ip_address;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$health = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($health) {
// responds ...
} else {
// doesn't respond ...
}
}
}
The issue is that I put a 5 seconds timeout to check if there is a response. That means that if an IP is not responding, the server will wait 5 seconds before pinging the next one and that's a big problem if I have like 1000 IP addresses that are not responding, the queue will take at least 5000 seconds to execute completely. So, is there any way to run all the pings simultaneously so that it doesn't wait 5 seconds to execute the next job ?
Thanks for your help
I think you're looking for this
- https://laravel.com/docs/9.x/queues#running-multiple-queue-workers
- https://laravel.com/docs/9.x/queues#supervisor-configuration
You can run multiple workers simultaneously (you may be interested in the "numprocs" ).
Please or to participate in this conversation.