没有不值得去解决的问题,也没有不值得去学习的技术!

Yii2 Queue Extension Error: Process Exceeded the 300-Second Timeout

In the Yii2 queue extension, an error occurred: 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. In the Yii2 queue extension, an error occurred: 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. As shown in Figure 1.

In the Yii2 queue extension, an error occurred: 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.
Figure 1
Plaintext

[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. The execution time in the queue: pub-article-queue exceeded the 300-second timeout limit. Check the configuration options for the queue: pub-article-queue. The maximum time for processing a job, in seconds, is set to 300 seconds.

PHP

'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. After analyzing the code, I initially suspected that an infinite loop existed in a method, so I printed log output within the method.

PHP

    /**
     * @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. Reviewing the log file list below, I confirmed that an infinite loop was causing the method to run indefinitely.

Plaintext

-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. I rewrote this method to avoid the infinite loop. The timeout error no longer occurred. As shown in Figure 2.

Rewrote this method to avoid the infinite loop. The timeout error no longer occurred
Figure 2
PHP

    /**
     * @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;
    }

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系