跳到主要內容

Run a Long-Time Script with Self Modification Detection

Since PHP is convenient tool to process text contents and data from internet, sometimes we use PHP to process a large amount of data as batch script program, and running for a long time. But we may want to change the process rules from PHP script without restarting the program manually. In this case a self modification detection is very useful.

class ScriptUpdater{
  private static $scripts;
  private static $md5;
  
  //add a script file to watch the modification, filePath can be string or array
  public static function addScript($filePath){
   if(is_array($filePath)) return self::addScript(array_pop($filePath)) && self::addScript($filePath);
   return ($md5 = @md5_file($filePath)) && (self::$scripts[$filePath] = $md5);
  }
  
  //reutrn the modified file or false, (if modified excutes a function)
  public static function update($onUpdateFunc = null, $args = null){
   if($onUpdateFunc) return self::_watch() && @call_user_func_array($onUpdateFunc, $args);
   else return self::_watch();
  }
  //reutrn the modified file or false 
  private static function _watch(){
   foreach(self::$scripts as $file => $md5) 
    if(@md5_file($file) != $md5) return $file;
   return false;
  }
 }


Example Usage

//put this part at the beginning of main program
 ScriptUpdater::addScript(array(
  __FILE__, //watch the current main program
  get_included_files(), //and all required files
 ));

 //while(...){

  //Put this part when necessary, usually in the beginning of a loop 
  if($file = ScriptUpdater::update()){
   echo " -- $file modified, exiting...\n";
   exit();
  }
  
  //...
  //Process something here

 // } end of loop


Another way to deal updating

//calling the die function
 ScriptUpdater::update('die', array('PHP file has been modified, run this program again.'));


Finally, there is another problem: After terminated, how would make PHP restart automatically?
I suggest a solution by using DOS batch file, running a endless loop like this:

@echo off
 chcp 65001
 cls

 IF NOT EXIST MyProgram.php GOTO Skip
 echo Starting MyProgram at %TIME%

 for /l %%i in (1 1 10000) DO  (
  php MyProgram.php
  echo MyProgram stopped
 )
 pause

 :Skip
 echo MyProgram.php is not exists
 pause

留言

這個網誌中的熱門文章

卡馬警衛 - 使用手冊

(核心架構 2.0,持續開發中) 卡馬警衛 x 掰噗   掰噗是在噗浪上和陪伴卡馬警衛工作的好夥伴 卡馬警衛截自此手冊更新前提供以下服務 自動回應 互動聊天 當日星座運勢查詢 地區天氣預報查詢 颱風最新動態 即時地震報告 當期發票號碼查詢 台鐵時刻與票價查詢 台灣電視節目查詢 咕狗翻譯 Youtube 影片/音樂 點播 以及影片資訊轉載功能 噗浪狗仔搜尋引擎 (beta) ※  請不要濫用服務資源,留給網友更多的福利  ※

Plurk Box Layout CSS

擁擠的版面掰掰~ 噗文盒子 body .block_cnt .plurk.cboxAnchor .plurk_cnt{overflow:hidden} body .block_cnt .plurk.cboxAnchor .plurk_cnt table{margin-bottom:20px;position:relative;top:20px} body .block_cnt .plurk.cboxAnchor .plurk_cnt table .td_qual{position:absolute;top:-20px;background:#efefef;width:100%} body .block_cnt .plurk.cboxAnchor .plurk_cnt table .td_cnt{padding:5px 12px} body .block_cnt .td_response_count{z-index:999;position:relative} 留言區塊 body #form_holder .plurk .plurk_cnt .td_qual{position:absolute;top:0} body #form_holder .plurk .plurk_cnt .td_cnt{padding:15px 10px 0} body #form_holder .qualifier{opacity:0.5} body #form_holder .text_holder{color:#222} 噗主回覆底色 body #form_holder .plurk.highlight_owner{background-color:#faecdf} body #form_holder .plurk_cnt{background:none!important} How To...

各種的點餅乾遊戲 クッキークリッカー

圈內似乎曾流行某款令人上癮的 點餅乾遊戲 阿罵表示 Cookies Crush!! 自製噗浪背景(CSS下載) まどマギクリッカー 點小圓手辦遊戲:出自飲料送的 小圓手辦 艦くり ぎょに☆クリッカー 魔理沙とアリスのクッキー☆Clicker