From c86fc02dbfc630d21d780724cb91805c05c45778 Mon Sep 17 00:00:00 2001 From: "steven.chiu" Date: Wed, 5 Jun 2019 21:55:02 +0800 Subject: [PATCH] Modify Redis conn util class structure. --- src/JobScheduler.php | 8 +-- src/RedisFactory.php | 127 ++++++++++++++++++++++--------------------- test/job_test.php | 6 +- 3 files changed, 73 insertions(+), 68 deletions(-) diff --git a/src/JobScheduler.php b/src/JobScheduler.php index 0033fa2..7bc833f 100644 --- a/src/JobScheduler.php +++ b/src/JobScheduler.php @@ -46,7 +46,7 @@ public function __construct( $this->subPattern = '__keyspace@' . $db . '__:'; $this->executableTime = $executableTime; $this->pollingInterval = $pollingInterval; - $this->forkChildCmd = "php -r \"include_once '$autoloadPath'; include '".__DIR__."/RedisFactory.php'; use TimeWorker\JobScheduler; TimeWorker\JobScheduler::fork(1, '$host', $port, $db);\""; + $this->forkChildCmd = "php -r \"include_once '$autoloadPath'; use TimeWorker\JobScheduler; TimeWorker\JobScheduler::fork(1, '$host', $port, $db);\""; $looper = Factory::create(); $this->looper = $looper; @@ -62,7 +62,7 @@ public function run(callable $cb = null) $this->cb = $cb; try { - $redisConnect = getRedisConn($this->redisConf); + $redisConnect = RedisFactory::getRedisConn($this->redisConf); } catch (\RedisException $exception) { echo "Exception:$exception\n"; echo "Prepare to quit...\n"; @@ -116,11 +116,11 @@ public function monitorExpireKey($redisConf, $looper) while ($rebirth-- > 0) { try { echo 'Redis subscription task PID[' . getmypid() . "]\n"; - $redisConnectPub = getRedisConn($redisConf); + $redisConnectPub = RedisFactory::getRedisConn($redisConf); $this->keyEventMonitor($redisConnectPub); } catch (\RedisException $exception) { echo "Exception:$exception\n"; - } catch (wxception\CacheException $exception) { + } catch (TimeWork\CacheException $exception) { echo "Exception:$exception\n"; } } diff --git a/src/RedisFactory.php b/src/RedisFactory.php index 5db3155..de1b7e2 100644 --- a/src/RedisFactory.php +++ b/src/RedisFactory.php @@ -1,86 +1,91 @@ setOption(\Redis::OPT_READ_TIMEOUT, 1); - $redis->ping(); - } catch (\Exception $exception) { - $redis->close(); - unset($redis); - $init = true; + if (empty($redisConf['host']) || empty($redisConf['port'])) { + throw new CacheException(CacheException::REDIS_CONFIG_ERROR); } - } - // init redis - if ($init) { - $index = 1; - $retry = 3; + $host = $redisConf['host']; + $port = $redisConf['port']; + $init = false; - while ($retry-- > 0) { + //check redis connect use ping not PONG reconnect + if (!isset($redis)) { + $init = true; + } else { try { - $redis = new \Redis(); - $result = $redis->pconnect($host, $port, 2); //设置pconnect超时时间为1秒 - if ($result == false) { - throw new \Exception('pconnect fail'); - } - if (isset($redisConf['passwd'])) { - $ret = $redis->auth($redisConf['passwd']); - if (!$ret) { - throw new CacheException(CacheException::REDIS_CONTENT_FAIL); - } - } - - if (isset($redisConf['db'])) { - $redis->select($redisConf['db']); - } $redis->setOption(\Redis::OPT_READ_TIMEOUT, 1); $redis->ping(); - break; } catch (\Exception $exception) { - print_r("redis info", "redis $host:$port pconnect or ping fail $index times:" . $exception->getMessage()); $redis->close(); unset($redis); + $init = true; } + } + + // init redis + if ($init) { + $index = 1; + $retry = 3; - $index++; + while ($retry-- > 0) { + try { + $redis = new \Redis(); + $result = $redis->pconnect($host, $port, 2); //设置pconnect超时时间为1秒 + if ($result == false) { + throw new \Exception('pconnect fail'); + } + if (isset($redisConf['passwd'])) { + $ret = $redis->auth($redisConf['passwd']); + if (!$ret) { + throw new CacheException(CacheException::REDIS_CONTENT_FAIL); + } + } + + if (isset($redisConf['db'])) { + $redis->select($redisConf['db']); + } + $redis->setOption(\Redis::OPT_READ_TIMEOUT, 1); + $redis->ping(); + break; + } catch (\Exception $exception) { + print_r("redis info", "redis $host:$port pconnect or ping fail $index times:" . $exception->getMessage()); + $redis->close(); + unset($redis); + } + + $index++; + } + if ($retry <= 0) { + print_r("redis fail", "redis $host:$port pconnect and ping fail after try 3 times"); + throw new CacheException(CacheException::REDIS_CONTENT_FAIL); + } } - if ($retry <= 0) { - print_r("redis fail", "redis $host:$port pconnect and ping fail after try 3 times"); - throw new CacheException(CacheException::REDIS_CONTENT_FAIL); + $redis->setOption(\Redis::OPT_READ_TIMEOUT, 3); + if (isset($redisConf['db'])) { + $redis->select($redisConf['db']); + } else { + $redis->select(0); // 默认 DB } - } - $redis->setOption(\Redis::OPT_READ_TIMEOUT, 3); - if (isset($redisConf['db'])) { - $redis->select($redisConf['db']); - } else { - $redis->select(0); // 默认 DB - } - return $redis; + return $redis; + } } diff --git a/test/job_test.php b/test/job_test.php index 0634c6e..cd0f599 100644 --- a/test/job_test.php +++ b/test/job_test.php @@ -1,9 +1,9 @@