pixelfed/wait-for-db.php

37 lines
953 B
PHP
Raw Normal View History

2020-05-30 10:46:57 +00:00
<?php
2021-10-29 19:00:54 +00:00
function connect()
{
$connection = getenv('DB_CONNECTION');
$host = getenv('DB_HOST');
$username = getenv('DB_USERNAME');
$password = getenv('DB_PASSWORD');
$database = getenv('DB_DATABASE');
$port = getenv('DB_PORT');
if ($connection === 'mysql') {
return mysqli_connect($host, $username, $password, $database, $port);
}
if ($connection === 'pgsql') {
return pg_connect('host=' . $host . ' port=' . $port . ' dbname=' . $database . ' user=' . $username . ' password=' . $password);
}
throw new Exception('unsupported connection type ' . $connection);
}
$conn = connect();
2020-05-30 10:46:57 +00:00
$counter = 10;
2020-05-30 11:08:08 +00:00
$count = 1;
2020-05-30 10:46:57 +00:00
while (!$conn) {
echo("Waiting for Database... $count / $counter\n");
sleep(2);
2021-10-29 19:00:54 +00:00
$conn = connect();
2020-05-30 10:46:57 +00:00
$count++;
2021-10-29 19:00:54 +00:00
if ($count == $counter) {
2020-05-30 10:46:57 +00:00
echo("Database did not respond after $counter tries, giving up\n");
die(1);
}
}
echo "Database is up\n";
?>