改名
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Queue\Abstracts;
|
||||
|
||||
|
||||
use Snowflake\Process\Process;
|
||||
|
||||
/**
|
||||
* Class AbstractsQueue
|
||||
* @package Queue\Abstracts
|
||||
*/
|
||||
abstract class AbstractsQueue extends Process
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Queue\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Queue\Consumer;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Queue
|
||||
* @package Queue\Abstracts
|
||||
*/
|
||||
abstract class Queue extends Component implements Relyon
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param Consumer $consumer
|
||||
* @param int $score
|
||||
* @return false|int
|
||||
* @throws ComponentException
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function push(string $key, Consumer $consumer, $score = 0)
|
||||
{
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
$serialize = serialize($consumer);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param Consumer $consumer
|
||||
* @return false|int
|
||||
* @throws ComponentException
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function pop($key, Consumer $consumer)
|
||||
{
|
||||
$serialize = serialize($consumer);
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
if (!$redis->lock($hash = md5($serialize))) {
|
||||
return false;
|
||||
}
|
||||
$isExists = $redis->zRevRank($key, $serialize);
|
||||
if ($isExists === null) {
|
||||
return $redis->unlink($hash);
|
||||
}
|
||||
$redis->zRem($key, $serialize);
|
||||
return $redis->unlink($hash);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Queue\Abstracts;
|
||||
|
||||
|
||||
use Queue\Consumer;
|
||||
|
||||
interface Relyon
|
||||
{
|
||||
|
||||
// /**
|
||||
// * @param string $key
|
||||
// * @param Consumer $consumer
|
||||
// * @param int $score
|
||||
// * @return mixed
|
||||
// *
|
||||
// * 追加
|
||||
// */
|
||||
// public function push(string $key, Consumer $consumer, $score = 0);
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * @param string $key
|
||||
// * @param Consumer $consumer
|
||||
// * @return mixed
|
||||
// * 消费
|
||||
// */
|
||||
// public function pop(string $key, Consumer $consumer);
|
||||
//
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Queue;
|
||||
|
||||
|
||||
use Queue\Abstracts\Relyon;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
|
||||
/**
|
||||
* Class Complete
|
||||
* @package Queue
|
||||
*/
|
||||
class Complete extends \Queue\Abstracts\Queue
|
||||
{
|
||||
|
||||
const QUEUE_COMPLETE = 'queue:complete:lists';
|
||||
|
||||
|
||||
/**
|
||||
* @param Consumer $consumer
|
||||
* @param int $score
|
||||
* @return false|int
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function add(Consumer $consumer, $score = 0)
|
||||
{
|
||||
return $this->push(self::QUEUE_COMPLETE, $consumer, $score);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $consumer
|
||||
* @return false|int
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function del(Consumer $consumer)
|
||||
{
|
||||
return $this->pop(self::QUEUE_COMPLETE, $consumer);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Queue;
|
||||
|
||||
|
||||
interface Consumer
|
||||
{
|
||||
|
||||
|
||||
public function __construct(array $params);
|
||||
|
||||
|
||||
|
||||
public function onWaiting();
|
||||
|
||||
|
||||
public function onRunning();
|
||||
|
||||
|
||||
public function onComplete();
|
||||
|
||||
|
||||
}
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Queue;
|
||||
|
||||
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Process;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
* Class Queue
|
||||
* @package Queue
|
||||
*/
|
||||
class Queue extends \Snowflake\Process\Process
|
||||
{
|
||||
|
||||
/** @var Waiting */
|
||||
private $waiting;
|
||||
|
||||
/** @var Complete */
|
||||
private $complete;
|
||||
|
||||
/** @var Running */
|
||||
private $running;
|
||||
|
||||
|
||||
private $shutdown = false;
|
||||
|
||||
|
||||
/**
|
||||
* Queue constructor.
|
||||
* @param $application
|
||||
* @param $name
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($application, $name)
|
||||
{
|
||||
parent::__construct($application, $name);
|
||||
$this->waiting = Snowflake::createObject(Waiting::class);
|
||||
$this->running = Snowflake::createObject(Running::class);
|
||||
$this->complete = Snowflake::createObject(Complete::class);
|
||||
|
||||
Process::signal(9 | 15, function () {
|
||||
$this->shutdown = true;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Process $process
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function onHandler(Process $process)
|
||||
{
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
Timer::tick(50, function ($timerId) use ($redis) {
|
||||
if ($this->shutdown) {
|
||||
return Timer::clear($timerId);
|
||||
}
|
||||
$data = $redis->zRevRangeByScore(Waiting::QUEUE_WAITING, 0, 20);
|
||||
if (empty($data)) {
|
||||
return 1;
|
||||
}
|
||||
return $this->scheduler($data);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Consumer $consumer
|
||||
* @param int $score
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function delivery(Consumer $consumer, $score = 0)
|
||||
{
|
||||
$this->waiting->add($consumer, $score);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @throws Exception
|
||||
*/
|
||||
private function scheduler($data)
|
||||
{
|
||||
$scheduler = new Coroutine\Scheduler();
|
||||
foreach ($data as $datum) {
|
||||
$scheduler->add([$this, 'runner'], $datum);
|
||||
}
|
||||
$scheduler->start();
|
||||
if ($this->shutdown === true) {
|
||||
Snowflake::shutdown($this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @return mixed|void
|
||||
* @throws ComponentException
|
||||
*/
|
||||
private function runner(string $class)
|
||||
{
|
||||
$logger = $this->application->getLogger();
|
||||
try {
|
||||
$rely_on = unserialize($class);
|
||||
$this->waiting->remove($class);
|
||||
if ($rely_on instanceof Consumer) {
|
||||
return;
|
||||
}
|
||||
$this->running->add($rely_on);
|
||||
$rely_on->onRunning();
|
||||
} catch (\Throwable $exception) {
|
||||
$logger->write($exception->getMessage(), 'queue');
|
||||
} finally {
|
||||
$this->running->del($class);
|
||||
if (isset($rely_on) && $rely_on instanceof Consumer) {
|
||||
$rely_on->onComplete();
|
||||
$this->complete->add($rely_on);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Waiting
|
||||
*/
|
||||
public function getWaiting(): Waiting
|
||||
{
|
||||
return $this->waiting;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Complete
|
||||
*/
|
||||
public function getComplete(): Complete
|
||||
{
|
||||
return $this->complete;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Running
|
||||
*/
|
||||
public function getRunning(): Running
|
||||
{
|
||||
return $this->running;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Queue;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Server;
|
||||
use Snowflake\Abstracts\Providers;
|
||||
use Snowflake\Application;
|
||||
|
||||
|
||||
/**
|
||||
* Class QueueProviders
|
||||
* @package Queue
|
||||
*/
|
||||
class QueueProviders extends Providers
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Application $application
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onImport(Application $application)
|
||||
{
|
||||
/** @var Server $server */
|
||||
$server = $application->get('server');
|
||||
$server->addProcess('queue', Queue::class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Queue;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Queue\Abstracts\AbstractsQueue;
|
||||
use Queue\Abstracts\Relyon;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Running
|
||||
* @package Queue
|
||||
*/
|
||||
class Running extends \Queue\Abstracts\Queue
|
||||
{
|
||||
|
||||
const QUEUE_RUNNING = 'queue:runner:lists';
|
||||
|
||||
|
||||
/**
|
||||
* @param Consumer $consumer
|
||||
* @param int $score
|
||||
* @return false|int
|
||||
* @throws ComponentException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function add(Consumer $consumer, $score = 0)
|
||||
{
|
||||
return $this->push(self::QUEUE_RUNNING, $consumer, $score);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $consumer
|
||||
* @return false|int
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function del(string $consumer)
|
||||
{
|
||||
$consumer = unserialize($consumer);
|
||||
if (!($consumer instanceof Consumer)) {
|
||||
return true;
|
||||
}
|
||||
return $this->pop(self::QUEUE_RUNNING, $consumer);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Queue;
|
||||
|
||||
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class TestQueue
|
||||
* @package Queue
|
||||
*/
|
||||
class TestQueue implements Consumer
|
||||
{
|
||||
|
||||
public $params;
|
||||
|
||||
|
||||
/**
|
||||
* TestQueue constructor.
|
||||
* @param array $params
|
||||
*/
|
||||
public function __construct(array $params)
|
||||
{
|
||||
$this->params = $params;
|
||||
$this->onWaiting();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function onWaiting()
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function onRunning()
|
||||
{
|
||||
// TODO: Implement onRunning() method.
|
||||
}
|
||||
|
||||
public function onComplete()
|
||||
{
|
||||
// TODO: Implement onComplete() method.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Queue;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Queue\Abstracts\Relyon;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Waiting
|
||||
* @package Queue
|
||||
*/
|
||||
class Waiting extends \Queue\Abstracts\Queue
|
||||
{
|
||||
|
||||
const QUEUE_WAITING = 'queue:waiting:lists';
|
||||
|
||||
|
||||
/**
|
||||
* @param Consumer $consumer
|
||||
* @param int $score
|
||||
* @return false|int
|
||||
* @throws ComponentException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function add(Consumer $consumer, $score = 0)
|
||||
{
|
||||
return $this->push(self::QUEUE_WAITING, $consumer, $score);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $consumer
|
||||
* @return false|int
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function del(Consumer $consumer)
|
||||
{
|
||||
return $this->pop(self::QUEUE_WAITING, $consumer);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user