根据支票频率发送http请求?

时间:2016-12-29 11:01:03

标签: php laravel guzzle

我写了一个工作代码,它将http请求发送到网站并将响应代码存储到数据库中。它很酷。但它有一个问题,在它发送http请求之前,首先它应该检查check_frequency,如果检查频率过期它应该发送http请求到该网站,否则,它应该跳过该网站而不发送http请求。我的代码如下

<?php
namespace App\Http\Controllers;

use \GuzzleHttp\Client;
use App\Utilities\Reporter;
use GuzzleHttp\Exception\ClientException;
use App\Notification;
use App\Status;
use App\Setting;
use Carbon;
class GuzzleController extends Controller
{
    private $default_check_frequency;

    protected $client;

    protected $reporter;

    public function __construct()
    {
        $this->client = new Client();

        $this->reporter = new Reporter;

        $this->default_check_frequency = Setting::defaultCheckFrequency();


    }

    private function addStatusToNotification(Notification $notification, Status $status, $resCode)
    {
        $notification->statuses()->attach($status, [
            'values' => strval($resCode)
            ]);
    }

    private function report(Notification $notification, $resCode)
    {
     if(empty($resCode)){
        $resCode = "no response found";
    }

    if(Notification::health($resCode) !== 'up'){
        $this->reporter->slack($notification->website_url . ':' . '  is down' . ' this is the status code!' . ' @- ' .$resCode,
            $notification->slack_channel);

        $this->reporter->mail($notification->email,$notification->website_url.' is down '. ' this is the status Code: '. $resCode);

    }else{
        $this->reporter->slack($notification->website_url . ':' . '  is up' . ' this is the status code!' . ' @- ' .$resCode,
            $notification->slack_channel);
    }
}

private function sendNotification(Notification $notification, Status $status, $status_health, $frequency, $resCode)
{
    $notify = empty($status_health['timestamp']);
    $elapsed_time = \Carbon\Carbon::parse($status_health['timestamp'])->diffInMinutes();



    if ($notify || $elapsed_time >= $frequency) {
        $this->addStatusToNotification($notification, $status, $resCode);
    }
    if(Notification::health($resCode) ==='up'){              
        if(Notification::health($resCode) <> Notification::health($status_health['value'])){  
        var_dump("1",Notification::health($resCode),Notification::health($status_health['value']));             
            $this->report($notification, $resCode);  
        }             
    }else{
        if(Notification::health($resCode) <> Notification::health($status_health['value'])){
             var_dump("2",Notification::health($resCode),Notification::health($status_health['value']));
            $this->report($notification, $resCode);
        }else{
            if($notify || $elapsed_time >= $notification->alert_frequency){
             $this->report($notification, $resCode);  
         }
     }  
 } 
}


public function status()
{
    $notifications = Notification::where('active', 1)->get();

    $status = Status::where('name', 'health')->first();

    foreach ($notifications as $notification) {
        $this->updateStatus($notification, $status);
    }
}

private function updateStatus(Notification $notification, Status $status)
{
    $resCode = $this->getStatusCode($notification->website_url);

    $this->sendNotification(
        $notification,
        $status,
        $notification->status('health'),
        $this->getFrequency($notification,$resCode),
        $resCode
        );
}

private function getFrequency(Notification $notification) 
{           

    return isset($notification->check_frequency)
    ? intval($notification->check_frequency)
    : $this->default_check_frequency;                
}

private function getStatusCode($url)
{
    try {   
         // add here the check frequency thing before sending http request    
        $response = $this->client->get($url, [
            'http_errors' => false
            ]);
        return $response->getStatusCode();  
    } catch (\GuzzleHttp\Exception\ConnectException $e) {   

    }
    }
}
我想弄清楚在哪里检查。如果经过的时间大于检查频率,则发送http请求,如果没有跳过该网站。我会帮忙吗?

0 个答案:

没有答案