Skip to content

Commit 6804c83

Browse files
committed
extracted wait-for-connection action
1 parent b368a85 commit 6804c83

File tree

1 file changed

+49
-32
lines changed

1 file changed

+49
-32
lines changed

console/controllers/MysqlController.php

+49-32
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,44 @@ public function actionIndex()
9696
$this->stdout("\n");
9797
}
9898

99+
100+
public function actionWaitForConnection(
101+
$dsn = null,
102+
$user = null,
103+
$password = null
104+
) {
105+
$dsn = $dsn ?: getenv("DATABASE_DSN_BASE");
106+
$user = $user ?: getenv("DB_ENV_MYSQL_ROOT_USER");
107+
$password = $password ?: getenv("DB_ENV_MYSQL_ROOT_PASSWORD");
108+
109+
if (empty($user) || empty($password) || empty($dsn)) {
110+
$this->stderr('Configuration failed, aborting.');
111+
return;
112+
}
113+
114+
// trying to connect to database with PDO (20 times, interval 1 second)
115+
$this->stdout(
116+
"Checking database connection on DSN '{$dsn}' with user '{$user}'"
117+
);
118+
119+
try {
120+
// retry an operation up to 20 times
121+
$pdo = \igorw\retry(
122+
$this->mysqlRetryMaxCount,
123+
function () use ($dsn, $user, $password) {
124+
$this->stdout('.');
125+
sleep($this->mysqlRetryTimeout);
126+
return new \PDO($dsn, $user, $password);
127+
}
128+
);
129+
} catch (FailingTooHardException $e) {
130+
$this->stderr("\n\nError: Unable to connect to database '".$e->getMessage()."''");
131+
\Yii::$app->end(1);
132+
}
133+
$this->stdout(' [OK]'.PHP_EOL);
134+
135+
}
136+
99137
/**
100138
* Create MySQL database
101139
*
@@ -120,63 +158,42 @@ public function actionCreate(
120158
$user = null,
121159
$pass = null
122160
) {
161+
162+
$db = $db ?: getenv("DATABASE_DSN_DB");
163+
$dsn = $dsn ?: getenv("DATABASE_DSN_BASE");
164+
$root = $root ?: getenv("DB_ENV_MYSQL_ROOT_USER");
165+
$rootPassword = $rootPassword ?: getenv("DB_ENV_MYSQL_ROOT_PASSWORD");
166+
$user = $user ?: getenv("DB_ENV_MYSQL_USER");
167+
$pass = $pass ?: getenv("DB_ENV_MYSQL_PASSWORD");
168+
123169
// check dsn
124-
if ($db === null) {
125-
$db = getenv("DATABASE_DSN_DB");
126-
}
127170
if (empty($db)) {
128171
$this->stderr('No database configured, aborting.');
129172
return;
130173
}
131-
132174
// check root user settings
133-
$root = $root ?: getenv("DB_ENV_MYSQL_ROOT_USER");
134175
if (empty($root)) {
135176
$this->stderr('No root user configured, aborting.');
136177
return;
137178
}
138-
$rootPassword = $rootPassword ?: getenv("DB_ENV_MYSQL_ROOT_PASSWORD");
139179
if (empty($rootPassword)) {
140180
$this->stderr('No root password configured, aborting.');
141181
return;
142182
}
143-
144-
$user = $user ?: getenv("DB_ENV_MYSQL_USER");
145-
$pass = $pass ?: getenv("DB_ENV_MYSQL_PASSWORD");
146-
$dsn = $dsn ?: getenv("DATABASE_DSN_BASE");
147-
148183
if (empty($user) || empty($pass) || empty($dsn)) {
149184
$this->stderr('Configuration failed, aborting.');
150185
return;
151186
}
152187

153-
// trying to connect to database with PDO (20 times, interval 1 second)
154-
$this->stdout(
155-
"Checking database connection on DSN '{$dsn}' with user '{$root}'"
156-
);
157-
158-
try {
159-
// retry an operation up to 20 times
160-
$pdo = \igorw\retry(
161-
$this->mysqlRetryMaxCount,
162-
function () use ($dsn, $root, $rootPassword) {
163-
$this->stdout('.');
164-
sleep($this->mysqlRetryTimeout);
165-
return new \PDO($dsn, $root, $rootPassword);
166-
}
167-
);
168-
} catch (FailingTooHardException $e) {
169-
$this->stderr("\n\nError: Unable to connect to database '".$e->getMessage()."''");
170-
\Yii::$app->end(1);
171-
}
172-
$this->stdout(' [OK]');
173-
188+
// wait for database connection (BC)
189+
$this->actionWaitForConnection($dsn, $root, $rootPassword);
174190

175191
// try to create a database for the user
176192
$this->stdout(
177193
"\nCreating database '{$db}' and granting permissions to user '{$user}' on DSN '{$dsn}' with user '{$root}'"
178194
);
179195
try {
196+
$pdo = new \PDO($dsn, $root, $rootPassword);
180197
\igorw\retry(
181198
$this->mysqlRetryMaxCount,
182199
function () use ($dsn, $root, $rootPassword, $pdo, $user, $pass, $db) {

0 commit comments

Comments
 (0)