2020-07-07 16:58:16 +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;
|
|
|
|
|
|
|
|
|
|
trait Store
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 存储 pid
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月05日
|
|
|
|
|
* @param $pid
|
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
|
|
|
|
public function storeMasterPid($pid)
|
|
|
|
|
{
|
|
|
|
|
$path = $this->getMasterPidPath();
|
|
|
|
|
|
|
|
|
|
return file_put_contents($path, $pid);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 19:47:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* 清除退出的 worker 信息
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月08日
|
|
|
|
|
* @param $pid
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function unsetWorkerStatus($pid)
|
|
|
|
|
{
|
2020-07-09 21:03:16 +08:00
|
|
|
|
$this->table->del($this->getColumnKey($pid));
|
2020-07-09 15:41:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 16:58:34 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输出
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月07日
|
|
|
|
|
* @return false|string
|
|
|
|
|
*/
|
|
|
|
|
public function output()
|
|
|
|
|
{
|
|
|
|
|
// 等待信号输出
|
2020-07-09 21:03:16 +08:00
|
|
|
|
usleep(500);
|
2020-07-07 16:58:34 +08:00
|
|
|
|
|
2020-07-09 10:04:31 +08:00
|
|
|
|
return $this->getProcessStatusInfo();
|
2020-07-07 16:58:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取 pid
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月05日
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getMasterPid()
|
|
|
|
|
{
|
|
|
|
|
$pid = file_get_contents($this->getMasterPidPath());
|
|
|
|
|
|
|
|
|
|
return intval($pid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取配置地址
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月05日
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getMasterPidPath()
|
2020-07-09 10:04:31 +08:00
|
|
|
|
{
|
2020-07-09 21:03:16 +08:00
|
|
|
|
return config('catch.schedule.master_pid_file');
|
2020-07-09 10:04:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建任务调度文件夹
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月09日
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function schedulePath()
|
2020-07-07 16:58:34 +08:00
|
|
|
|
{
|
2020-07-09 21:03:16 +08:00
|
|
|
|
$path = config('catch.schedule.store_path');
|
2020-07-07 16:58:34 +08:00
|
|
|
|
|
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
|
mkdir($path, 0777, true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 10:04:31 +08:00
|
|
|
|
return $path;
|
2020-07-07 16:58:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-07-09 10:04:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 进程状态文件
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月09日
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getSaveProcessStatusFile()
|
|
|
|
|
{
|
|
|
|
|
return $this->schedulePath() . '.worker-status';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保存进程状态
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月09日
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function saveProcessStatus()
|
|
|
|
|
{
|
|
|
|
|
file_put_contents($this->getSaveProcessStatusFile(), $this->renderProcessesStatusToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取进程状态
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年07月09日
|
|
|
|
|
* @return false|string
|
|
|
|
|
*/
|
|
|
|
|
protected function getProcessStatusInfo()
|
|
|
|
|
{
|
|
|
|
|
return file_get_contents($this->getSaveProcessStatusFile());
|
2020-07-07 16:58:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|