Я знаю, что это распространенная ошибка, и, скорее всего, на нее был дан ответ, но проблема, с которой я сталкиваюсь, заключается в том, что когда функция выполняется, она возвращает переменную $down_time
как null, когда новый веб-сайт становится недоступным в этой строке $diff_in_seconds = $now_time->getTimestamp() - $down_time->getTimestamp();
однако, когда я снова запускаю функцию в своем терминале, ошибка исчезает. По сути, он возвращает переменную $down_time
как null перед обновлением в базе данных, что меня смущает, поскольку сначала выполняется запрос на обновление. Я был бы очень признателен за помощь и руководство, спасибо.
public static function run()
{
$dept_emails = array(
19 => 'test@email.co.uk'
);
$active_clients = Client::get_active_clients();
foreach ($active_clients as $client) {
$websites = $client->websites;
$services = $client->services;
foreach ($websites as $website) {
$website_url = $website->url;
$website_url_parse = parse_url($website_url);
$new_website_url = "https://" . $website_url;
$website_status = Client::get_website_status($new_website_url);
if(array_key_exists('host', $website_url_parse)) {
$website_url = $website_url_parse['host'];
}
if($website_status == false) {
$new_website_url = "http://" . $website_url;
$website_status = Client::get_website_status($new_website_url);
}
$now = date('Y-m-d H:i:s');
$now_time = DateTime::createFromFormat('Y-m-d H:i:s', $now);
$down_time = DateTime::createFromFormat('Y-m-d H:i:s', $website->down_at);
$result = DB::update('clients_websites')
->set(array(
'last_checked' => $now
))
->where('id', '=', $website->id)
->execute();
if ($website_status) {
// Website is back online, send email
if($website->down_at != null && $website_status == true){
$diff_in_seconds = $now_time->getTimestamp() - $down_time->getTimestamp();
// Website has been down for more than 5 minutes
if($diff_in_seconds >= 300){
Cli::write("Emailing: " . $website->url . " is back online");
$result = DB::update('clients_websites')
->set(array(
'down_at' => null,
'up_at' => date('Y-m-d H:i:s'),
))
->where('id', '=', $website->id)
->execute();
$notify_emails = array();
foreach ($services as $service)
{
$service_id = $service->service_id;
if (in_array($service_id, $dept_emails))
{
$notify_emails[] = $dept_emails[$service_id];
}
}
$message = "Hi " . $website->url . " is back online.";
$email = Model_Mail::send_email($dept_emails, "" . $website->url . " is back online", $message);
}
} else {
// Website is online insert up time in to DB
Cli::write("Online: " . $website->url . " is online");
$result = DB::update('clients_websites')
->set(array(
'down_at' => null,
'up_at' => date('Y-m-d H:i:s'),
))
->where('id', '=', $website->id)
->execute();
}
} else {
// Website is down insert down time in to DB
Cli::write("Offline: " . $website->url . " is offline");
if(is_null($website->down_at)){
$result = DB::update('clients_websites')
->set(array(
'down_at' => date('Y-m-d H:i:s')
))
->where('id', '=', $website->id)
->execute();
$query = DB::select('*')->from('clients_websites')->where('id', '=', $website->id)
->and_where_open()
->where('down_at', 'IS NOT', NULL)
->and_where_close()
->execute();
$down_time = DateTime::createFromFormat('Y-m-d H:i:s', $query['down_at']);
$down_time_email = $down_time->format('d-m-Y H:i:s');
$diff_in_seconds = $now_time->getTimestamp() - $down_time->getTimestamp();
if($diff_in_seconds >= 300){
Cli::write("Emailing: " . $website->url . " is offline");
$notify_emails = array();
foreach ($services as $service)
{
$service_id = $service->service_id;
$active = $service->end_date;
if (in_array($service_id, $dept_emails ) && ($active == null || $active > $now))
{
$notify_emails[] = $dept_emails[$service_id];
}
}
$message = "Hi " . $website->url . " has been down since " . $down_time_email . ".";
$email = Model_Mail::send_email($dept_emails, "" . $website->url . " is down", $message);
$result = DB::update('clients_websites')
->set(array(
'down_email_sent' => date('Y-m-d H:i:s')
))
->where('id', '=', $website->id)
->execute();
}
}
}
}
}
}