2019-09-16 14:47:49 +02:00
|
|
|
#!/usr/bin/php
|
|
|
|
<?php
|
2020-02-09 16:34:23 +01:00
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 16:34:23 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2020-02-02 22:27:05 +01:00
|
|
|
* This script tries to connect to a database for a given interval
|
|
|
|
* Useful in case of installation e.g. to wait for the database to not generate unnecessary errors
|
|
|
|
*
|
|
|
|
* Usage: php bin/wait-for-connection {HOST} {PORT} [{TIMEOUT}]
|
|
|
|
*/
|
|
|
|
|
2021-10-30 01:21:07 +02:00
|
|
|
use Friendica\Network\HTTPException\ForbiddenException;
|
|
|
|
|
2020-09-07 11:51:26 +02:00
|
|
|
if (php_sapi_name() !== 'cli') {
|
2021-10-30 01:21:07 +02:00
|
|
|
throw new ForbiddenException();
|
2020-09-07 11:51:26 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 14:47:49 +02:00
|
|
|
$timeout = 60;
|
|
|
|
switch ($argc) {
|
|
|
|
case 4:
|
|
|
|
$timeout = (float)$argv[3];
|
|
|
|
case 3:
|
|
|
|
$host = $argv[1];
|
|
|
|
$port = (int)$argv[2];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fwrite(STDERR, 'Usage: '.$argv[0].' host port [timeout]'."\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if ($timeout < 0) {
|
|
|
|
fwrite(STDERR, 'Timeout must be greater than zero'."\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if ($port < 1) {
|
|
|
|
fwrite(STDERR, 'Port must be an integer greater than zero'."\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
$socketTimeout = (float)ini_get('default_socket_timeout');
|
|
|
|
if ($socketTimeout > $timeout) {
|
|
|
|
$socketTimeout = $timeout;
|
|
|
|
}
|
|
|
|
$stopTime = time() + $timeout;
|
|
|
|
do {
|
|
|
|
$sock = @fsockopen($host, $port, $errno, $errstr, $socketTimeout);
|
|
|
|
if ($sock !== false) {
|
|
|
|
fclose($sock);
|
|
|
|
fwrite(STDOUT, "\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
fwrite(STDOUT, '.');
|
|
|
|
} while (time() < $stopTime);
|
|
|
|
fwrite(STDOUT, "\n");
|
2019-09-23 15:36:16 +02:00
|
|
|
exit(1);
|