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.
@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) {
...
@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 - 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..
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?