|
@@ -8,48 +8,71 @@ class Api
|
|
|
public function add_api($id)
|
|
|
{
|
|
|
# 写入队列
|
|
|
- $config = Dever::load('spider/lib/project')->get($id);
|
|
|
+ if (is_array($id)) {
|
|
|
+ $config = $id;
|
|
|
+ } else {
|
|
|
+ $config = Dever::load('spider/lib/project')->get($id);
|
|
|
+ }
|
|
|
+
|
|
|
if (!$config) {
|
|
|
Dever::alert('项目不存在');
|
|
|
}
|
|
|
- Dever::load('spider/lib/project')->set($config, 3);
|
|
|
- Dever::load('spider/lib/queue')->push($config['id']);
|
|
|
- return 'yes';
|
|
|
+ if ($config['status'] <= 2) {
|
|
|
+ Dever::load('spider/lib/project')->set($config, 3);
|
|
|
+ Dever::load('spider/lib/queue')->push($config['id']);
|
|
|
+ }
|
|
|
+ return 'reload';
|
|
|
}
|
|
|
|
|
|
public function test_api($id)
|
|
|
{
|
|
|
Dever::setInput('test', 1);
|
|
|
$this->run($id);
|
|
|
- return 'yes';
|
|
|
+ return 'reload';
|
|
|
}
|
|
|
|
|
|
- public function cron()
|
|
|
+ # 守护进程 每分钟执行一次即可
|
|
|
+ public function daemon()
|
|
|
{
|
|
|
- if (!$this->queue) {
|
|
|
- $this->queue = new Queue();
|
|
|
+ # 查看进程是否存在
|
|
|
+ $state = Dever::process('lib/api.cron', true);
|
|
|
+ if ($state <= 0) {
|
|
|
+ Dever::daemon('lib/api.cron', 'spider');
|
|
|
+ }
|
|
|
+
|
|
|
+ # 查看当前所有项目是否可以开始运行
|
|
|
+ $data = Dever::load('spider/lib/project')->getAll();
|
|
|
+ if ($data) {
|
|
|
+ foreach ($data as $k => $v) {
|
|
|
+ $this->add_api($v);
|
|
|
+ }
|
|
|
}
|
|
|
- Dever::import('task');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function cron()
|
|
|
+ {
|
|
|
+ $this->queue = new Queue();
|
|
|
+ //Dever::import('task');
|
|
|
while (1) {
|
|
|
$this->load();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private function load()
|
|
|
+ public function load()
|
|
|
{
|
|
|
try {
|
|
|
$id = $this->queue->pop();
|
|
|
if ($id) {
|
|
|
$config = Dever::load('spider/lib/project')->get($id);
|
|
|
- if ($config && $config['status'] <= 2 && time() >= $config['sdate']) {
|
|
|
+ if ($config) {
|
|
|
# 推到后台运行
|
|
|
# 获取当前执行的进程数量
|
|
|
- $num = $this->getNum();
|
|
|
+ $num = Dever::process('lib/api.run', true);
|
|
|
if ($num >= 1000) {
|
|
|
# 等会儿再执行
|
|
|
sleep(60);
|
|
|
}
|
|
|
- $this->popen($id);
|
|
|
+ Dever::daemon('lib/api.run?id=' . $id, 'spider');
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
@@ -58,8 +81,12 @@ class Api
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function run($id)
|
|
|
+ public function run()
|
|
|
{
|
|
|
+ $id = Dever::input('id');
|
|
|
+ if (!$id) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
$config = Dever::load('spider/lib/project')->get($id);
|
|
|
if (!$config) {
|
|
|
return false;
|
|
@@ -83,22 +110,6 @@ class Api
|
|
|
Dever::load('spider/lib/project')->set($config, 2);
|
|
|
}
|
|
|
|
|
|
- # 将数据推到子进程处理
|
|
|
- public function popen($id)
|
|
|
- {
|
|
|
- $path = Dever::db('spider/project')->config['path'] . 'daemon/run.php';
|
|
|
- $command = 'php '.$path.' -send id=' . $id . ' 1>/dev/null 2>&1 &';
|
|
|
- exec($command);
|
|
|
- }
|
|
|
-
|
|
|
- # 获取当前执行的子进程数量
|
|
|
- public function getNum()
|
|
|
- {
|
|
|
- $command = 'ps -ef | grep gateway/api.task/runOne | grep -v grep | wc -l';
|
|
|
- $num = exec($command);
|
|
|
- return $num;
|
|
|
- }
|
|
|
-
|
|
|
private function col($project)
|
|
|
{
|
|
|
return Dever::db('spider/col')->getList(['where_pid' => $project]);
|