Yii Scheduled Tasks

Source: Internet
Author: User

The list of Web application skeletons automatically generated by the YII framework contains a footstep file, Yiic and Yiic.bat. YIIC is used by the Unix/linux platform, and Yiic.bat is used by Windows platforms. If you want to view the script's help to get to the root of the footstep, and then execute Yiic., he will list all available commands, including the system commands provided by Yii and the user-defined commands. If you want to know how to execute a command, you can execute the following command: 1YIIC help if you want to execute a command, you can use the following format: 1YIIC [parameters ...] 1. Command console commands are stored as class files in the directory specified by Cconsoleapplication::commandpath. The default is stored in Protected/commands. Each class must inherit from Cconsolecommand. The format of the class name is Xyzcommand, the first letter of the command is capitalized, and XYZ is the command itself. You can configure CCONSOLEAPPLICATION::COMMANDMAP to have different naming conventions and different directories for the command class. Create a new command you can overwrite Cconsolecommand::run () or write one or more action. The Run method format that overrides the parent class can be: 1public function run ($args) {...} When a command is executed, the Run method will be called, and any arguments appended to the call command will be assigned to $args. An instance of this console can be invoked inside the command using Yii::app (). Starting with version 1.1.1, you can create a global command that is shared by all yii applications on the same machine. To achieve this, you need to define an environment variable named Yii_console_commands, point to an existing directory, and then place the global command class in this directory. 2. Console command action A console command action is a method of a console command class. Method name format: ACTIONXYZ, the first letter of the action name is capitalized, and XYZ is the action itself that is called. command format for executing an action: 1YIIC--option1=value1--option2=value2 ... The Option-value on the back will be assigned to the arguments of this action method. If you give the option name and do not give the corresponding value, then this option will be considered a Boolean value of true. The Action parameter AlsoYou can declare an array type, such as: 1public function Actionindex (array $types) {...} The command to invoke it is: the 1YIIC sitemap index--types=news--types=article Final command call is: Actionindex (' News ', ' article '). Starting with 1.1.6, anonymous parameters and global options are also supported. The anonymous parameter refers to command-line arguments that do not follow the normal option parameter format (the format of options), such as: YIIC sitemap index--limit=5 News, news is an anonymous parameter. To use an anonymous parameter, the action must declare a $args variable, such as: 1public function Actionindex ($limit =10, $args = Array ()) {...} $ args receives all available anonymous parameters. Global options refers to a command-line option that is shared by all actions of this command. For example: A command has several actions, and we want to have an option named verbose in each action, and we can declare a parameter called $verbose in each action method. A better approach would be to declare it as a public member variable of the command class (member variable) so that verbose becomes a global option. 1class Sitemapcommand extends Cconsolecommand2{3public $verbose =false;4public function Actionindex ($type) {. .} 5} This allows you to execute a command with the verbose option: 1YIIC sitemap index--verbose=1--TYPE=NEWS3, the host machine that exits the code execution command may need to detect the success of our command execution, It can be identified by the exit code that is returned by detecting the command last exit. The exit code is an integer value between 0 and 254, and 0 indicates that the command executed successfully, not 0 indicates that an error occurred during the execution of the command. You can exit your application by using an exit code inside the action or the Run method. For example: 1if (/* ERROR */) {2return   1; Exit with error code 13}4//... do something ... 5return 0; Exit successfully If there is no return value, there will be a default of 0 is returned 4, custom console App default console app configuration location: protected/config/console.php. The public properties of any cconsoleapplication can be configured in this file. This configuration file is similar to a normal Web application configuration file. customizing Console applications By default, if a application is created using the YIIC WebApp tool, the configuration For the console application would be protected/config/console.php. Like a Web application configuration file, this file was a PHP script which returns an array representing the property init Ial values for a console application instance. As a result, any public property of Cconsoleapplication can is configured in the this file.  Because console commands is often created to serve for the WEB application, they need to access the resources (such as DB connections) that is used by the latter.    We can do in the console application configuration file like the Following:return array (...).         ' Components ' =>array (' db ' =>array (   ......        ),    ),); As we can see, the format of the configuration was very similar to what we do in a WEB application configuration. This is because both Cconsoleapplication and cwebapplication share the same base class. Article Reference: HTTP://WWW.YIIFRAMEWORK.C om/doc/guide/1.1/zh_cn/ Topics.console----------------------------------------------------------------------------------------------------------- -----------article: Planning tasks for PHP programs using the YII Framework Tutorial 1. When you create a WebApp app from Yiic, yiic.php is generated under webapp/protected/because it's a command line app, So here the yiic.php is actually the same as the WebApp under the index.php, command line entry file. 2. Open the Yiic file, add a row of settings, add the path of the commands directory to YIIC, so that YIIC can find the commands directory command file, the modified code is as follows, red for the new Add code: 123456$YIIC =dirname (_ _FILE__). '/http://www.cnblogs.com/yii-read-only/framework/yiic.php '; $config =dirname (__file__). '/config/console.php '; @putenv (' yii_console_commands= '. DirName (__file__). '/commands '); require_once ($YIIC); or: Configured, the page to execute. This article is Protected/commands/crons.phprun ();? >3. Configure the components that Produ ct/config/console.php need to use, like database connections. Configure Main/console.php, set the import path, and the DB connection, which is similar to main.php. php//the configuration for YIIC console application.//any writable cconsoleapplication properties can be Configu Red Here.return Array (' basepath ' = dirname (__file__). Directory_separator.                ' ... ', ' name ' = ' My Console application ', ' import ' = = Array (' application.models.* ', ' application.components.* ', ' application.components.base.* ', ' Application.comp Onents.imgthumb.* ', ' application.models.form.* ', ' et cetera, import the desired class package '), ' components ' = = Array (//Main DB connection ' db ' = = Array (' ConnectionStr                        ing ' = ' mysql:host=localhost;dbname= database name ', ' emulateprepare ' = ' = True ',        ' username ' + ' database name ', ' Password ' + ' database password ', ' charset ' = ' utf8 ',                 ' Tableprefix ' = ' company_ ',//table prefix, ' log ' = = Array (                                        ' Class ' = ' clogrouter ', ' routes ' = = Array (Array (  ' Class ' = ' Cfilelogroute ', ' levels ' = ' Error, warning '))); 4. Inherit cconsolecom Mand writes its own command class, YII provides two ways to do it, if you perform a single task, write directly in the Run method, the other is to write your controller, add actionxxx. This example takes the second approach, which is to implement the program by adding the Actionxxx method to the Cconsolecommand class based on the Web program development.            We create a file in the commands directory to perform the task we are going to perform, for the name of testcommand.php. 4. Open your Linux command window and create an automatic task.        As for the Windows system, is the planning task (win system, can Google how to operate), the following only the Linux system. crontab-e# #然后输入1 * * * * PHP/specific address/protected/commands/crons.php Test >>/specific address/protected/commands/test.log# #上面命令说明  , execute the test task once every minute, save the log under Test.log, and the automation task is complete. Scheduled tasks under Windows: SCHTASKS/CREATE/SC minuTe/mo 1/tn "taskphp"/tr "php F:\xampp\htdocs\php\yiiblog2\protected\commands\crons.php cinsert insertdata" Delete Scheduled Tasks SC      Htasks/delete/tn "taskphp" executes the InsertData method in the Cinsert command every 1 minutes. Referenced by: http://www.cnlvzi.com/index.php/Index/article/id/124 http://www.yiibase.com/yii/218.html http://www. yiiframework.com/wiki/221/cronjobsyii/http://986866294.blog.163.com/blog/static/1651222522013571578115/

  

Yii Scheduled Tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.