我制作了一个检查服务器可用性的脚本。
该网站已关闭,我正在等待修复(我正在等待客户端正在等待来自提供商的机票),以限制我使用的呼叫sleep():
$client = new \GuzzleHttp\Client();
$available = false;
date_default_timezone_set('doesntMatter');
//The server was more likely to respond after 5 AM, hence the decrese between intervals
$hours = array( //Minutes between calls based on current hour
0=>30,
1=>30,
2=>30,
3=>30,
4=>20,
5=>20,
6=>10,
7=>10,
8=>10
);
$lastResponse = null;
while(!$available) {
$time = time();
$hour = date('G', $time);
echo "\n Current hour ".$hour;
try {
$crawler = $client->request('GET', 'www.someSiteToCheck.com');
$available = true; //When the server returns a stus code of 200 available is set to TRUE
} catch (\GuzzleHttp\Exception\ServerException $e) {}
if(!$available) {
$secondsToSleep = $hours[$hour]*60;
echo "\n Sleeping for ".$secondsToSleep;
sleep($hours[$hour]*$secondsToSleep); //Sleep until the next request
} else {
exec('start ringtone.mp3'); //Blast my stereo to wake me up
}
}
当我开始编写脚本时,它进入1800秒睡眠并冻结,它没有重新执行任何操作
答案 0 :(得分:3)
我认为你的逻辑有误。
例如:
当它是凌晨5点
那么$secondsToSleep
是20*60
= 1200sec;
当您调用睡眠功能时,再次将其与20相乘
sleep($hours[$hour]*$secondsToSleep);
=> sleep(20*1200);
=> 24000sec
=> 6,66... hours
答案 1 :(得分:0)
如果您只是更新睡眠参数,结果应该符合预期。
if(!$available) {
$secondsToSleep = $hours[$hour]*60;
echo "\n Sleeping for ".$secondsToSleep;
sleep($secondsToSleep); //Sleep until the next request
}