在 Yii2 队列扩展 中报错:The process “‘/usr/local/php/bin/php’ ‘/mcloud/www/ccp_api/yii’ ‘pub-article-queue/exec’ ‘103’ ‘300’ ‘1’ ‘3555’ ‘–color='” exceeded the timeout of 300 seconds.

1、在 Yii2 队列扩展 中报错:The process “‘/usr/local/php/bin/php’ ‘/mcloud/www/ccp_api/yii’ ‘pub-article-queue/exec’ ‘103’ ‘300’ ‘1’ ‘3555’ ‘–color='” exceeded the timeout of 300 seconds.。如图1

图1

[103] common\jobs\PubArticleJob (attempt: 1, PID: 3555) is finished with error: Symfony\Component\Process\Exception\ProcessTimedOutException: The process "'/usr/local/php/bin/php' '/mcloud/www/ccp_api/yii' 'pub-article-queue/exec' '103' '300' '1' '3555' '--color='" exceeded the timeout of 300 seconds. in /mcloud/www/ccp_api/vendor/symfony/process/Process.php:1213
Stack trace:
#0 /mcloud/www/ccp_api/vendor/symfony/process/Process.php(425): Symfony\Component\Process\Process->checkTimeout()
#1 /mcloud/www/ccp_api/vendor/symfony/process/Process.php(249): Symfony\Component\Process\Process->wait()
#2 /mcloud/www/ccp_api/vendor/yiisoft/yii2-queue/src/cli/Command.php(193): Symfony\Component\Process\Process->run(Object(Closure))
#3 /mcloud/www/ccp_api/vendor/yiisoft/yii2-queue/src/cli/Command.php(126): yii\queue\cli\Command->handleMessage('103', 'O:25:"common\\jo...', '300', '1')
#4 [internal function]: yii\queue\cli\Command->yii\queue\cli\{closure}('103', 'O:25:"common\\jo...', '300', '1')
#5 /mcloud/www/ccp_api/vendor/yiisoft/yii2-queue/src/cli/Queue.php(144): call_user_func(Object(Closure), '103', 'O:25:"common\\jo...', '300', '1')
#6 /mcloud/www/ccp_api/vendor/yiisoft/yii2-queue/src/drivers/redis/Queue.php(61): yii\queue\cli\Queue->handleMessage('103', 'O:25:"common\\jo...', '300', '1')
#7 [internal function]: yii\queue\redis\Queue->yii\queue\redis\{closure}(Object(Closure))
#8 /mcloud/www/ccp_api/vendor/yiisoft/yii2-queue/src/cli/Queue.php(117): call_user_func(Object(Closure), Object(Closure))
#9 /mcloud/www/ccp_api/vendor/yiisoft/yii2-queue/src/drivers/redis/Queue.php(68): yii\queue\cli\Queue->runWorker(Object(Closure))
#10 /mcloud/www/ccp_api/vendor/yiisoft/yii2-queue/src/drivers/redis/Command.php(56): yii\queue\redis\Queue->run(false)
#11 [internal function]: yii\queue\redis\Command->actionRun()
#12 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#13 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/Controller.php(181): yii\base\InlineAction->runWithParams(Array)
#14 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller->runAction('run', Array)
#15 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/Module.php(534): yii\console\Controller->runAction('run', Array)
#16 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction('pub-article-que...', Array)
#17 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/Application.php(148): yii\console\Application->runAction('pub-article-que...', Array)
#18 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest(Object(yii\console\Request))
#19 /mcloud/www/ccp_api/yii(23): yii\base\Application->run()
#20 {main}.

2、在队列:pub-article-queue 中运行时间超过了 300 秒的超时时间限制。查看队列:pub-article-queue 的配置项。作业处理的最长时间,单位(秒),其值等于 300 秒。

'pubArticleQueue' => [ // 发布文章队列
    'class' => 'yii\queue\redis\Queue',
    'redis' => 'redis', // Redis 连接组件或它的配置
    'channel' => 'cpa:queue:pub:article', // 队列键前缀
    'ttr' => 5 * 60, // 作业处理的最长时间,单位(秒)
    'attempts' => 1, // 作业处理的最大尝试次数
    'on afterExec' => ['common\components\queue\PubArticleEventHandler', 'afterExec'], // 每次成功执行作业后
    'on afterError' => ['common\components\queue\PubArticleEventHandler', 'afterError'], // 在作业执行期间发生未捕获的异常时
    'as log' => 'yii\queue\LogBehavior',
],

3、分析代码,初步怀疑是某个方法存在死循环,在方法中打印输出日志文件。

    /**
     * @param $basePath
     * @param $relativePath
     * @return array
     */    public static function zoomFile($basePath,$relativePath){
        @file_put_contents(Yii::getAlias('@runtime') . '/common-services-WxArticleService-zoomFile-relativePath-0-' . microtime(true) . '-' . mt_rand()  . '.txt', print_r(filesize($basePath . $relativePath), true), FILE_APPEND | LOCK_EX);
        // 获取等比例缩放图 按照指定的宽度缩放
        $contentEqCompressImage = ImageManagerService::getContentEqCompressImage($basePath, $relativePath, 960, 'wx');
        if (filesize($contentEqCompressImage['absolute_url']) / 1024 / 1024 > WxApi::IMG_SIZE) {
            self::zoomFile($contentEqCompressImage['bath_path'],$contentEqCompressImage['relative_path']);
        }
        @file_put_contents(Yii::getAlias('@runtime') . '/common-services-WxArticleService-zoomFile-relativePath-1-' . microtime(true) . '-' . mt_rand()  . '.txt', print_r(filesize($contentEqCompressImage['absolute_url']), true), FILE_APPEND | LOCK_EX);
        return $contentEqCompressImage;
    }

4、查看日志文件列表如下,确定是死循环,无限地运行此方法。

-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654458.9219-1750979813.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654459.0625-1831884460.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654459.1974-463273601.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654459.3297-919543675.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654459.4673-1269097860.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654459.5992-1943098976.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654459.7392-216079189.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654459.8643-1638931132.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654460.0062-423432329.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654460.1336-1098374207.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654460.2586-415748258.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654460.4237-575533057.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654460.5654-288957913.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654460.7071-1133068454.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654460.8422-1242355308.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654460.9789-9457376.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654461.1175-2060694234.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654461.2496-541989633.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654461.3765-878062054.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654461.5172-1277280564.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654461.6538-1127469764.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654461.8073-751096802.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654461.9708-517915729.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654462.0926-1968281916.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654462.2176-1607878648.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654462.3476-193020564.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654462.4664-1607338781.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654462.5832-287954690.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654462.7004-1764170938.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654462.8372-964892149.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654462.9685-1374806731.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654463.1074-2019478455.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654463.2326-580727938.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654463.3703-1746740924.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654463.51-1173416615.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654463.654-245574664.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654463.785-1966035946.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654463.9282-401315863.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654464.0551-1528211151.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654464.197-457782281.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654464.3438-1548268735.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654464.4882-1878770225.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654464.6303-638953717.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654464.7698-738392284.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654464.9036-657677212.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654465.0422-1015158770.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654465.1729-12482654.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654465.3266-1567483898.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654465.465-1980251142.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654465.5879-665123006.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654465.7257-51655869.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654465.8668-1708965469.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654466.0006-1673619092.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654466.1367-799767428.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654466.2712-1380864922.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654466.4693-194311921.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654466.6085-29689994.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654466.7474-992298987.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654466.8929-449058410.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654467.0261-2035107847.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654467.1665-149744013.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654467.3057-250525434.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654467.4259-1969764702.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654467.5645-1144163093.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654467.706-1340334251.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654467.833-1392913915.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654467.9614-1472869952.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654468.1118-1023320156.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654468.2498-1388667642.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654468.3716-1240386686.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654468.4934-44793397.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654468.6219-1404370653.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654468.7611-524246945.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654468.894-957707083.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654469.0359-17434399.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654469.1776-682402104.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654469.312-1000574445.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654469.4503-445348410.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654469.5939-1525660465.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654469.7302-1692466072.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654469.8693-1257085507.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654470.0088-1755524707.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654470.1474-1973165486.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654470.281-1978075478.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654470.4243-1399205613.txt
-rw-r--r-- 1 root root    7 Jul 30 22:14 common-services-WxArticleService-zoomFile-relativePath-0-1627654470.5636-231967638.txt

5、重新编辑此方法,以避免死循环。未再报错超时的问题。如图2

图2

    /**
     * @param $basePath
     * @param $relativePath
     * @return array
     */    public static function zoomFile($basePath,$relativePath){

        // 获取等比例缩放图 按照指定的宽度缩放
        $contentEqCompressImage = ImageManagerService::getContentEqCompressImage($basePath, $relativePath, 960, 'wx');
        if (filesize($contentEqCompressImage['absolute_url']) / 1024 / 1024 > WxApi::IMG_SIZE) {
            $contentEqCompressImage = ImageManagerService::getContentEqCompressImage($basePath, $relativePath, 720, 'wx');
            if (filesize($contentEqCompressImage['absolute_url']) / 1024 / 1024 > WxApi::IMG_SIZE) {
                $contentEqCompressImage = ImageManagerService::getContentEqCompressImage($basePath, $relativePath, 480, 'wx');
            }
        }
        return $contentEqCompressImage;
    }

 

永夜