现场

事发后的某一段log日志

1
2
3
[2020-02-06 14:09:20] production.ERROR: Symfony\Component\Debug\Exception\FatalErrorException: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1191936 bytes) in /project-path/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Query/Builder.php:373
Stack trace:
#0 {main}

500

临时解决办法

放开PHP内存限制,修改php.ini,中memory_limit=-1,然后重启,php-fpm 服务,sudo service php7.1-fpm restart

网站恢复访问后,排查首页代码,定位手改一处过大查询代码(前人遗留)。

问题代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    /**
     * 根据发布时间倒序获取新闻 首页用
     *
     * @param int $limit
     * @return array
     */
    public function getNewsAtIndexPage($limit = 5)
    {
        $newsList = [];
        $rawNews = (new Filter())->getEnableDisplay()->sortByPublishAt()->getNews();
        foreach ($rawNews as $news) {
            $newsList[] = [
                'url'   => $this->getNewsPublishURL($news['type'], $news['_id'],false),
                'title' => $news['title'],
                'cover' => $news['cover'],
                'publish_at'=> $news['publish_at']
            ];
            if(count($newsList) == $limit) break;
        }

        return $newsList;
    }

    public function getNews()
    {
        return $this->model->get();
    }

优化前后结果

500

500

500

好文推荐