Yii2 console执行定时脚本

 

为什么要做crontab脚本

我们的项目使用YII2开发,并不是很大的一个电商平台,pv、IP访问量并不是很高,但客户的数据是日积月累已经产生100万条数据了,之前更新订单等数据使用定时脚本直接访问内网的一个url,因为更新订单只是部分数据,php后台执行10s完全可以胜任。

现在需求是这样的:之前系统没有考虑客户的积分等级,现在需要加入这个功能,所以必须给客户表添加积分字段,在添加一个积分记录表,那么之前老客户数据因下单,本应该也生成对应的积分等级,而且要每晚上执行一次昨天下单客户对应的成长值,每月定期计算一次过期积分清零操作(类似支付宝会员积分、网易游戏积分:http://game.163.com/news/2011/3/22/442_233242.html)。好,需求就这么简单。

继续使用crontab 访问一个内网的url进行更新操作,这样不可取,因为现在是100w条数据,php的url访问肯定会超时。

那就只能执行php脚本了,执行php脚本想了两个方案,第一种,写一个pdo类,直接单独文件执行,第二种,使用YII自带的console,两者之间选择了后者。

 

Test Yii2 console

官文手册: http://www.yiiframework.com/doc-2.0/guide-tutorial-console.html

项目用的是YII2的yii2-app-advanced 高级版,所以下载 https://github.com/yiisoft/yii2-app-advanced 中的init文件(项目中有,但未使用Del了),执行初始化生成yii文件

目录结构

F:projectwebyii2-app-advanced-master
$ php init
Yii Application Initialization Tool v1.0

Which environment do you want the application to be initialized in?

  [0] Development
  [1] Production

  Your choice [0-1, or "q" to quit] 0

  Initialize the application under 'Development' environment? [yes|no] yes

  Start initialization ...

      exist backend/config/main-local.php
            ...overwrite? [Yes|No|All|Quit] yes
  overwrite backend/config/main-local.php
  unchanged backend/config/params-local.php
   generate backend/config/test-local.php
   generate backend/web/index-test.php
      exist backend/web/index.php
            ...overwrite? [Yes|No|All|Quit] yes
  overwrite backend/web/index.php
      exist common/config/main-local.php
            ...overwrite? [Yes|No|All|Quit] All
  overwrite common/config/main-local.php
  unchanged common/config/params-local.php
   generate common/config/test-local.php
  overwrite console/config/main-local.php
  unchanged console/config/params-local.php
  overwrite frontend/config/main-local.php
  unchanged frontend/config/params-local.php
   generate frontend/config/test-local.php
   generate frontend/web/index-test.php
  overwrite frontend/web/index.php
   generate yii
   generate yii_test
   generate yii_test.bat
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in frontend/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0755 yii
      chmod 0755 yii_test

  ... initialization completed.

HelloController

namespace consolecontrollers;
 
use yiiconsoleController;

class HelloController extends Controller {

    /**
     * This command echoes what you have entered as the message.
     * @param string $message the message to be echoed.
     */
    public function actionIndex() {
        echo "hello world" . "
";
    }

}

访问试试

$ php yii hello
hello world

这里有个问题需要说下,刚开始我执行的时候一直提示:

Error: Unknown command "hello".

因为目录别名的问题,所以,如果没有生成bootstrap.php 中的别名配置,可以直接在yii文件中加上:

Yii::setAlias('@console', (dirname(__DIR__)) . '/console');

如果在console中要使用ActiveRecord 操作 models,必须设置common别名

Yii::setAlias('common', (dirname(__DIR__)) . '/common');

config文件中db的引用应该使用决定路径而不是相对路径

$db= require(dirname(dirname(__DIR__))."/common/config/main.php");

否则提示: 找不到../../common/config/main.php

代码下载: http://files.cnblogs.com/files/dcb3688/yii2-console.7z

YII2 的基础版那更简单了,执行下载 https://github.com/yiisoft/yii2-app-basic  解压后发现根目录下已经有yii文件和commands目录了,

可以直接执行

$ php yii hello
hello world

ActiveRecord操作

在上面说过,要连接数据必须要给common使用别名执行路径,下面测试下数据库操作

 public function actionCreate() {

        $model = new commonmodelstest();
        $model->text = "sdkfjlskdjflsdf";
        $model->save();
        echo 'ok';
    }

查看数据库已经插入。

 

crontab执行

写好了逻辑代码,直接用Linux的crontab命令执行定时任务

关于crontab命令可参考: http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html

原文地址:https://www.cnblogs.com/dcb3688/p/4608059.html