<?php include 'vendor/autoload.php'; class server { private $serv; private $db; /** * [__construct description] * 构造方法中,初始化 $serv 服务 */ public function __construct() { $this->serv = new swoole_server('0.0.0.0', 9501); //初始化swoole服务 $this->serv->set(array( 'worker_num' => 1, 'daemonize' => false, //是否作为守护进程,此配置一般配合log_file使用 'max_request' => 1000, 'log_file' => './swoole.log', 'task_worker_num' => 2 )); //设置监听 $this->serv->on('Start', array($this, 'onStart')); $this->serv->on('WorkerStart', array($this, 'onWorkerStart')); $this->serv->on('Connect', array($this, 'onConnect')); $this->serv->on("Receive", array($this, 'onReceive')); $this->serv->on("Close", array($this, 'onClose')); $this->serv->on("Task", array($this, 'onTask')); $this->serv->on("Finish", array($this, 'onFinish')); //开启 $this->serv->start(); } /** * 连接MySQL */ public function connect() { $this->db = new medoo([ 'database_type' => 'mysql', 'database_name' => 'xxxx', 'server' => '127.0.0.1', 'username' => 'root', 'password' => 'xxxxx', 'prefix' => '', 'charset' => 'utf8' ]); } /** * 在worker启动的时候,创建MySQL连接池 * @param $ser * @param $worker_id */ public function onWorkerStart($ser, $worker_id) { if ($ser->taskworker) { $this->connect(); echo "taskWorker Starting ." . spl_object_hash($this->db) . "......... "; } else { echo 'wokerStart...db has=>' . PHP_EOL; } } public function onStart($serv) { echo SWOOLE_VERSION . " onStart "; } public function onConnect($serv, $fd) { echo $fd . "Client Connect. "; } /** * 这个发生在worker进程中,当接受到来自mater进程中reactor线程转发过来的客户端 * @param $serv * @param $fd * @param $from_id * @param $data */ public function onReceive($serv, $fd, $from_id, $data) { echo "Get Message From Client {$fd}:{$data} "; $param = array( 'fd' => $fd ); $serv->task(json_encode($param)); echo "Continue Handle Worker "; } public function onClose($serv, $fd) { echo "Client Close. "; } /** * 异步任务,用来生成csv表格数据导出 * @param $serv * @param $task_id * @param $from_id * @param $data * @return string */ public function onTask($serv, $task_id, $from_id, $data) { echo "This Task {$task_id} from Worker {$from_id} "; for ($i = 0; $i < 2; $i++) { $all_pager = $this->db->count('dtb_checking'); echo '总条数:' . $all_pager . PHP_EOL; // 其实这里需要判断是否是连接超时,等一系列操作,但是这里就是为了测试数据库连接失效情况。 if ($all_pager === false) { echo '重新连' . PHP_EOL; $this->connect(); continue; } $fp = fopen(__DIR__ . DIRECTORY_SEPARATOR . rand() . '.csv', 'w'); $page_size = 500; $page = ceil($all_pager / $page_size); for ($p = 1; $p <= $page; $p++) { echo '第' . $p . '页' . PHP_EOL; $start = ($p - 1) * $page_size; $list = $this->db->select('dtb_checking', ['id', 'real_name', 'account_no'], ['LIMIT' => [$start, 500]]); foreach ($list as $k => $row) { $temp = [ iconv('utf-8', 'gbk', $row['id']), iconv('utf-8', 'gbk', $row['real_name']), iconv('utf-8', 'gbk', $row['account_no']), ]; fputcsv($fp, $temp); } } break; } return "Task {$task_id}'s result"; } public function onFinish($serv, $task_id, $data) { echo "Task {$task_id} finish "; echo "Result: {$data} "; } } $server = new server();