-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasySwooleEvent.php
More file actions
70 lines (60 loc) · 2.14 KB
/
EasySwooleEvent.php
File metadata and controls
70 lines (60 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Created by PhpStorm.
* User: yf
* Date: 2018/5/28
* Time: 下午6:33
*/
namespace EasySwoole\EasySwoole;
use App\Lib\Kv;
use App\Process\ListSpider;
use App\Spider\ItemSpider;
use EasySwoole\Component\Timer;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\Task\TaskManager;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// TODO: Implement initialize() method.
date_default_timezone_set('Asia/Shanghai');
}
public static function mainServerCreate(EventRegister $register)
{
//更新代理池进程
ServerManager::getInstance()->getSwooleServer()->addProcess((new \App\Process\UpdateProxyPool('UpdateProxyPool', []))->getProcess());
//列表爬取进程
ServerManager::getInstance()->getSwooleServer()->addProcess((new \App\Process\ListSpider('ListSpider', []))->getProcess());
$register->set($register::onWorkerStart,function(\swoole_server $server,$workerId){
if($workerId == 0)
{
Timer::getInstance()->loop(30000, function () {
$ret = Kv::redis()->get(ListSpider::LIST_KV_KEY);
if($ret){
$ret = json_decode($ret,true);
foreach($ret as $item) {
TaskManager::async(function () use($item){
(new ItemSpider(true))->run($item);
return true;
}, function () use($item){
var_dump("{$item} Done");
});
}
}
});
}
});
}
public static function onRequest(Request $request, Response $response): bool
{
// TODO: Implement onRequest() method.
return true;
}
public static function afterRequest(Request $request, Response $response): void
{
// TODO: Implement afterAction() method.
}
}