使用cron耗尽内存限制运行脚本

时间:2018-08-20 05:31:53

标签: php codeigniter

我在CodeIgniter框架中制作了一个如下所示的脚本。

没有给我以下错误的确切原因。 我已尝试解决此错误,这是我的最佳经验。但是对于错误,仍然不太清楚。

我知道有可能在功能启动时在行下方设置解决方案。 但是据我所知,这并不是一个可靠的解决方案。

ini_set("memory_limit", "-1");

致命错误:允许的内存大小为134217728字节,已用尽(尝试分配85个字节)

代码:

function read_data(){
        $fp=fopen("/var/log/test/testdata.log","a+");
        $start_date = "2018-07-12 00:00:00";
        $end_date = "2018-07-31 23:59:59";
        $table = "july_calls";
        $table_prefix = 'custom_';
        $insert_table = 'july_cc_cps';
        $start_date_strtotime = strtotime($start_date);
        $end_date_strtotime = strtotime($end_date);
        $j=0;
        $old_date = strtotime(date("Y-m-d H:i:s",strtotime($start_date .' -1 day')));
        for($i= $start_date_strtotime;$i <= $end_date_strtotime;$i++){
        $table_name = $table_prefix.date("Y_m_d",$i);
        $date = date("Y-m-d",$i);
        $datetime = date("Y-m-d H:i:s",$i);
        if(date("d",$old_date) != date("d",$i)){ 
            $drop_table_query = "DROP TABLE IF EXISTS ".$table_name;
            //fwrite($fp,"Drop table query :".$drop_table_query."\n");
            $this->db->query($drop_table_query);
            $create_table_query ="create table ".$table_name."(
                id varchar(60) NOT NULL,
                userid int(11) NULL DEFAULT 0,
                trunk_id smallint(6) NOT NULL,
                callstart datetime NOT NULL default '0000-00-00 00:00:00',
                callend datetime NOT NULL default '0000-00-00 00:00:00',
                billseconds smallint(6) NOT NULL default 0
            ) ";
            fwrite($fp,"create table query :".$create_table_query."\n");
                    $this->db->query($create_table_query);
                    $insert_data_query = "insert into ".$table_name." select id,userid,trunk_id,callstart,callend,billseconds from ".$table." where callend >= '".$date." 00:00:00' AND callstart <='".$date." 23:59:59' and billseconds > 0 and trunk_id=10";
            //fwrite($fp,"insert data query :".$insert_data_query."\n");
                    $this->db->query($insert_data_query);
                    $before_yesterday_table = $table_prefix.date("Y_m_d",strtotime($datetime.' -2 day'));
                    $drop_old_table_query = "DROP TABLE IF EXISTS ".$before_yesterday_table;
                //fwrite($fp,"drop old table query :".$drop_old_table_query."\n");
                    $this->db->query($drop_old_table_query);
                    $drop_old_table_query = $drop_table_query= $create_table_query = null;
        }
        $insert_array =array();
        $query = "select count(uniqueid) as count,trunk_id from ".$table_name." where callstart <= '".$datetime."' AND callend >='".$datetime."' and trunk_id > 0 and billseconds > 0 and trunk_id=10 group by trunk_id";
        //fwrite($fp,"Trunk query :".$query."\n");
            $trunk_result = $this->db->query($query);
            if($trunk_result->num_rows() > 0 ){
                $trunk_result = $trunk_result ->result_array();
                    foreach($trunk_result as $key=>$value){
                        $insert_array[$i][$value['trunk_id']]['trunk_id'] = $value['trunk_id'];
                        $insert_array[$i][$value['trunk_id']]['asr']=0;
                        $insert_array[$i][$value['trunk_id']]['cc']=$value['count'];
                        $insert_array[$i][$value['trunk_id']]['cps']=0;
                        $insert_array[$i][$value['trunk_id']]['creation_date'] = date("Y-m-d H:i:s",$i);
                        $insert_array[$i][$value['trunk_id']]['userid']=0;
                    }           
            }
            $query = "select count(uniqueid) as count,trunk_id from ".$table_name." where callstart = '".$datetime."' and trunk_id > 0 and trunk_id=10 group by trunk_id";
                //fwrite($fp,"Trunk cps query :".$query."\n");
                $trunk_result = $this->db->query($query);
                if($trunk_result->num_rows() > 0 ){
                    $trunk_result = $trunk_result ->result_array();
                    foreach($trunk_result as $key=>$value){
                if(isset($insert_array[$i][$value['trunk_id']])){
                            $insert_array[$i][$value['trunk_id']]['cps']=$value['count'];
                }else{
                            $insert_array[$i][$value['trunk_id']]['trunk_id']=$value['trunk_id'];
                            $insert_array[$i][$value['trunk_id']]['asr']=0;
                            $insert_array[$i][$value['trunk_id']]['cc']=0;
                            $insert_array[$i][$value['trunk_id']]['cps']=$value['count'];
                            $insert_array[$i][$value['trunk_id']]['creation_date'] = date("Y-m-d H:i:s",$i);
                            $insert_array[$i][$value['trunk_id']]['userid']=0;
                }
                    }
                }
                if(!empty($insert_array)){
                    foreach($insert_array as $key=>$value){
                        foreach($value as $subkey=>$subvalue){
                            $this->db->insert($insert_table,$subvalue);
                        }
                    }
                }
                $old_date = $i;
                $query ="delete from ".$table_name." where callend <= '".$datetime."'";
                $this->db->query($query);
        }
        exit;
    }

0 个答案:

没有答案