-
Notifications
You must be signed in to change notification settings - Fork 16
/
EasySwooleEvent.php
72 lines (61 loc) · 2.51 KB
/
EasySwooleEvent.php
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
71
72
<?php
namespace EasySwoole\EasySwoole;
use App\Crontab\TimerTask;
use App\Queue\Queue;
use App\Task\JdClient;
use App\Task\JdGoodClient;
use App\Template;
use EasySwoole\Component\Timer;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\RedisPool\RedisPool;
use EasySwoole\Template\Render;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// mysql 连接注册
$mysqlConfig = Config::getInstance()->getConf('MYSQL');
DbManager::getInstance()->addConnection(new Connection(new \EasySwoole\ORM\Db\Config($mysqlConfig)));
// redis 连接注册
$redisConfig = Config::getInstance()->getConf('REDIS');
RedisPool::getInstance()->register(new RedisConfig($redisConfig), 'redis');
}
public static function mainServerCreate(EventRegister $register)
{
// 清空数据
// $conf = Config::getInstance()->getConf('REDIS');
// $redis = new \Redis();
// $redis->connect($conf['host'], $conf['port']);
// if (!empty($conf['auth'])) {
// $redis->auth($conf['auth']);
// }
// $redis->del(Queue::$queue);
$register->add(EventRegister::onWorkerStart, function (\swoole_server $server, $workerId) {
if ($workerId === 0) {
// \Co::create(function (){
// $client = new JdClient(); // 协程客户端
// $client->run();
// });
// Timer::getInstance()->after(5 * 1000, function () {
// // 定时任务
// $timer = Timer::getInstance()->loop(3 * 1000, function () use (&$timer) {
// \Co::create(function () use (&$timer){
// $goodTask = new JdGoodClient(); // 协程客户端
// $goodTask->run();
// });
// });
// });
}
});
Render::getInstance()->getConfig()->setRender(new Template());
Render::getInstance()->attachServer(ServerManager::getInstance()->getSwooleServer());
// 任务计划
// Crontab::getInstance()->addTask(TimerTask::class);
// 开启热重启进程
// ServerManager::getInstance()->getSwooleServer()->addProcess((new Inotify('autoReload', ['disableInotify' => false]))->getProcess());
}
}