Skip to content

Commit

Permalink
feat: 新增数据库自动重连机制
Browse files Browse the repository at this point in the history
  • Loading branch information
im050 committed Apr 21, 2019
1 parent 99868ee commit 9ed5ef8
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 39 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -2,7 +2,7 @@
"name": "im050/wechat",
"type": "library",
"description": "基于PHP开发自定义微信机器人,异步消息回复机制,消息记录,撤回消息保留",
"version": "1.3.4",
"version": "1.3.5",
"homepage": "https://github.com/im050/wechat_robot",
"keywords": [
"微信",
Expand Down
12 changes: 12 additions & 0 deletions example/example.php
Expand Up @@ -72,6 +72,18 @@
$fileHelper->sendMessage("hello! " . time(), true);
});

/**
* 测试数据库
*/
$robot->cron("*/1 * * * *", function () {
$conn = app()->database->getConnection();
$list = $conn->query("SHOW VARIABLES WHERE variable_name like '%timeout'")->fetchAll();
Console::log(\Im050\WeChat\Component\Utils::json_encode($list));
sleep(20);
$list = $conn->query("SHOW VARIABLES WHERE variable_name like '%timeout'")->fetchAll();
Console::log(\Im050\WeChat\Component\Utils::json_encode($list));
});

$robot->onLogout(function ($code) {
//code=1100 or 1101 在其他客户端登录
//code=1102 无效的cookies
Expand Down
35 changes: 0 additions & 35 deletions src/WeChat/Component/Database.php

This file was deleted.

52 changes: 52 additions & 0 deletions src/WeChat/Component/Database/Database.php
@@ -0,0 +1,52 @@
<?php
/**
* Created by PhpStorm.
* User: linyulin
* Date: 2019/4/13
* Time: 8:33 PM
*/

namespace Im050\WeChat\Component\Database;

use Im050\WeChat\Component\Console;
use Medoo\Medoo;

class Database extends Medoo
{

private $name = 'default';

private $maxRetryTimes = 3;

public function __construct(array $options, string $name)
{
parent::__construct($options);
$this->name = $name;
}

public function reconnect()
{
$config = config('db.' . $this->name);
$this->__construct($config, $this->name); //init.
Console::log("Triggered reconnection database mechanism.", Console::DEBUG);
}

public function exec($query, $map = [])
{
if ($this->maxRetryTimes <= 0) {
throw new \PDOException("MySQL has gone away", 'HY000');
}
try {
$result = parent::exec($query, $map);
$this->maxRetryTimes < 5 && $this->maxRetryTimes++;
return $result;
} catch (\PDOException $e) {
if ($e->getCode() == 'HY000') {
$this->reconnect();
$this->maxRetryTimes--;
return $this->exec($query, $map);
}
throw $e;
}
}
}
42 changes: 42 additions & 0 deletions src/WeChat/Component/Database/DatabaseFactory.php
@@ -0,0 +1,42 @@
<?php
/**
* Created by PhpStorm.
* User: linyulin
* Date: 2019/4/22
* Time: 4:10 AM
*/

namespace Im050\WeChat\Component\Database;

use PDO;

class DatabaseFactory
{
private $dbInstances = [];

public function __construct()
{
}

/**
* @param string $name
* @return Database
*/
public function getConnection($name = 'default') {
$realName = $this->getInstanceName($name);
if (array_key_exists($realName, $this->dbInstances)) {
return $this->dbInstances[$realName];
}
$config = config("db." . $name);
!isset($config['option']) && $config['option'] = [];
$config['option'] = $config['option'] + [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$this->dbInstances[$realName] = new Database($config, $name);
return $this->dbInstances[$realName];
}

private function getInstanceName($name) {
$pid = posix_getpid();
$realName = $name . "_" . $pid;
return $realName;
}
}
4 changes: 2 additions & 2 deletions src/WeChat/Core/Application.php
Expand Up @@ -3,7 +3,7 @@
use Im050\WeChat\Collection\Members;
use Im050\WeChat\Collection\MessageCollection;
use Im050\WeChat\Component\Config;
use Im050\WeChat\Component\Database;
use Im050\WeChat\Component\Database\DatabaseFactory;
use Im050\WeChat\Component\HttpClient;
use Im050\WeChat\Crontab\Crontab;
use Im050\WeChat\Message\MessageHandler;
Expand Down Expand Up @@ -38,7 +38,7 @@
* @property Crontab $crontab
* @property Logger $log
* @property Logger $messageLog
* @property Database $database
* @property DatabaseFactory $database
*/
class Application extends Container
{
Expand Down
3 changes: 2 additions & 1 deletion src/WeChat/Providers/DatabaseProvider.php
Expand Up @@ -10,6 +10,7 @@


use Im050\WeChat\Component\Database;
use Im050\WeChat\Component\Database\DatabaseFactory;
use Im050\WeChat\Core\Application;

class DatabaseProvider implements ServiceProvider
Expand All @@ -21,7 +22,7 @@ class DatabaseProvider implements ServiceProvider
public function register(Application $application)
{
$application->singleton('database', function() {
return new Database();
return new DatabaseFactory();
});
}

Expand Down

0 comments on commit 9ed5ef8

Please sign in to comment.