4 * @copyright Copyright (C) 2010-2021, the Friendica project
6 * @license GNU AGPL version 3 or any later version
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * This script tries to connect to a database for a given interval
22 * Useful in case of installation e.g. to wait for the database to not generate unnecessary errors
24 * Usage: php bin/wait-for-connection {HOST} {PORT} [{TIMEOUT}]
27 use Friendica\Network\HTTPException\ForbiddenException;
29 if (php_sapi_name() !== 'cli') {
30 throw new ForbiddenException();
36 $timeout = (float)$argv[3];
39 $port = (int)$argv[2];
42 fwrite(STDERR, 'Usage: '.$argv[0].' host port [timeout]'."\n");
46 fwrite(STDERR, 'Timeout must be greater than zero'."\n");
50 fwrite(STDERR, 'Port must be an integer greater than zero'."\n");
53 $socketTimeout = (float)ini_get('default_socket_timeout');
54 if ($socketTimeout > $timeout) {
55 $socketTimeout = $timeout;
57 $stopTime = time() + $timeout;
59 $sock = @fsockopen($host, $port, $errno, $errstr, $socketTimeout);
60 if ($sock !== false) {
67 } while (time() < $stopTime);