1
私のコードはhttp
リクエストの送信時に完全に動作しています。ウェブサイトがダウンしているか、レスポンスコードが200
でない場合は、スラック通知を送信します。私が今理解しようとしているのは、通知テーブルに私がcheck_frequency
とalert_frequence
を持っているということです。ウェブサイトがダウンしている場合、確認頻度を使用して経過時間を計算する代わりに、alert_frequence
を使用する必要があります。http応答に基づくアラート頻度の変更
namespace App\Http\Controllers;
use GuzzleHttp\Client;
use App\Utilities\Reporter;
use GuzzleHttp\Exception\ClientException;
use App\Notification;
use App\Status;
use App\Setting;
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();
}
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)
{
$status_health = $notification->status('health');
$frequency = $this->getFrequency($notification);
$elapsed_time = \Carbon\Carbon::parse($status_health['timestamp'])->diffInMinutes();
if($elapsed_time >= $frequency) {
$response = $this->client->get($notification->website_url, [
'http_errors' => false
]);
$resCode = $response->getStatusCode();
$notification->statuses()->attach($status, [
'values' => $resCode === 200 ? 'up' : 'down'
]);
if($resCode != 200){
/* how to send slack to different slach channels, now it is sending only to one channel!*/
$this->reporter->slack($notification->website_url.':'.' is down'. ' please check your email for the status code!'.' @- '.$notification->email,
$notification->slack_channel);
$this->reporter->mail($notification->email,$resCode);
}
}
}
private function getFrequency(Notification $notification)
{
return isset($notification->check_frequency)
? intval($notification->check_frequency)
: $this->default_check_frequency;
}
}
私はコードをチェックして回答します。 config/slack.phpでエンドポイント(Webフック)がハードコーディングされている1つのスラックチャンネルにのみ送信します。しかし、私はどのようにそれを無効にするか分かりません。助言がありますか? –