PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

关于thinkphp-queue问题解决

藏色散人
藏色散人 转载
2021-02-23 15:13:29 5089浏览

下面由thinkphp/" target="_blank">thinkphp教程栏目给大家介绍关于thinkphp-queue问题解决,希望对需要的朋友有所帮助!

使用版本:tp5.1、 thinkphp-queue 2.0

mysql超时断线问题

队列任务运行一段时间,出现:SQLSTATE[HY000]: General error: 2006 MySQL server has gone away报错。

解决方法和分析:

配置文件database.php中配置断线重连:

  // 是否需要断线重连
  'break_reconnect'  =>  true,
  // 断线标识字符串
  'break_match_str'  => ['2006'],

配置后虽然日志中会出现另一个报错:PDO::prepare(): send of 60 bytes failed with errno=32 Broken pipe,但并不影响程序运行结果。因为断线重连后,程序都会抛出错误:

...} catch (\PDOException $e) {
 if ($this->isBreak($e)) {
 return $this->close()->query($sql, $bind, $master, $pdo);
 }

 throw new PDOException($e, $this->config, $this->getLastsql());} catch (\Throwable $e) {
 if ($this->isBreak($e)) {
 return $this->close()->query($sql, $bind, $master, $pdo);
 }

 throw $e;} catch (\Exception $e) {
 if ($this->isBreak($e)) {
 return $this->close()->query($sql, $bind, $master, $pdo);
  }
 throw $e;}

如何在docker环境进行进程监护

一般情况下,可以使用supervisor监护队列进程。配合docker使用的话,大概有几方案:

1.将supervisor安装到php服务所在的容器中

2.跑一个新的容器来运行队列任务(不用supervisor,容器本身是一个daemon)

3.直接在现有的php容器运行队列任务(命令行使用–daemon选项)

方法一supervisor参考配置(放在/etc/supervisor/conf.d, 文件命名为{file-name}.conf):

[program:my_queue_name]process_name=%(program_name)s_%(process_num)02d
command=php /path/to/think queue:work --queue=your-queue-name --sleep=3 --daemon
autostart=trueautorestart=truenumprocs=1user=root
stopasgroup=truekillasgroup=trueredirect_stderr=truestdout_logfile=/path/to/your-queue.log

方法二新开一个镜像参考配置(在docker-compose.yml中添加服务):

php-queue:
 container_name: queue
 image: docker_php-fpm73
 restart: always
 command: php path/to/think queue:work --sleep=3
 volumes:
 - ../project:/var/www/html - ./conf/php:/usr/local/etc/php - ./conf/php/conf.d:/usr/local/etc/php/conf.d - ./conf/supervisor:/etc/supervisor/conf.d
 networks:
 - mysql - nginx

方法三有点hack,为了不大改线上环境,最后使用方法三(在host机子操作)。

启动:docker exec -i php7 php /path/to/think queue:work --queue=my-queue-name --sleep=3 --daemon

重启:docker exec -i php7 php /path/to/think queue:restart (重启后发现队列进程消失了),然后再启动

查看队列进程: ps -aux | grep queue

日志调整

有时候某些原因程序出错,会有大量日志生成,最好调整下日志,单独出来。在配置文件config/queue.php开头添加:

use think\facade\Log;Log::init([
    'single'    => 'queue',
    'file_size' => 1024 * 1024 * 10,
    'level'     => ['error'],]);

日志将输出到runtime目录的queue-cli.log文件                                                    

声明:本文转载于:learnku,如有侵犯,请联系admin@php.cn删除