-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25511cd
commit b8ac39a
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace console\controllers; | ||
|
||
use Yii; | ||
use yii\console\Controller; | ||
use yii\helpers\Console; | ||
|
||
/** | ||
* @author Gani Georgiev <gani.georgiev@gmail.com> | ||
*/ | ||
class HealthcheckController extends Controller | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public $color = true; | ||
|
||
/** | ||
* Checks whether the DB connection is established. | ||
* | ||
* @example | ||
* ```bash | ||
* php yii healthcheck/db | ||
* ``` | ||
* | ||
* @return integer | ||
*/ | ||
public function actionDb() | ||
{ | ||
try { | ||
Yii::$app->db->open(); | ||
|
||
if (Yii::$app->db->isActive) { | ||
$this->stdout('DB connection is established.', Console::FG_GREEN); | ||
$this->stdout(PHP_EOL); | ||
|
||
Yii::$app->db->close(); | ||
|
||
return self::EXIT_CODE_NORMAL; | ||
} | ||
} catch (\Exception $e) { | ||
$this->stdout($e->getMessage(), Console::FG_RED); | ||
$this->stdout(PHP_EOL); | ||
} | ||
|
||
$this->stdout('Cannot connect to DB.', Console::BG_RED); | ||
$this->stdout(PHP_EOL); | ||
|
||
return self::EXIT_CODE_ERROR; | ||
} | ||
} |