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\n";
	}
}

3.執行任務

打開命令行工具,進入項目的/protected 目錄下 輸入yiic命令即出現提示,提示列表顯示剛纔寫的任務文件
E:\project\app\protected>yiic
Yii command runner (based on Yii v1.1.12)
Usage: E:\zeee\zyd\protected\yiic.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 可實現任務處理
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章