Last active
August 8, 2016 12:13
-
-
Save airatkh/9e02437b281bfb842a09878dae385db7 to your computer and use it in GitHub Desktop.
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
class DefaultController extends Controller | |
/** | |
* Data settings from Database: | |
* | |
* wait_timeout 28800 | |
* connect_timeout 10 | |
* interactive_timeout 28800 | |
* | |
* @var integer seconds | |
*/ | |
const DB_CONNECTION_TIMEOUT = 599; | |
/** | |
* Add param for component logger. Logger will log data in real time. | |
* | |
* Refresh Last Db ConnectionDataTime. | |
*/ | |
public function init() | |
{ | |
\Yii::getLogger()->flushInterval = 1; | |
$this->refreshLastDbConnectionDataTime(); //First start Console application | |
parent::init(); | |
} | |
/** | |
* Refresh information about last connection to DateBase. | |
*/ | |
protected function refreshLastDbConnectionDataTime() | |
{ | |
$this->last_db_conn_data_time = new \DateTime('now'); | |
} | |
/** | |
* Check timeout between last use database/mongodb connections. | |
* | |
* If connection "OLD"(delta time big), | |
* reconnect database/mongodb connections. | |
* | |
* @throws \Exception | |
*/ | |
protected function checkDbConnectionAndTimeoutDbManipulation() | |
{ | |
if ($this->isLastDbConnectionDataTimeOld()) { | |
$this->reconnectDbConnection(); | |
} else { | |
$this->refreshLastDbConnectionDataTime(); | |
} | |
} | |
/** | |
* Check interval between Last DB connection and Actual DateTime(NOW). | |
* Compare interval with constant. | |
* | |
* @return bool If OLD db connection TRUE |otherwise FALSE. | |
*/ | |
protected function isLastDbConnectionDataTimeOld() | |
{ | |
$last_db_conn_data_time = $this->last_db_conn_data_time; | |
$now_data_time = new \DateTime('now'); | |
$delta_last_db_conn = $now_data_time->getTimestamp() - $last_db_conn_data_time->getTimestamp(); | |
return $delta_last_db_conn >= self::DB_CONNECTION_TIMEOUT; | |
} | |
/** | |
* Reconnect (refresh) Database connection. | |
* @throws \Exception | |
*/ | |
protected function reconnectDbConnection() | |
{ | |
try { | |
$msg = 'Start reconnection database connection.'; | |
\Yii::info($msg, get_class($this)); | |
/** @var \yii\db\Connection $db2 */ | |
$db2 = Yii::$app->get('db2'); | |
$db2->close(); | |
$db2->open(); | |
/** @var \yii\db\Connection $mongodb */ | |
$mongodb = Yii::$app->get('mongodb'); | |
$mongodb->close(); | |
$mongodb->open(); | |
$this->refreshLastDbConnectionDataTime(); | |
} catch (\Exception $e) { | |
$msg = 'Error happen while reconnect database connection. System error: ' . $e->getMessage(); | |
\Yii::error($msg, get_class($this)); | |
throw $e; | |
} | |
} | |
class ExampleWorker extends DefaultController | |
{ | |
public function actionStart(AMQPMessage $msg){ | |
$this->checkDbConnectionAndTimeoutDbManipulation(); | |
//........ | |
//TO DO USEFUL WORK | |
//........ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment