webman mysql已添加定时器但扔报错SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

xiaobai1

第一次发现报错的时候,查找资料添加了定时器,每50s执行一次select 1;
然后今天下午使用的时候,发现又再次报了同样的错误。检查发现,mysql服务器正常没有宕机过,也没有做过其他操作,请问是什么原因呢?谢谢解答!

$worker->onWorkerStart = function ($worker) use ($config) {
        foreach (config('autoload.files', []) as $file) {
            include_once $file;
        }
        Dotenv::createMutable(base_path())->load();
        Config::reload(config_path(), ['route']);

        $bootstrap = $config['bootstrap'] ?? config('bootstrap', []);
        if (!in_array(support\bootstrap\Log::class, $bootstrap)) {
            $bootstrap[] = support\bootstrap\Log::class;
        }
        foreach ($bootstrap as $class_name) {
            /** @var \Webman\Bootstrap $class_name */
            $class_name::start($worker);
        }

        foreach ($config['services'] ?? [] as $server) {
            if (!class_exists($server['handler'])) {
                echo "process error: class {$config['handler']} not exists\r\n";
                continue;
            }
            $listen = new Worker($server['listen'] ?? null, $server['context'] ?? []);
            if (isset($server['listen'])) {
                echo "listen: {$server['listen']}\n";
            }
            $class = Container::make($server['handler'], $server['constructor'] ?? []);
            worker_bind($listen, $class);
            $listen->listen();
        }

        if (isset($config['handler'])) {
            if (!class_exists($config['handler'])) {
                echo "process error: class {$config['handler']} not exists\r\n";
                return;
            }

            $class = Container::make($config['handler'], $config['constructor'] ?? []);
            worker_bind($worker, $class);
        }

        Timer::add(50, function(){
            Db::query('select 1');
        });

    };
阅读 601
1个回答

walkor

你把代码放错位置了,start.php里有2个onWorkerStart相关的代码,放在第一个里。

另外webman自带一个DB的心跳,在 config/bootstrap.php 配置里加上一行 support\bootstrap\db\Heartbeat::class就可以了。