206 lines
4.7 KiB
PHP
Raw Normal View History

2020-07-05 16:36:28 +08:00
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
2020-07-07 16:58:34 +08:00
namespace catcher\library\crontab;
use Swoole\Process;
use catcher\library\crontab\Process as MProcess;
2020-07-08 17:48:45 +08:00
use Swoole\Timer;
2020-07-07 16:58:34 +08:00
class ManageProcess
{
use RegisterSignal, MProcess, Store;
/**
* 动态扩展的最大 process 数量
*
* @var int
*/
protected $maxNum = 10;
/**
* 常驻 process
*
* @var int
*/
2020-07-08 17:48:45 +08:00
protected $staticNum = 1;
2020-07-07 16:58:34 +08:00
/**
* 存储 process 信息
*
* @var array
*/
protected $process = [];
/**
* 主进程ID
*
* @var
*/
protected $master_pid;
/**
* pid 文件名称
*
* @var string
*/
protected $mater = 'catch-master';
/**
* process status 存储文件
*
* @var string
*/
protected $processStatus = 'process-status';
// 版本
const VERSION = '1.0.0';
// process 等待状态
const WAITING = 'waiting';
// process 繁忙状态
const BUSYING = 'busying';
/**
* 启动进程
*
* @time 2020年07月07日
* @return void
*/
public function start()
{
// 守护进程
// Process::daemon(true, false);
// alarm 信号
2020-07-08 17:48:45 +08:00
// Process::alarm(1000 * 1000);
2020-07-07 16:58:34 +08:00
// 1s 调度一次
2020-07-08 17:48:45 +08:00
$this->timeTick(1000, $this->schedule());
2020-07-07 16:58:34 +08:00
// 注册信号
$this->registerSignal();
// 存储 pid
$this->storeMasterPid(getmypid());
// pid
$this->master_pid = getmypid();
// 初始化进程
$this->initProcesses();
}
2020-07-08 17:48:45 +08:00
/**
* 自定义 tick 关闭协程
*
* @time 2020年07月08日
* @param int $time
* @param $callable
* @return void
*/
protected function timeTick(int $time, $callable)
{
// 关闭协程
Timer::set([
'enable_coroutine' => false,
]);
Timer::tick($time, $callable);
}
2020-07-07 16:58:34 +08:00
/**
* 调度
*
* @time 2020年07月07日
* @return \Closure
*/
protected function schedule()
{
return function () {
$schedule = new Schedule();
2020-07-08 17:48:45 +08:00
$schedule->command('catch:cache')->everyThirtySeconds();
2020-07-07 16:58:34 +08:00
foreach ($schedule->getCronTask() as $cron) {
if ($cron->can()) {
list($waiting, $process) = $this->hasWaitingProcess();
if ($waiting) {
// 向 process 投递 cron
2020-07-08 17:48:45 +08:00
// var_dump(serialize($cron));
//$process->push(serialize($cron));
var_dump($process->pop());
2020-07-07 16:58:34 +08:00
} else {
// 创建临时 process 处理,处理完自动销毁
$this->createProcess($cron);
}
}
}
};
}
/**
* Create Task
*
* @time 2019年08月06日
* @param Cron $cron
* @return void
*/
protected function createProcess(Cron $cron)
{
$process = new Process(function (Process $process) use($cron) {
2020-07-08 17:48:45 +08:00
echo 'hello world';
//$cron->run();
2020-07-07 16:58:34 +08:00
$process->exit();
});
2020-07-08 17:48:45 +08:00
// $process->name(sprintf('worker: '));
2020-07-07 16:58:34 +08:00
$process->start();
}
/**
* 创建静态 worker 进程
*
* @time 2020年07月05日
* @return Process
*/
protected function createStaticProcess()
{
2020-07-08 17:48:45 +08:00
$process = new Process($this->createProcessCallback());
// 使用非阻塞队列
$process->useQueue(1, 2|Process::IPC_NOWAIT);
return $process;
2020-07-07 16:58:34 +08:00
}
/**
* 初始化 workers
*
* @time 2020年07月03日
* @return void
*/
protected function initProcesses()
{
for ($i = 0; $i < $this->staticNum; $i++) {
$process = $this->createStaticProcess();
// $worker->name("[$i+1]catch-worker");
$process->start();
$this->process[$process->pid] = $this->processInfo($process);
}
}
2020-07-08 17:48:45 +08:00
/**
* 记录日志
*
* @time 2020年07月07日
* @return void
*/
protected function log()
{
fwrite(STDOUT, runtime_path('schedule') . 'error.log');
}
2020-07-07 16:58:34 +08:00
}