关于thinkphp-queue问题解决
2022-11-24 11:02:51
142
{{single.collect_count}}

下面由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)02dcommand=php /path/to/think queue:work --queue=your-queue-name --sleep=3 --daemonautostart=trueautorestart=truenumprocs=1user=rootstopasgroup=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文件

php入门到就业线上直播课:立即学习
全程直播 + 实战授课 + 边学 + 边练 + 边辅导

以上就是关于thinkphp-queue问题解决的详细内容,更多请关注php中文网其它相关文章!

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}