PHP Command Line execution integrates pathinfo to simulate the scheduled task instance, command line pathinfo

Source: Internet
Author: User

PHP Command Line execution integrates pathinfo to simulate the scheduled task instance, command line pathinfo

In command line mode, different controllers are called Based on passing parameters. The Controller periodically executes the specified method according to the configuration.

Application. php

<? Phpclass Application {public static function main () {header ("content-type: text/html; charset = UTF-8"); self: register (); self :: commandLine (); self: pathInfo () ;}// automatically load the public static function loadClass ($ class) {$ class = str_replace ('\\','/', $ class); $ dir = str_replace ('\', '/', _ DIR _); $ class = $ dir. "/". $ class. ". php "; require_once $ class;} // public static function commandLine () {if (php_sapi_name () = "Cli") {$ _ SERVER ['path _ info'] = ""; foreach ($ _ SERVER ['argv'] as $ k => $ v) {if ($ k = 0) continue; $ _ SERVER ['path _ info']. = "/". $ v ;}}// pathinfo public static function pathInfo () {if (isset ($ _ SERVER ['path _ info']) {$ pathinfo = array_filter (explode ("/", $ _ SERVER ['path _ info']); for ($ I = 1; $ I <= count ($ pathinfo); $ I ++) {$ key = isset ($ pathinfo [$ I])? $ Pathinfo [$ I]: ''; $ value = isset ($ pathinfo [$ I + 1])? $ Pathinfo [$ I + 1]: ""; switch ($ I) {case 1: $ _ GET ['M'] = ucfirst ($ key); break; case 2: $ _ GET ['C'] = ucfirst ($ key); break; case 3: $ _ GET ['a'] = $ key; break; default: if ($ I> 3) {if ($ I % 2 = 0) {$ _ GET [$ key] = $ value ;}} break ;}}} $ _ GET ['M'] =! Empty ($ _ GET ['M'])? Ucfirst ($ _ GET ['M']): 'index'; $ _ GET ['C'] =! Empty ($ _ GET ['C'])? Ucfirst ($ _ GET ['C']): 'index'; $ _ GET ['a'] =! Empty ($ _ GET ['a'])? $ _ GET ['a']: 'index '; $ class = "\ Controller \\{$ _ GET ['M'] }\{$ _ GET ['C']}"; $ controller = new $ class; $ controller-> $ _ GET ['a'] ();} // fatal error callback public static function shutdownCallback () {$ e = error_get_last (); if (! $ E) return; self: errorHandler ($ e ['type'], '<font color = "red"> Fatal Error </font> '. $ e ['message'], $ e ['file'], $ e ['line']);} // error handling protected static function myErrorHandler ($ errno, $ errstr, $ errfile, $ errline) {list ($ micseconds, $ seconds) = explode ("", microtime (); $ micseconds = round ($ micseconds * 1000 ); $ micseconds = strlen ($ micseconds) = 1? '0 '. $ micseconds: $ micseconds; if (php_sapi_name () = "cli") {$ break = "\ r \ n ";} else {$ break = "<br/>" ;}$ mes = "[". date ("Y-m-d H: I: s", $ seconds ). ": {$ micseconds}]". $ errfile. "". $ errline. "line ". $ errstr. $ break; echo $ mes;} // register public static function register () {error_reporting (0); set_error_handler (function ($ errno, $ errstr, $ errfile, $ errline) {self: myErrorHandler ($ errno, $ errstr, $ errfile, $ errline) ;}); register_shutdown_function (function () {self: shutdownCallback ();}); spl_autoload_register ("self: loadClass") ;}} Application: main ();

\ Controller \ Client \ Cron. php

<?phpnamespace Controller\Client;class Cron{  private $second=0;  private $tasks=array(    array("duration"=>5,"method"=>"doSomething"),    array("duration"=>2,"method"=>"doSomething2"),  );  public function index(){    while (true) {      sleep(1);      $this->second++;      foreach($this->tasks as $task){        if($this->second%$task['duration']==0){          $this->$task['method']();        }      }    }  }  public function doSomething(){    echo "[".date("Y-m-d H:i:s",time())."] doSomething1 ok!\r\n";  }  public function doSomething2(){    echo "[".date("Y-m-d H:i:s",time())."] doSomething2 ok!\r\n";  }}

Effect:

Method doSomething is executed every 2 seconds.

Method doSomething2 is executed every 5 seconds.

Currently, other methods are synchronized. You can optimize them to open new threads to execute these methods, so that the timer of the main thread will not be blocked.

The above PHP Command Line execution integrated pathinfo simulation timing task instance is all the content shared by xiaobian to everyone, I hope to give you a reference, but also hope you can support a lot of help home.

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.