• Yii --Command 任务处理


    1.配置,执行任务所需要的组件

    任务配置文件:/protected/config/console.php 
    配置方法跟配置main文件差不多
    <?php
    
    // This is the configuration for yiic console application.
    // Any writable CConsoleApplication properties can be configured here.
    return array(
    	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    	'name'=>'My Console Application',
    	// application components
    
    	// 自动载入的模型和组件类
    	'import'=>array(
    			'application.models.*',//载入"application/models/"文件夹下的所有模型类
    			'application.components.*',//载入"application/components/"文件夹下的所有应用组件类
    			'application.extensions.*',//载入"application/extensions/"文件夹下的所有应用组件类
    	),
    
    	'components'=>array(
    			// uncomment the following to use a MySQL database
    			'db'=>array(
    					'connectionString' => 'mysql:host=localhost;dbname=dbname',//连接mysql数据库
    					'emulatePrepare' => true,
    					'username' => 'root',//MySQL数据库用户名
    					'password' => '123456',//MySQL数据库用户密码
    					'charset' => 'utf8',//MySQL数据库编码
    					'tablePrefix' => 'zd_', //MySQL数据库表前缀
    					'enableProfiling'=>true,
    					'enableParamLogging'=>true,
    			),
    			//加载Email组件
    			'mailer' => array(
    					'class'     => 'application.extensions.mailer.EMailer',
    			),
    	),
    );

    2.任务文件

    放在 /protected/commands/ 文件目录下继承 CConsoleCommand 基类的为任务文件 命名方法为   任务名称+Command
    例如 GoCommand.php
    <?php
    
    /**
     * 自动运行文件
     */
    class GoCommand  extends CConsoleCommand
    {
    
    
    	/**
    	 * 死循环输出
    	 */
    	public function run(){
    		
    		for($i=1;$i>0;$i++){
    			self::echoWord($i);
    			sleep(2);//休眠2秒
    			
    			//跳出
    			if(i==500){
    				break;
    			}
    		}
    	}
    
    	/**
    	 * 输出hollo word
    	 */
    	public function echoWord($i){
    		echo "hollo word --$i
    ";
    	}
    }

    3.执行任务

    打开命令行工具,进入项目的/protected 目录下 输入yiic命令即出现提示,提示列表显示刚才写的任务文件
    E:projectappprotected>yiic
    Yii command runner (based on Yii v1.1.12)
    Usage: E:zeeezydprotectedyiic.php <command-name> [parameters...]
    
    The following commands are available:
    - go
    - mailqueue
    - message
    - migrate
    - shell
    - webapp
    
    To see individual command help, use the following:
    执行命令 yiic go 可实现任务处理
  • 相关阅读:
    一个配置引发的血案
    软件工程之学习方法篇
    开篇
    重拾《 两周自制脚本语言 》- 中文关键字与原生函数
    重拾《 两周自制脚本语言 》- 支持中文标识符
    将《 两周自制脚本语言 》测试中使用的接口中文化
    5分钟入门LingaScript-尝鲜中文版TypeScript
    为《 两周自制脚本语言 》添加中文测试代码
    2019-02-18 扩展Python控制台实现中文反馈信息之二-正则替换
    2019-02-14 1992年日本对母语编程的可读性比较实验
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3149603.html
Copyright © 2020-2023  润新知