ThinkPHP5结合command定时任务扩展内存溢出问题

 

使用命令行进入项目文件,不是进入到public目录下

ThinkPHP5结合command定时任务扩展内存溢出问题_第1张图片

执行php think emailSend之后出现以下错误信息

而且我的command目录是在app/admin/controller目录下的

ThinkPHP5结合command定时任务扩展内存溢出问题_第2张图片

当然admin模块已经有个config.php配置了.出现内存溢出的问题是因为框架调试模式设置的是debug=true的情况下,因为Log::recorde()方法是先存储在内存中,在执行Log::save()之后才会写入到log文件中的

解决方案:

1.可以把全局的debug模式关闭,debug=false

          这样做的问题是:其他模块需要调试信息的话,是没有log记录的,所以我放弃这个方法了

2.可以修改thinkphp/library/think目录下的Log.php文件

ThinkPHP5结合command定时任务扩展内存溢出问题_第3张图片

      找到Log.php文件中的record方法和save方法,代码如下:

  /**
     * 记录调试信息
     * @param mixed  $msg  调试信息
     * @param string $type 信息类型
     * @return void
     */
    public static function record($msg, $type = 'log')
    {
        self::$log[$type][] = $msg;
        if (IS_CLI && count(self::$log[$type]) > 100) {
            // 命令行下面日志写入改进
            self::save();
        }
    }



    /**
     * 保存调试信息
     * @return bool
     */
    public static function save()
    {
        if (!empty(self::$log)) {
            if (is_null(self::$driver)) {
                self::init(Config::get('log'));
            }

            if (!self::check(self::$config)) {
                // 检测日志写入权限
                return false;
            }

            if (empty(self::$config['level'])) {
                // 获取全部日志
                $log = self::$log;
                if (!App::$debug && isset($log['debug'])) {
                    unset($log['debug']);
                }
            } else {
                // 记录允许级别
                $log = [];
                foreach (self::$config['level'] as $level) {
                    if (isset(self::$log[$level])) {
                        $log[$level] = self::$log[$level];
                    }
                }
            }

            $result = self::$driver->save($log);
            if ($result) {
                self::$log = [];
            }

            return $result;
        }
        return true;
    }

    当然修改底层文件代码有未知风险,也对项目的其他模块开发有影响,所以我放弃了这个方法

3.这个方法是我采用的,在我的emailSend文件同级目录下新建一个ConfigSet.php文件,代码如下:

false,
//            'cache'                  => [
//                // 驱动方式
//                'type'   => 'File',
//                // 缓存保存目录
//                'path'   => CACHE_PATH,
//                // 缓存前缀
//                'prefix' => '',
//                // 缓存有效期 0表示永久缓存
//                'expire' => 0,
//            ],
        ]);
    }
}

4.然后,我的emailSend文件继承ConfigSet.php文件之后,执行命令行命令  

 代码中注释的cache设置,我的项目是报错了,报错信息是:

ThinkPHP5结合command定时任务扩展内存溢出问题_第4张图片

   注释的释放之后就执行成功了:

5.最简洁的办法,在定时任务文件中(我的文件时EmailSend.php)添加如下代码:

protected function initialize(Input $input, Output $output)
    {
        parent::initialize($input, $output); // TODO: Change the autogenerated stub
        App::$debug = false;
    }

 

你可能感兴趣的:(TP5,command扩展,内存溢出问题,PHP)