pixelfed-container/wait-for-db.php

37 lines
953 B
PHP
Raw Permalink Normal View History

2020-05-30 12:46:57 +02:00
<?php
2021-10-29 21:00:54 +02: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 12:46:57 +02:00
$counter = 10;
2020-05-30 13:08:08 +02:00
$count = 1;
2020-05-30 12:46:57 +02:00
while (!$conn) {
echo("Waiting for Database... $count / $counter\n");
sleep(2);
2021-10-29 21:00:54 +02:00
$conn = connect();
2020-05-30 12:46:57 +02:00
$count++;
2021-10-29 21:00:54 +02:00
if ($count == $counter) {
2020-05-30 12:46:57 +02:00
echo("Database did not respond after $counter tries, giving up\n");
die(1);
}
}
echo "Database is up\n";
?>