Reputation: 21830
We are using an application server that supplies our data.
Sometimes the connection to the appserver takes a long time to respond, but there is not an actual connection failure (and the slowness registers as downtime on pingdom, even though its connected successfully)
How can I time the connection in php so that way I can log a slow-to-respond event after a certain amount of time (even if the connection has not responded yet)?
Upvotes: 1
Views: 132
Reputation: 53472
One idea came to my head. At the beginning of the request, you could set up a database entry or a file that contains the request that you've made, similar to the suggestion that Rawr made. Then, you could trigger unix at command from PHP to be run after your limit has passed - that command would then activate a shell or a PHP script that would check the existance of the file/database entry and act accordingly.
This is equally hacky, but it would have the benefit of not having extra script running and polling stuff all the time, instead activating just once.
Upvotes: 1
Reputation: 2224
Alright although, I must agree with eis on this one. PHP probably isn't the best language to be doing this in, here's my suggestion...
First File:
//Write "1" to text file (loading.txt)
//Run secondary php file (loading.php) asyncronously
//I would suggest curl_post_async or exec(url)
//Start Connection
//Upon load finish write "0" to text file (loading.txt)
Here's a nice discussion about requesting a page without waiting for it to load: php request url without waiting for response
Second File (loading.php):
$start=mktime();
while (true) {
if ("loading.txt" if value = 0) {
break;
else if (($start-mktime()) > MAX WAIT TIME) {
//Log as "slow-to-respond"
break;
}
}
You will prob need to modify your php.ini and set the max execution time accordingly.
Again I feel like this is kinda sloppy. I really would suggest using some other language.
Basically, right before you start the connection, you run this other php file which continues to loop until either 1. it reads from the loading.txt file that you've finished the connection OR 2. you're ready to log it as a "slow-to-respond" instance. In which case the 2nd php file ends and you've achieved your desired result.
Upvotes: 1