Skip to content

Commit

Permalink
added db healtcheck console command
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Jun 30, 2018
1 parent 25511cd commit b8ac39a
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions console/controllers/HealthcheckController.php
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;
}
}

0 comments on commit b8ac39a

Please sign in to comment.