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;
}
}
我会检查代码并回复您。你是对的Iam只发送到一个松散的通道,其中的终点(网络钩子)是硬编码在config/slack.php。但我不知道如何覆盖它。有什么建议么? –