Skip to content

Commit

Permalink
update. PIPE_MESSAGE事件结束后触发COROUTINE_DEFER, COROUTINE_COMPLETE
Browse files Browse the repository at this point in the history
  • Loading branch information
anhoder committed Jan 7, 2021
1 parent d27337b commit 8ba3c8d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
57 changes: 57 additions & 0 deletions src/server/src/Context/PipeMessageContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* The file is part of the swoft_marketing_engine.
*
* (c) alan <[email protected]>.
*
* 2021/1/3 1:43 下午
*/

namespace Swoft\Server\Context;

use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Concern\PrototypeTrait;
use Swoft\Context\AbstractContext;

/**
* Class PipeMessageContext
* @package App\Context
*
* @Bean(scope=Bean::PROTOTYPE)
*/
class PipeMessageContext extends AbstractContext
{
use PrototypeTrait;

/**
* @param int $srcWorkerId
* @param $pipeMessage
* @return PipeMessageContext
*/
public static function new(int $srcWorkerId, $pipeMessage): self
{
$self = self::__instance();

$self->set('srcWorkerId', $srcWorkerId);
$self->set('pipeMessage', $pipeMessage);

return $self;
}

/**
* @return int
*/
public function getSrcWorkerId(): int
{
return $this->get('srcWorkerId');
}

/**
* @return mixed
*/
public function getPipeMessage()
{
return $this->get('pipeMessage');
}
}

27 changes: 25 additions & 2 deletions src/server/src/Swoole/PipeMessageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@

use Swoft;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Context\Context;
use Swoft\Log\Error;
use Swoft\Log\Helper\CLog;
use Swoft\Server\Contract\PipeMessageInterface;
use Swoft\Server\ServerEvent;
use Swoft\SwoftEvent;
use Swoole\Server;
use Swoft\Server\Context\PipeMessageContext;
use Throwable;

/**
* Class PipeMessageListener
Expand All @@ -34,8 +39,26 @@ class PipeMessageListener implements PipeMessageInterface
*/
public function onPipeMessage(Server $server, int $srcWorkerId, $message): void
{
CLog::debug("PipeMessage: received pipe-message fromWID=$srcWorkerId message=$message");
Context::set(PipeMessageContext::new($srcWorkerId, $message));

Swoft::trigger(ServerEvent::PIPE_MESSAGE, $message, $srcWorkerId, $server);
CLog::debug("PipeMessage: received pipe-message fromWID={$srcWorkerId}}");

try {

Swoft::trigger(ServerEvent::PIPE_MESSAGE, $message, $srcWorkerId, $server);

} catch (Throwable $e) {

Error::log("PipeMessage handle fails: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}");

} finally {

// Defer
Swoft::trigger(SwoftEvent::COROUTINE_DEFER);

// Destroy
Swoft::trigger(SwoftEvent::COROUTINE_COMPLETE);

}
}
}

0 comments on commit 8ba3c8d

Please sign in to comment.