Skip to content

Commit

Permalink
fixed:修复脚本执行完退出,导致协程没有执行完毕也推出了
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed Aug 4, 2023
1 parent f6a8ab4 commit 2284e96
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 30 deletions.
17 changes: 10 additions & 7 deletions Test/Scripts/FixedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,27 @@ public function fixName()
var_dump("name=".$name);
var_dump('CID='.\Swoole\Coroutine::getCid());
var_dump('Script test');
sleep(5);
sleep(2);

var_dump('spl_object_id='.spl_object_id($this->db));
$result1 = $this->db->newQuery()->table('tbl_users')->limit(1)->select()->toArray();
//var_dump($result1);

goApp(function () {
var_dump('CID11='.\Swoole\Coroutine::getCid());
var_dump('spl_object_id-11='.spl_object_id($this->db));
$result1 = $this->db->newQuery()->table('tbl_users')->limit(1)->order('user_id','desc')->select()->toArray();
//var_dump($result1);
try {
var_dump('CID11='.\Swoole\Coroutine::getCid());
var_dump('spl_object_id-11='.spl_object_id($this->db));
$result1 = Application::getApp()->get('db')->newQuery()->table('tbl_users')->limit(1)->select()->toArray();
var_dump($result1);
}catch (\Throwable $exception) {
var_dump($exception->getMessage());
}

});

}catch (\Throwable $exception) {
var_dump($exception->getMessage());
}

//$this->exitAll();
}

public function onHandleException(\Throwable $throwable, array $context = [])
Expand Down
5 changes: 4 additions & 1 deletion src/Http/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@
'max_request' => 1000,
'task_worker_num' => 2,
'task_tmpdir' => '/dev/shm',
'task_enable_coroutine' => 1,
'task_max_request' => 1000,
'daemonize' => 0,
'dispatch_mode' => 3,
'reload_async' => true,
'enable_coroutine' => 1,
'task_enable_coroutine' => 1,
'enable_preemptive_scheduler' => 1,
// 压缩
'http_compression' => true,
// $level 压缩等级,范围是 1-9,等级越高压缩后的尺寸越小,但 CPU 消耗更多。默认为 1, 最高为 9
'http_compression_level' => 1,
'stats_file' => '/tmp/' . APP_NAME . '/stats.log',
'log_file' => '/tmp/' . APP_NAME . '/swoole_log.txt',
'pid_file' => '/data/' . APP_NAME . '/log/server.pid',
],
Expand Down
18 changes: 0 additions & 18 deletions src/Rpc/RpcHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,6 @@ public function __construct(array $config = [])
parent::__construct($config);
}

/**
* init 当执行run方法时,首先会执行init->bootstrap
* @param mixed $payload
* @return void
*/
public function init($payload)
{
}

/**
* bootstrap 当执行run方法时,首先会执行init->bootstrap
* @param mixed $payload
* @return void
*/
public function bootstrap($payload)
{
}

/**
* run 完成初始化后路由匹配和创建访问实例
* @param int $fd
Expand Down
5 changes: 4 additions & 1 deletion src/Rpc/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
'max_request' => 1000,
'task_worker_num' => 2,
'task_tmpdir' => '/dev/shm',
'task_enable_coroutine' => 1,
'task_max_request' => 1000,
'daemonize' => 0,
'dispatch_mode' => 2,
'open_length_check' => 1,
Expand All @@ -40,7 +42,8 @@
'package_body_offset' => 34, //第几个字节开始计算长度
'package_max_length' => 2000000,//协议最大长度
'enable_coroutine' => 1,
'task_enable_coroutine' => 1,
'enable_preemptive_scheduler' => 1,
'stats_file' => '/tmp/' . APP_NAME . '/stats.log',
'log_file' => '/tmp/' . APP_NAME . '/swoole_log.txt',
'pid_file' => '/data/' . APP_NAME . '/log/server.pid',
],
Expand Down
19 changes: 19 additions & 0 deletions src/Script/MainCliScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function run()
$action = getenv('a');
list($method, $params) = Helper::parseActionParams($this, $action, Helper::getCliParams());
$this->{$action}(...$params);
$this->waitCoroutineFinish();
$this->exitAll();
}catch (\Throwable $throwable) {
write($throwable->getMessage());
Expand All @@ -78,6 +79,24 @@ protected function isExecuted(): bool
return false;
}

/**
* 防止脚本创建协程时,主进程脚本直接退出了,会把协程也退出,导致协程没执行,所以需要等待一段时间,让协程执行完
*
* @param float $timeOut
* @return void
*/
protected function waitCoroutineFinish(float $timeOut = 5.0)
{
$time = time();
while (true) {
$status = \Swoole\Coroutine::stats();
if ($status['coroutine_num'] == 1 || time() > ($time + $timeOut)) {
break;
}
\Swoole\Coroutine\System::sleep(0.5);
}
}

/**
* @return void
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Udp/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
'max_request' => 1000,
'task_worker_num' => 2,
'task_tmpdir' => '/dev/shm',
'task_enable_coroutine' => 1,
'task_max_request' => 1000,
'daemonize' => 0,
'dispatch_mode' => 3,
'enable_coroutine' => 1,
'task_enable_coroutine' => 1,
'enable_preemptive_scheduler' => 1,
'stats_file' => '/tmp/' . APP_NAME . '/stats.log',
'log_file' => '/tmp/' . APP_NAME . '/swoole_log.txt',
'pid_file' => '/data/' . APP_NAME . '/log/server.pid',
],
Expand Down
1 change: 0 additions & 1 deletion src/Websocket/WebsocketEventServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ abstract public function onOpen(Server $server, Request $request);
* @param Request $request
* @param Response $response
* @return void
* @throws \Throwable
*/
public function onRequest(Request $request, Response $response)
{
Expand Down
5 changes: 4 additions & 1 deletion src/Websocket/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
'max_request' => 1000,
'task_worker_num' => 2,
'task_tmpdir' => '/dev/shm',
'task_enable_coroutine' => 1,
'task_max_request' => 1000,
'daemonize' => 0,
'dispatch_mode' => 2,
'enable_coroutine' => 1,
'task_enable_coroutine' => 1,
'enable_preemptive_scheduler' => 1,
'stats_file' => '/tmp/' . APP_NAME . '/stats.log',
'log_file' => '/tmp/' . APP_NAME . '/swoole_log.txt',
'pid_file' => '/data/' . APP_NAME . '/log/server.pid',
],
Expand Down

0 comments on commit 2284e96

Please sign in to comment.