web服务超时处理

Posted by chunpat on September 6, 2019

事由

执行285675条mysql数据操作的时候,页面出现nginx异常An error occurred.报错。查看日志,详情如下:

2019/09/06 14:32:46 [error] 9#9: *7 upstream timed out (110: Connection timed out) while 

reading response header from upstream, client: 172.26.0.1, server: localhost, request: 

"GET /index/elasticsearch/setDocument HTTP/1.1", upstream: "fastcgi://172.26.0.3:9000", 

host: "192.168.199.182:8002"

解决

本地开发环境,由于要执行这些数据的处理,加长超时时间处理,所以:

nginx.conf


http {
    keepalive_timeout  600; //10分钟
    client_header_timeout 600;
    client_body_timeout 600;
 
    server{
         proxy_read_timeout 600;
 
         location ~ \.php$ {
                  fastcgi_connect_timeout 600s;
                  fastcgi_send_timeout 600s;
                  fastcgi_read_timeout 600s;
                  ...
         }
    }
 
}


php对应的方法下加上 set_time_limit(0);


/**
 * 数据写入
 * @author: chunpat
 * Date: 2019/9/5
 * @return \think\Response
 */
public function setDocument(){
    set_time_limit(0);
    $count = DepotGoodsDetail::count();

    $pageSize = 500;
    for ($i = 0;$i< ceil($count / $pageSize); $i++){
        $offset = $pageSize * $i;
        $data = DepotGoodsDetail::limit($offset,$pageSize)->column('id,goods_sn,goods_name,skus_json,classify_type_id,store_id');
        $documents = [];
        foreach ($data as $id => $vo) {
            $sku = implode(" ", json_decode($vo['skus_json'], true));
            $documents[] = [
                'goods_detail_id' => $vo['id'],
                'name' => $vo['goods_sn'] . ' ' . $vo['goods_name'] . ' ' . $sku,
                'search_name' => $vo['goods_sn'] . ' ' . trim($vo['goods_name'] . $sku),
                'classify_type_id' => $vo['classify_type_id'],
                'store_id' => $vo['store_id'],
            ];
        }
        ElasticsearchLogic::setDocoments('oh_depot_goods_name_list',$documents);
        unset($documents);
        sleep(2);
    }

    return $this->successResponse();
}

参考


Creative Commons License
本作品采用CC BY-NC-ND 4.0进行许可。转载,请注明原作者 chunpat 及本文源链接。