schedule());//不会继承 // 注册信号 $this->registerSignal(); // 存储 pid $this->storeMasterPid(getmypid()); // pid $this->master_pid = getmypid(); // 初始化进程 $this->initProcesses(); } /** * 调度 * * @time 2020年07月07日 * @return \Closure */ protected function schedule() { return function () { $schedule = new Schedule(); $schedule->command('route:list')->everyFiveMinutes(); foreach ($schedule->getCronTask() as $cron) { if ($cron->can()) { list($waiting, $process) = $this->hasWaitingProcess(); if ($waiting) { // 向 process 投递 cron } 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) { $cron->run(); $process->exit(); }); $process->name(sprintf('worker: ')); $process->start(); } /** * 创建静态 worker 进程 * * @time 2020年07月05日 * @return Process */ protected function createStaticProcess() { return new Process($this->createProcessCallback()); } /** * 初始化 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); } } }