3 # This script tries to connect to a database for a given interval
4 # Useful in case of installation e.g. to wait for the database to not generate unnecessary errors
6 # Usage: php bin/wait-for-connection {HOST} {PORT} [{TIMEOUT}]
12 $timeout = (float)$argv[3];
15 $port = (int)$argv[2];
18 fwrite(STDERR, 'Usage: '.$argv[0].' host port [timeout]'."\n");
22 fwrite(STDERR, 'Timeout must be greater than zero'."\n");
26 fwrite(STDERR, 'Port must be an integer greater than zero'."\n");
29 $socketTimeout = (float)ini_get('default_socket_timeout');
30 if ($socketTimeout > $timeout) {
31 $socketTimeout = $timeout;
33 $stopTime = time() + $timeout;
35 $sock = @fsockopen($host, $port, $errno, $errstr, $socketTimeout);
36 if ($sock !== false) {
43 } while (time() < $stopTime);