Skip to content

Commit 12e1c99

Browse files
committed
Context::onDestroy
1 parent e5f05f1 commit 12e1c99

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/Redis.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*/
1414
namespace Webman\RedisQueue;
1515

16+
use RedisException;
1617
use Webman\Context;
17-
use Workerman\Coroutine\Coroutine;
1818
use Workerman\Coroutine\Pool;
1919

2020
/**
@@ -64,7 +64,7 @@ public static function connection($name = 'default') {
6464
$connection = static::$pools[$name]->get();
6565
Context::set($key, $connection);
6666
} finally {
67-
Coroutine::defer(function () use ($connection, $name) {
67+
Context::onDestroy(function () use ($connection, $name) {
6868
try {
6969
$connection && static::$pools[$name]->put($connection);
7070
} catch (Throwable) {
@@ -76,7 +76,14 @@ public static function connection($name = 'default') {
7676
return $connection;
7777
}
7878

79-
protected static function connect($config)
79+
/**
80+
* Connect to redis.
81+
*
82+
* @param $config
83+
* @return RedisConnection
84+
* @throws RedisException
85+
*/
86+
protected static function connect($config): RedisConnection
8087
{
8188
if (!extension_loaded('redis')) {
8289
throw new \RuntimeException('Please make sure the PHP Redis extension is installed and enabled.');

src/RedisConnection.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
*/
1414
namespace Webman\RedisQueue;
1515

16-
use Workerman\Timer;
17-
use Workerman\Worker;
16+
use RedisException;
17+
use Throwable;
1818

1919
class RedisConnection extends \Redis
2020
{
2121
/**
2222
* @var array
2323
*/
24-
protected $config = [];
24+
protected array $config = [];
2525

2626
/**
2727
* @param array $config
2828
* @return void
29+
* @throws RedisException
2930
*/
30-
public function connectWithConfig(array $config = [])
31+
public function connectWithConfig(array $config = []): void
3132
{
32-
static $timer;
3333
if ($config) {
3434
$this->config = $config;
3535
}
@@ -45,24 +45,19 @@ public function connectWithConfig(array $config = [])
4545
if (!empty($this->config['prefix'])) {
4646
$this->setOption(\Redis::OPT_PREFIX, $this->config['prefix']);
4747
}
48-
if (Worker::getAllWorkers() && !$timer) {
49-
$timer = Timer::add($this->config['ping'] ?? 55, function () {
50-
$this->execCommand('ping');
51-
});
52-
}
5348
}
5449

5550
/**
5651
* @param $command
5752
* @param ...$args
5853
* @return mixed
59-
* @throws \Throwable
54+
* @throws Throwable
6055
*/
6156
protected function execCommand($command, ...$args)
6257
{
6358
try {
6459
return $this->{$command}(...$args);
65-
} catch (\Throwable $e) {
60+
} catch (Throwable $e) {
6661
$msg = strtolower($e->getMessage());
6762
if ($msg === 'connection lost' || strpos($msg, 'went away')) {
6863
$this->connectWithConfig();
@@ -75,10 +70,11 @@ protected function execCommand($command, ...$args)
7570
/**
7671
* @param $queue
7772
* @param $data
78-
* @param $delay
73+
* @param int $delay
7974
* @return bool
75+
* @throws Throwable
8076
*/
81-
public function send($queue, $data, $delay = 0)
77+
public function send($queue, $data, int $delay = 0): bool
8278
{
8379
$queue_waiting = '{redis-queue}-waiting';
8480
$queue_delay = '{redis-queue}-delayed';

0 commit comments

Comments
 (0)