Q:

Php Laravel - What's the best way to create a loop with polling external servers?

I need your advice. I have to do a function that will send queries to external servers every few minutes, which ip addresses will be obtained from the database.
My two ideas:

every 5 minutes, select all servers from the database, and then pass them all through one loop in which I will send queries to these servers and I will receive a small portion of return data, which I will then save individually in the database.
Every minute, the script will select several hundred servers that were polled more than 5 minutes ago (this will be written in the column in the table with servers)
Which way is better? Or maybe you have some ideas of your own? And in the loop I have to add saving data to the database or is there any way to save the data after looping all at once?

For starters, I'll have about 3000-4000 records. Over time, there will be more and more of them. I use Laravel.
I would be grateful for any tips. Sorry for my English.

A:

The best way to do this is with queues. Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time. Deferring these time consuming tasks drastically speeds up web requests to your application.
In the job constructor you can pass the ip of the server on which you want to extract the data and it makes the logic inside the job. Without the need of any loop, the Jobs will be executed one by one or as many as you wish to be executed at the same time. I have done this about 5 times and in my opinion Laravel provides an extremely powerful tool.
A quick example would be.
foreach($ips as $ip) {
    \App\Jobs\ExtractInfoFromServer::dispatch($ip);
}

And in your Job
<?php

namespace App\Jobs;

class ExtractInfoFromServer implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $ip;

    /**
     * Create a new job instance.
     *
     * @param  string $ip
     * @return void
     */
    public function __construct(string $ip)
    {
        $this->ip = $ip;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //Send the request to server ip
    }
    ...

You can read more about the queues in the official documentation.

