hyperf-chat/app/Command/RemoveWsCacheCommand.php

59 lines
1.5 KiB
PHP
Raw Normal View History

2020-11-08 17:10:05 +08:00
<?php
declare(strict_types=1);
2020-12-26 21:33:40 +08:00
/**
* This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author Yuandong<837215079@qq.com>
* @link https://github.com/gzydong/hyperf-chat
*/
2020-11-08 17:10:05 +08:00
namespace App\Command;
use Hyperf\Command\Annotation\Command;
2020-12-02 15:14:29 +08:00
use Hyperf\Command\Command as HyperfCommand;
2020-11-08 17:10:05 +08:00
use Psr\Container\ContainerInterface;
2020-11-29 14:44:11 +08:00
use App\Service\SocketClientService;
2020-11-08 17:10:05 +08:00
/**
* @Command
*/
class RemoveWsCacheCommand extends HyperfCommand
{
/**
* @var ContainerInterface
*/
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct('ws:remove-cache');
}
public function configure()
{
parent::configure();
$this->setDescription('清除 WebSocket 客户端 FD 与用户绑定的缓存信息');
}
public function handle()
{
2020-11-29 14:44:11 +08:00
$socket = new SocketClientService();
$this->line('此过程可能耗时较长,请耐心等待!', 'info');
2020-12-01 17:47:25 +08:00
// 获取所有已停止运行的服务ID
2020-11-29 14:44:11 +08:00
$arr = $socket->getServerRunIdAll(2);
2020-12-01 17:47:25 +08:00
2020-11-29 14:44:11 +08:00
foreach ($arr as $run_id => $value) {
go(function () use ($socket, $run_id) {
$socket->removeRedisCache($run_id);
});
2020-11-08 17:10:05 +08:00
}
$this->line('缓存已清除!', 'info');
}
}