Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Fluber's avatar

Laravel job check site if online

Hello. On every post I have url a website. I need get status and check every hour if this site is online. How I can do it with queues Laravel? I have a job CheckPostSite:

public function handle()
{
    $allposts = Post::all();

    foreach($allposts as $post) {
         if($this->checkOnline($post->site_url) {
              $post->website_online = true;
              $post->save();
         }
    }
}

private function checkOnline($domain) {
    $curlInit = curl_init($domain);
    curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
    curl_setopt($curlInit,CURLOPT_HEADER,true);
    curl_setopt($curlInit,CURLOPT_NOBODY,true);
    curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);

    //get answer
    $response = curl_exec($curlInit);

    curl_close($curlInit);
    if ($response) return true;
    return false;
}

I'm doing everything right? Or not? I have 10000+ posts in database.

0 likes
20 replies
Ricardo's avatar

@dronax I think you need a Task that should run every hour and dispatch a job for each of the posts.

Your task:

foreach(Post::all() as $post) {
    CheckIfOnline::dispatch($post);
}

in App\Jobs\CheckIfOnline:

public function handle(Post $post)
{
    if ($this->checkOnline($post->site_url) {
        $post->website_online = true;
        $post->save();
    }
}

private function checkOnline($domain) {
...
Fluber's avatar

@RICARDO - Where I need do this:

foreach(Post::all() as $post) {
   CheckIfOnline::dispatch($post);
}

in controller?

Fluber's avatar

@RICARDO

 /**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
       foreach(Post::all() as $post) {
    CheckIfOnline::dispatch($post);
}
    })->hourly();
}

I right?

1 like
Snapey's avatar

I would consider using unique on the URL - or at least do it once to see how much duplication there is. No point keep checking the same site?

If you do have 10,000 sites to check, thats going to take quite a long time.

Fluber's avatar

@SNAPEY - You're right. How I can do this correctly? In database all sites are different. But I have more 10000+ different sites. I need to check every site if it's online, or at least once per day..

Snapey's avatar

is the purpose to actually check that the site is online, or is it to just avoid showing the user a dead link? If its offline what then?

Fluber's avatar

@SNAPEY - I need display only if site is online :) In every post, I have a status (Online, Offline) of site this post. And yes, I don't need to show user a dead link..

Snapey's avatar

So you don't need to update the site status in the background?

I'm just wondering if you could list all the sites against the user's enquiry and show online? wait... and then either check the site from the browser or check from Laravel following an ajax request, replacing wait with Yes or No.

Then you check the site and return the status to the client. A page with 20-30 websites listed could all be updated with their status as each query returns in real-time.

If your number of visitors is low compared to the number of sites, this would be way more efficient. You are not checking the status of sites around the clock that noone is ever going to see (a lot of wasted effort) and a lot of fake traffic for those sites.... or is that the point?

Fluber's avatar

@SNAPEY - A lot of fake traffic for those sites - Yes. I need remove fake sites..

Snapey's avatar

Fake sites? thats then a different requirement...

Fluber's avatar

@SNAPEY - I can with queues check it's status if online or not? If not, then I can do some actions..

This Code:

protected function schedule(Schedule $schedule)
{
     $schedule->call(function () {
        foreach(Post::all() as $post) {
    CheckIfOnline::dispatch($post);
}
})->hourly();
}

is Good?

Snapey's avatar

Its good, but thats 3 checks per second, 24 hours a day, whether anyone needs the information or not.

What happens if after 1 hour you still have 500 left to check?

The next hour, you now have 10,500 to check, and after that hour 11,050 to check and so on until the queue fills up and your server crashes.

Snapey's avatar

I suggested. Only check when someone is actually wanting the result.

However, you then went on to talk about checking for fake sites which seems a different problem to me

Fluber's avatar

@SNAPEY - I have page of post, there I show site status. How I can do it, with queues?

Snapey's avatar

It might be the language barrier, but you are not listening to anything I am replying with. So good luck.

Fluber's avatar

@SNAPEY - I need do monitor activity for sites in my database. How I can do it without queues ?

Please or to participate in this conversation.