mirror of
https://gitee.com/ShopeX/OMS
synced 2026-03-23 10:55:34 +08:00
119 lines
2.7 KiB
PHP
119 lines
2.7 KiB
PHP
<?php
|
||
/**
|
||
* Copyright © ShopeX (http://www.shopex.cn). All rights reserved.
|
||
* See LICENSE file for license details.
|
||
*/
|
||
|
||
/**
|
||
* 多进程任务执行命令
|
||
*
|
||
* @author chenping@shopex.cn
|
||
* @version Fri May 6 23:15:31 2022
|
||
*/
|
||
use Swoole\Event;
|
||
use Swoole\Process;
|
||
use Swoole\Process\Manager;
|
||
use Swoole\Process\Pool;
|
||
// use Swoole\Timer;
|
||
|
||
class taskmgr_swcommand_task extends taskmgr_swcommand_base
|
||
{
|
||
|
||
private $manage;
|
||
private $wait = false;
|
||
|
||
protected $cmd;
|
||
|
||
protected static $signature = [
|
||
'worker:start' => 'start',
|
||
'worker:stop' => 'stop',
|
||
'worker:restart' => 'restart',
|
||
// 'worker:reload' => 'reload',
|
||
// 'worker:status' => 'status',
|
||
|
||
];
|
||
|
||
protected static $description = [
|
||
'worker:start' => '启动 携带参数-d 后台启动',
|
||
'worker:stop' => '停止',
|
||
'worker:restart' => '重启 携带参数-d 后台启动',
|
||
// 'worker:reload' => '平滑重启',
|
||
// 'worker:status' => '查看进程运行状态',
|
||
];
|
||
|
||
/**
|
||
* undocumented function
|
||
*
|
||
* @return void
|
||
* @author
|
||
**/
|
||
public function __construct()
|
||
{
|
||
$this->manage = new taskmgr_swprocess_manage;
|
||
}
|
||
|
||
/**
|
||
* 启动
|
||
*/
|
||
public function start()
|
||
{
|
||
global $argv;
|
||
|
||
if ($this->manage->getPid()) {
|
||
taskmgr_swconsole_output::warning('已启动不可重复启动' . PHP_EOL);
|
||
|
||
exit();
|
||
}
|
||
|
||
if (isset($argv[2]) && $argv[2] == '-d') {
|
||
taskmgr_swconsole_output::warning('以守护进程方式启动,请去日志中查询详细信息,日志目录地址:./logs/' . PHP_EOL);
|
||
Process::daemon();
|
||
|
||
taskmgr_swprocess_conf::setDaemon(true);
|
||
}
|
||
taskmgr_log::info('正在启动中,请稍后....', [], 'system');
|
||
|
||
$this->manage->run();
|
||
|
||
// $this->wait();
|
||
}
|
||
|
||
/**
|
||
* 重启
|
||
* @throws \Exception
|
||
*/
|
||
public function restart()
|
||
{
|
||
$this->stop();
|
||
$this->start();
|
||
}
|
||
|
||
/**
|
||
* 停止
|
||
* @param false $is_start
|
||
* @throws \Exception
|
||
*/
|
||
public function stop()
|
||
{
|
||
$pid = $this->manage->getPid();
|
||
|
||
if ($pid) {
|
||
taskmgr_swconsole_output::normal('正在停止....' . PHP_EOL);
|
||
while (true) {
|
||
Process::kill($pid, SIGTERM);
|
||
|
||
usleep(1000000);
|
||
if (!$this->manage->getPid()) {
|
||
taskmgr_swconsole_output::normal("已停止...\n");
|
||
return true;
|
||
}
|
||
|
||
|
||
}
|
||
}
|
||
|
||
taskmgr_swconsole_output::normal('任务未启动无需停止' . PHP_EOL);
|
||
return;
|
||
}
|
||
}
|