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
+24 -16
View File
@@ -29,15 +29,19 @@ 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();
$serialize = serialize($consumer); try {
if (!$redis->lock($hash = md5($serialize))) { $serialize = serialize($consumer);
return false; if (!$redis->lock($hash = md5($serialize))) {
return false;
}
$isExists = $redis->zRevRank($key, $serialize);
if ($isExists !== null) {
$redis->zAdd($key, $score, $serialize);
}
return $redis->unlink($hash);
} finally {
$redis->release();
} }
$isExists = $redis->zRevRank($key, $serialize);
if ($isExists !== null) {
$redis->zAdd($key, $score, $serialize);
}
return $redis->unlink($hash);
} }
@@ -50,17 +54,21 @@ 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();
if (!$redis->lock($hash = md5($serialize))) { try {
return false; $serialize = serialize($consumer);
} if (!$redis->lock($hash = md5($serialize))) {
$isExists = $redis->zRevRank($key, $serialize); return false;
if ($isExists === null) { }
$isExists = $redis->zRevRank($key, $serialize);
if ($isExists === null) {
return $redis->unlink($hash);
}
$redis->zRem($key, $serialize);
return $redis->unlink($hash); return $redis->unlink($hash);
} finally {
$redis->release();
} }
$redis->zRem($key, $serialize);
return $redis->unlink($hash);
} }
+19 -9
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();
if ($this->shutdown) { try {
return Timer::clear($timerId); if ($this->shutdown) {
return;
}
$data = $redis->zRevRange(Waiting::QUEUE_WAITING, 0, 20);
if (empty($data)) {
Coroutine::sleep(0.05);
} else {
$this->scheduler($data);
}
} catch (\Throwable $exception) {
$this->application->error($exception->getMessage());
} finally {
$redis->release();
} }
$data = $redis->zRevRange(Waiting::QUEUE_WAITING, 0, 20); } while (true);
if (empty($data)) {
return 1;
}
return $this->scheduler($data);
});
} }