This commit is contained in:
2020-09-11 01:25:43 +08:00
parent 67ca557961
commit 9df3b56ec0
2 changed files with 43 additions and 25 deletions
+9 -1
View File
@@ -29,6 +29,7 @@ abstract class Queue extends Component implements Relyon
protected function push(string $key, Consumer $consumer, $score = 0) protected function push(string $key, Consumer $consumer, $score = 0)
{ {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
try {
$serialize = serialize($consumer); $serialize = serialize($consumer);
if (!$redis->lock($hash = md5($serialize))) { if (!$redis->lock($hash = md5($serialize))) {
return false; return false;
@@ -38,6 +39,9 @@ abstract class Queue extends Component implements Relyon
$redis->zAdd($key, $score, $serialize); $redis->zAdd($key, $score, $serialize);
} }
return $redis->unlink($hash); return $redis->unlink($hash);
} finally {
$redis->release();
}
} }
@@ -50,8 +54,9 @@ abstract class Queue extends Component implements Relyon
*/ */
protected function pop($key, Consumer $consumer) protected function pop($key, Consumer $consumer)
{ {
$serialize = serialize($consumer);
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
try {
$serialize = serialize($consumer);
if (!$redis->lock($hash = md5($serialize))) { if (!$redis->lock($hash = md5($serialize))) {
return false; return false;
} }
@@ -61,6 +66,9 @@ abstract class Queue extends Component implements Relyon
} }
$redis->zRem($key, $serialize); $redis->zRem($key, $serialize);
return $redis->unlink($hash); return $redis->unlink($hash);
} finally {
$redis->release();
}
} }
+15 -5
View File
@@ -7,6 +7,7 @@ namespace Queue;
use Exception; use Exception;
use ReflectionException; use ReflectionException;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine; use Swoole\Coroutine;
@@ -56,20 +57,29 @@ class Queue extends \Snowflake\Process\Process
/** /**
* @param Process $process * @param Process $process
* @throws ComponentException
* @throws ConfigException
*/ */
public function onHandler(Process $process) public function onHandler(Process $process)
{ {
Timer::tick(50, function ($timerId) { do {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
try {
if ($this->shutdown) { if ($this->shutdown) {
return Timer::clear($timerId); return;
} }
$data = $redis->zRevRange(Waiting::QUEUE_WAITING, 0, 20); $data = $redis->zRevRange(Waiting::QUEUE_WAITING, 0, 20);
if (empty($data)) { if (empty($data)) {
return 1; Coroutine::sleep(0.05);
} else {
$this->scheduler($data);
} }
return $this->scheduler($data); } catch (\Throwable $exception) {
}); $this->application->error($exception->getMessage());
} finally {
$redis->release();
}
} while (true);
} }