【问题标题】:gateway timeout error while running cron job运行 cron 作业时出现网关超时错误
【发布时间】:2018-09-19 21:16:22
【问题描述】:

在共享软管上运行 cron 作业时,任何人都可以为 gateway timeout 504 错误提出任何解决方案。我已经尝试过 sleep 功能,但它不起作用,我有以下用于 cron 作业的功能 -

public function checkOrderStatus(){
        $orders = Order::select('id')
                    ->whereNotIn('status', ['COMPLETED', 'CANCELLED', 'PARTIAL', 'REFUNDED'])
                    ->where('api_order_id', '!=', null)
                    ->orderBy('id', 'desc')
                    ->pluck('id')
                    ->toArray();

        $collection = collect($orders);

        $chunks = $collection->chunk(20);

        $request = new \Illuminate\Http\Request();
        foreach($chunks as $ids){
            foreach($ids as $id){
                $request->replace(['id' => $id]);
                $rep = $this->getOrderStatusFromAPI($request);
            }
            sleep(10);
        }
    }

getOrderStatusFromAPI() 函数调用 3rd 方 API 来获取一些记录。 checkOrderStatus() 函数当前在每个 cron 调用中获取大约 300 条记录。请提出服务器升级以外的任何解决方案。非常感谢!!

【问题讨论】:

    标签: php laravel-5 cron


    【解决方案1】:

    您的问题有多种解决方案。如果您将 NGINX 与 FastCGI 一起使用,请尝试:

    php.ini 的变化

    尝试在 php.ini 文件中提高 max_execution_time 设置(CentOS 路径为 /etc/php.ini):

    max_execution_time = 150
    

    PHP-FPM 的变化

    尝试在 php.ini 文件中提高 request_terminate_timeout 设置(CentOS 路径为 /etc/php-fpm.d):

    request_terminate_timeout = 150
    

    Nginx 配置的变化

    最后,在我们的 Nginx 虚拟主机配置中添加 fastcgi_read_timeout 变量:

    location ~* \.php$ {
        include         fastcgi_params;
        fastcgi_index   index.php;
        fastcgi_read_timeout 150;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    }
    

    重新加载 PHP-FPM 和 Nginx:

    service php–fpm restart
    service nginx restart
    

    【讨论】:

    • 感谢您的回复,但正如我所提到的 - 它是共享主机,因此我无法更改配置文件。
    猜你喜欢
    • 2019-12-31
    • 2017-06-08
    • 2015-03-15
    • 2014-09-09
    • 1970-01-01
    • 2014-10-02
    • 2012-04-28
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多