有什么方法可以防止php cornjob占用过多内存

时间:2019-06-27 10:04:25

标签: php mysql codeigniter

我正在从Stripe API中提取余额交易,并使用cornjob将其存储到我的数据库中,cornjob在两分钟后运行。现在,我面临一个问题,因为它在一小时内消耗了太多内存。

这是我的Stripe和PHP代码,在这里我关闭数据库连接并退出for-each循环以停止内存泄漏。

function index(){
    set_time_limit(0);  
    $DB=$this->load->database('testing', true);
    $sql="SELECT stripeKey FROM app_key";
    $qry=$DB->query($sql);
    $keys= $qry->result_array();
    $DB->close();

    if(is_array($keys) && count($keys)>0){
       foreach($keys as $k){
         if($k['stripeKey']!=''){
             try {                      
                   $this->stripelib->getStripe()->setApiKey($k['stripeKey']);

               $this->callApi();

              }catch(\Stripe\Error\Card $e) {

                $messages[] = $this->stripeErrorMsg($e);

              }
         }
       }
    }   
}

function callApi(){

    $balance = $this->stripelib->getBalanceTransaction()->all(
        array(
                'limit'   => 100,
                'created' => array(
                    'gte' => strtotime('-1 day 00:00'),
                    'lte' => strtotime('1 day 23:59') 
                )
            )
        );

    foreach ($balance->data as $bl){
      //echo $bl->id . " created " . date("d-m-y", $bl->created) . "<br>";
    }
    $this->addRecord($balance->data);

    while ($balance->has_more){
        $balance = $this->stripelib->getBalanceTransaction()->all(
        array(
            "limit" => 100,
            "created" => array(
                "gte" => strtotime('-1 day 00:00'),
                'lte' => strtotime('1 day 23:59')
                ), 
            "starting_after" => $bl->id)
        );

        foreach ($balance->data as $bl){
          //echo $bl->id . " created " . date("d-m-y", $bl->created) . "<br>";
        }
        $this->addRecord($balance->data);

    }   
}

function addRecord($data)
{
    $i=0; 
    fee1=0.00;
    $DB=$this->load->database('testing', true);
    $DB2=$this->load->database('testing2', true);

    if(is_array($data) && count($data)>0){
        foreach($data as $b){
            $i++;

            if($b['description']!='' )
            {

                $sql="SELECT fee FROM my_app WHERE description='".$b['description']."' ";
                $qry=$DB->query($sql);
                $cs= $qry->row_array(); 


                if(is_array($cs) && count($cs)>0){
                    $fee1=$cs['fee'];
                }   
            }



            $fee2=0.00;
            ANOTHER MQSQL QUERY - FETCHING ONLY 5 RECORDS

            $chrg= $this->stripelib->getCharge()->retrieve($b['source']);               $chrgType=$payment['payment_method_details']->type; 

            $rendA = $this->stripelib->getRefund()->retrieve($b['source']); 
            $refund_reason=$rendA ['reason'];   


            $sql="INSERT IGNORE INTO `transactions`(`id`, `fee`, `refund_reason`, `payment_type`) VALUES 
            ('".$b['id']."', '".fee1."', '".$refund_reason."' , '".$paymentType."' )";

            $query = $DB2->query($sql);



            if(count($data)==$i){
                exit;
            }

        }

    }


    $DB->close();
    $DB2->close();
}

在日志中,我得到了- 27 / Jun / 2019:08:59:42 +0100]“选项/检查HTTP / 1.0” 200-“-”“-”

如何解决该过多的内存使用问题。

0 个答案:

没有答案