Codeigniter Cron作业消耗更多时间然后插入数据库

I have a cron job which call the following codeigniter controller
But it is time costly
Which calculate and operate more operations (on average need 5 minutes)
Then insert values on database

defined('BASEPATH') OR exit('No direct script access allowed');

class Mycron extends CI_Controller {

    public function cron_function()
        // simulate time cost operations
        sleep(300); // 5 mint = 5 *60 = 300 sec

        $this->load->database();    // line1
        $this->db->reconnect();     // line2

        $this           -> db -> set    ( 'source_id',  '11');
        $this           -> db -> set    ( 'title',  'TTL');
        $query = $this  -> db -> insert ( 'my_table' );

        echo 'END ...'; 

My issue
Without both lines line1/line2
When database try to connect after consumed time on operationsthe result will be

SQL Error: 2006: MySQL server has gone away

Then when try to apply the fix RE_CONNECT to database by using 2 lines
We face

A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by 
(output started at .../system/database/drivers/mysqli/mysqli_driver.php:392)
Filename: core/Common.php
Line Number: 568

A PHP Error was encountered
Severity: Error
Message: Call to a member function real_escape_string() on boolean
Filename: mysqli/mysqli_driver.php
Line Number: 392
dou448172583 检查你的`my.cnf'中的wait_timeout变量
3 年多之前 回复


我建议在cron控制器中创建第二个方法,只负责将数据插入数据库。</ p>

然后,在经过时间成本高昂的操作后,您可以发布生成的数据,例如 curl 到第二种方法,因此绕过MySQL超时限制。</ p>
</ div>



I would suggest creating a second method in cron controller responsible only for inserting data into database.

Then, after time costly operation is finished, you could post the generated data with, say, curl to the second method, thus going around MySQL timeout limitation.

I disabled auto-loading of database library
Old code

$autoload['libraries'] = array('database');

to be

$autoload['libraries'] = array();

And load it manually before calling DB statement


So the database connection will open after time of costly operation will be finished

Then this time (of costly operation) will NOT considered as a database synchronous operation time
Opposite of auto-loading of database library which start timer when page loaded

douhan1860 还删除$ this-> db-> reconnect(); 声明
3 年多之前 回复
Csdn user default icon