Skip to content

Commit

Permalink
Modify Redis conn util class structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
steven.chiu committed Jun 5, 2019
1 parent 63f6d73 commit c86fc02
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 68 deletions.
8 changes: 4 additions & 4 deletions src/JobScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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";
}
}
Expand Down
127 changes: 66 additions & 61 deletions src/RedisFactory.php
Original file line number Diff line number Diff line change
@@ -1,86 +1,91 @@
<?php

class CacheException extends Exception
{
const REDIS_CONFIG_ERROR = "redis connection error...";
}
namespace TimeWorker;

function getRedisConn($redisConf)
class CacheException extends \Exception
{
return _initRedisPconnect($redisConf);
const REDIS_CONFIG_ERROR = "redis connection error...";
}

function _initRedisPconnect($redisConf)
class RedisFactory
{
// config socket time out 24 hours
ini_set('default_socket_timeout', 86400);

if (empty($redisConf['host']) || empty($redisConf['port'])) {
throw new CacheException(CacheException::REDIS_CONFIG_ERROR);
public static function getRedisConn($redisConf)
{
return self::_initRedisPconnect($redisConf);
}

$host = $redisConf['host'];
$port = $redisConf['port'];
$init = false;
private static function _initRedisPconnect($redisConf)
{
// config socket time out 24 hours
ini_set('default_socket_timeout', 86400);

//check redis connect use ping not PONG reconnect
if (!isset($redis)) {
$init = true;
} else {
try {
$redis->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;
}
}
6 changes: 3 additions & 3 deletions test/job_test.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
include __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../src/RedisFactory.php';

use TimeWorker\JobManager;
use TimeWorker\JobScheduler;
use TimeWorker\RedisFactory;
use React\EventLoop\Factory;

$redisConf['host'] = '127.0.0.1';
Expand Down Expand Up @@ -31,7 +31,7 @@ function init($redisConf, $isJobSubmitter = false)
echo "Exception:$exception\n";
echo "Prepare to quit...\n";
return;
} catch (wxception\CacheException $exception) {
} catch (TimeWorker\CacheException $exception) {
echo "Exception:$exception\n";
echo "Prepare to quit...\n";
return;
Expand All @@ -40,7 +40,7 @@ function init($redisConf, $isJobSubmitter = false)

function testAddJob($redisConf)
{
$redis = getRedisConn($redisConf);
$redis = TimeWorker\RedisFactory::getRedisConn($redisConf);
echo "================AddJob================\n";
$strJsonFileContents = file_get_contents(__DIR__.'/'."script.json");
$array = json_decode($strJsonFileContents);
Expand Down

0 comments on commit c86fc02

Please sign in to comment.