当前位置:  开发笔记 > 编程语言 > 正文

如何在CodeIgniter控制器上运行所有URL所执行的cron作业是从API运行查询,更新数据库并发送电子邮件(从CI内部)?

如何解决《如何在CodeIgniter控制器上运行所有URL所执行的cron作业是从API运行查询,更新数据库并发送电子邮件(从CI内部)?》经验,为你挑选了0个好方法。

我有一个Web应用程序,它在api上运行查询,获取新信息,更改数据库中的变量并在需要时发送电子邮件(比较旧变量和新变量).

我使用Ubuntu Server,我尝试了几种变体来运行它.

我的控制器看起来像这样:

class Cli_only extends CI_Controller {

    public function __construct() {

       parent::__construct();
        is_cli() OR show_404(); // If cronjob ! 

       $this->load->model('kas_model');

        // Sets the server not to have a time out. 
        ini_set('max_execution_time', 0); 
        ini_set('memory_limit', '-1');      
        // Expand the array displays
        ini_set('xdebug.var_display_max_depth', 5);
        ini_set('xdebug.var_display_max_children', 256);
        ini_set('xdebug.var_display_max_data', 1024);
    }



    // This function has to run every day using a corn job. 
    public function cron_job(){
        // does stuff...
    }

所以起初我尝试在我的cronjob上使用常规'curl'命令:

15 15 * * * http://10.0.1.666/tools/Cli_only/cron_job

我在日志文件中收到此错误:

Dec 6 15:30:01 MYserver CRON[1134]: (root) CMD (curl http://10.0.1.66/tools/Cli_only/cron_job) 
Dec 6 15:30:02 MYserver CRON[1133]: (CRON) info (No MTA installed, discarding output)

在这上面运行一个快速的谷歌,我注意到我必须在我的服务器上安装"postfix",我做到了.我重新启动了cron并得到:

Dec  6 16:26:01 MYserver cron[2663]: (root) RELOAD (crontabs/root)
Dec  6 16:26:01 MYserver CRON[2703]: (root) CMD (curl http://10.0.1.666/tools/Cli_only/cron_job)
Dec  6 16:26:01 MYserver postfix/pickup[2479]: 23430102E11: uid=0 from=
Dec  6 16:26:01 MYserver postfix/cleanup[2707]: 23430102E11: message-id=<20151206142601.23430102E11@MYserver>
Dec  6 16:26:01 MYserver postfix/qmgr[2480]: 23430102E11: from=, size=2058, nrcpt=1 (queue active)
Dec  6 16:26:01 MYserver postfix/local[2709]: 23430102E11: to=, orig_to=, relay=local, delay=0.02, delays=0.01/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Dec  6 16:26:01 MYserver postfix/qmgr[2480]: 23430102E11: removed

仍然,数据库没有任何变化 - 如果有的话 - CodeIgniter有一个工作的邮件配置,应该只运行URL.

我试过这样的事情:

14 16 * * * wget -O - http://10.0.1.666/tools/Cli_only/cron_job >/dev/null 2>&1

14 16 * * * curl -O - http://10.0.1.666/tools/Cli_only/cron_job >/dev/null 2>&1

14 16 * * * GET -O - http://10.0.1.666/tools/Cli_only/cron_job >/dev/null 2>&1

我真的不知道他们做了什么 - 但仍然无法工作.

编辑:

只是为了清楚,如果我在浏览器上运行控制器,一切都很完美!

这是我在控制器中的所有代码,cron_job()函数就在__construct()函数之后:

input->is_cli_request()) show_error('Direct access is not allowed');          

       $this->load->model('kas_model');

        // Sets the server not to have a time out. 
        ini_set('max_execution_time', 0); 
        ini_set('memory_limit', '-1');      
        // Expand the array displays
        ini_set('xdebug.var_display_max_depth', 5);
        ini_set('xdebug.var_display_max_children', 256);
        ini_set('xdebug.var_display_max_data', 1024);
    }



    // This function has to run every day using a corn job. 
    public function cron_job(){

        // 1. Run the query that gets the table data from the DB('from kas table')
        $data['table'] = $this->kas_model->get_kas_table();

        // 2. Go through each row.
        foreach ( $data['table'] as $row ) {

            // 3.1. But first, get vars! 
            $kas_id            = $row->kas_id;
            $kas_key       = $row->kas_key; 
            $kas_id_aaa        = $row->kas_id_aaa;
            $kas_id_bbb    = $row->kas_id_bbb;
            $kas_rank1_aaa     = $row->kas_rank1_aaa;
            $kas_rank2_aaa     = $row->kas_rank2_aaa;
            $kas_rank1_bbb = $row->kas_rank1_bbb;
            $kas_rank2_bbb = $row->kas_rank2_bbb;

            // 3.2. move to yesterday to make place for a new query result.
            $this->kas_model->move_tod_to_yes($kas_id, $kas_rank2_aaa, $kas_rank2_bbb);

            // 3.3. Run the key query again for today on each one of the keys and insert to DB. 
            if ( ($kas_id_aaa != 0) || ( !empty($kas_id_aaa) ) ) {
                $aaa_rank_today     = $this->get_rank_aaa_by_id_and_kw($kas_id_aaa, $kas_key);
            } 

            if ( ($kas_id_bbb != 0) || ( !empty($kas_id_bbb) ) ) {
                $bbb_rank_today = $this->get_rank_bbb_by_id_and_kw($kas_id_bbb, $kas_key);
            }

            // 3.4. Add the new rank to rank2 in the DB.
            $this->kas_model->add_new_today($kas_id, $aaa_rank_today, $bbb_rank_today);

            // 4. Check difference as Sag described : 
            $msg = ''; 
            $send = FALSE;
            // First if: aaa
            if ( ($aaa_rank_today > 10 ) && ( $kas_rank2_aaa < 30 ) && ( ( abs( $aaa_rank_today - $kas_rank2_aaa ) ) > 10) ) {
                $msg .= 'aaa:
( (Today > 10 ) && ( Yesterday < 30 ) && ( ( |Today - Yesterday| > 10) ) ==> True.

'; $send = TRUE; } // Second if: aaa if ( ( ($kas_rank2_aaa < 5) && ($aaa_rank_today > 10) ) || ( ($aaa_rank_today < 5) && ($kas_rank2_aaa > 10) ) ) { $msg .= 'aaa:
( ( (Yesterday < 5) && (Today > 10) ) || ( (Today < 5) && (Yesterday > 10) ) ) ==> True.

'; $send = TRUE; } // First if: bbb if ( ($bbb_rank_today > 10 ) && ( $kas_rank2_bbb < 30 ) && ( ( abs( $bbb_rank_today - $kas_rank2_bbb ) ) > 10) ) { $msg .= 'bbb:
( (Today > 10 ) && ( Yesterday < 30 ) && ( ( |Today - Yesterday| > 10) ) ==> True.

'; $send = TRUE; } // Second if: bbb if ( ( ($kas_rank2_bbb < 5) && ($bbb_rank_today > 10) ) || ( ($bbb_rank_today < 5) && ($kas_rank2_bbb > 10) ) ) { $msg .= 'bbb:
( ( (Yesterday < 5) && (Today > 10) ) || ( (Today < 5) && (Yesterday > 10) ) ) ==> True.

'; $send = TRUE; } $this->send_mail($kas_id_aaa, $kas_id_bbb, $msg, $send, $aaa_rank_today, $bbb_rank_today, $kas_rank2_aaa, $kas_rank2_bbb, $kas_key); } } // Gets aaa categorys Ranking by ID. public function get_rank_aaa_by_id_and_kw($id, $key, $query_country){ $key_for_url = rawurlencode($key); $found = FALSE; $i = 0; // Create a stream for Json. That's how the code knows what to expect to get. $context_opts = array( 'http' => array( 'method' => "GET", 'header' => "Accepts: categorylication/json\r\n" )); $context = stream_context_create($context_opts); while ($found == FALSE) { // aaa Query $json_query_aaa = "https://api.example.com:666/aaa/ajax/research_key?category_id=$id&term=$key_for_url&page_index=$i&country=$query_country&auth_token=tokentokentoken"; // Get the Json $json_query_aaa = file_get_contents($json_query_aaa, false, $context); // Turn Json to a PHP array $json_query_aaa = json_decode($json_query_aaa, true); // Finally, the main categorys array. $json_query_aaa = $json_query_aaa['key']['phone_categorys']['category_list']; if ( count($json_query_aaa) > 2 ) { for ( $j=0; $j array( 'method' => "GET", 'header' => "Accepts: categorylication/json\r\n" )); $context = stream_context_create($context_opts); while ($found == FALSE) { // aaa Query $json_query_bbb = "https://api.example.com:666/bbb/research_key?category_id=$id&term=$key_for_url&page_index=$i&country=$query_country&auth_token=tokentokentoken"; // Get the Json $json_query_bbb = file_get_contents($json_query_bbb, false, $context); // Turn Json to a PHP array $json_query_bbb = json_decode($json_query_bbb, true); // Finally, the main categorys array. $json_query_bbb = $json_query_bbb['key']['phone_categorys']['category_list']; if ( count($json_query_bbb) > 2 ) { for ( $j=0; $jload->library('email'); $ci->email->initialize($config); $ci->email->from('myEmail@example.pro', 'key Alerting System (KAS)'); $list = array('myEmail.do@gmail.com', 'sag@example.pro'); $ci->email->to($list); $this->email->reply_to('no-reply@example.pro', 'KAS Alert'); $ci->email->subject('KAS Alert!'); $ci->email->message("key: $kas_key

aaa ID:$id_aaa
bbb ID: $id_bbb


$msg

aaa Rank Today: $aaa_rank_today
aaa Rank Yesterday: $aaa_rank_yesterday

bbb Rank Today: $bbb_rank_today
bbb Rank Yesterday: $bbb_rank_yesterday"); $ci->email->send(); } } function test(){ echo 'banana'; } }

编辑#2:

没有is_cli() OR show_404();构造函数时,这很好用.但我确实希望这个控制器只能用于cronjob

我得到了相同的结果:

if (!$this->input->is_cli_request()) show_error('Direct access is not allowed');

推荐阅读
保佑欣疼你的芯疼
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有