]> git.mxchange.org Git - friendica.git/blob - bin/wait-for-connection
Merge pull request #8215 from nupplaphil/task/extract_email
[friendica.git] / bin / wait-for-connection
1 #!/usr/bin/php
2
3 <?php
4
5 /*
6  * This script tries to connect to a database for a given interval
7  * Useful in case of installation e.g. to wait for the database to not generate unnecessary errors
8  *
9  * Usage: php bin/wait-for-connection {HOST} {PORT} [{TIMEOUT}]
10  */
11
12 $timeout = 60;
13 switch ($argc) {
14         case 4:
15                 $timeout = (float)$argv[3];
16         case 3:
17                 $host = $argv[1];
18                 $port = (int)$argv[2];
19                 break;
20         default:
21                 fwrite(STDERR, 'Usage: '.$argv[0].' host port [timeout]'."\n");
22                 exit(2);
23 }
24 if ($timeout < 0) {
25         fwrite(STDERR, 'Timeout must be greater than zero'."\n");
26         exit(2);
27 }
28 if ($port < 1) {
29         fwrite(STDERR, 'Port must be an integer greater than zero'."\n");
30         exit(2);
31 }
32 $socketTimeout = (float)ini_get('default_socket_timeout');
33 if ($socketTimeout > $timeout) {
34         $socketTimeout = $timeout;
35 }
36 $stopTime = time() + $timeout;
37 do {
38         $sock = @fsockopen($host, $port, $errno, $errstr, $socketTimeout);
39         if ($sock !== false) {
40                 fclose($sock);
41                 fwrite(STDOUT, "\n");
42                 exit(0);
43         }
44         sleep(1);
45         fwrite(STDOUT, '.');
46 } while (time() < $stopTime);
47 fwrite(STDOUT, "\n");
48 exit(1);