Skip to content

Instantly share code, notes, and snippets.

@raselupm
Created October 16, 2020 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raselupm/ef8702f2faf17a0fc19c327b01860380 to your computer and use it in GitHub Desktop.
Save raselupm/ef8702f2faf17a0fc19c327b01860380 to your computer and use it in GitHub Desktop.
<?php
namespace App\Jobs;
use App\Models\Domain;
use App\Models\Event;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Exception;
class CreateEvent implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
$domains = Domain::all();
foreach ($domains as $domain) {
// checking a single domain
try {
$ping = Http::get('http://' . $domain->name);
$crawlerError = false;
} catch (Exception $e) {
$crawlerError = true;
}
if($ping->successful() && $crawlerError == false) {
// domain is up | checking last previous record
$checkLatestEvent = $domain->events()->latest()->get()->first();
// last record is not up, so adding a new one
if($checkLatestEvent->type != 1) {
$event = new Event();
$event->type = 1; // 1 = up
$event->reason = 1; // 1 = 200 ok
$event->save();
$domain->events()->attach($event->id);
// todo email admin about site is up
}
} else {
if($ping->failed()) {
$reason = 2; // 2 = forbidden error
} elseif($ping->clientError()) {
$reason = 3; // 3 = client error
} else {
$reason = 4; // 4 = server error
}
// domain is down | checking last previous record
$checkLatestEvent = $domain->events()->latest()->get()->first();
// last record is not down, so adding a new one
if($checkLatestEvent->type != 2) {
$event = new Event();
$event->type = 2; // 2 = down
$event->reason = $reason;
$event->save();
$domain->events()->attach($event->id);
// todo email admin about site is down
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment