douman9420 2017-01-04 13:53
浏览 28

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

<?php
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
Backtrace:

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
Backtrace:
  • 写回答

2条回答 默认 最新

  • doulu2576 2017-01-05 09:08
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料