2009-09-09 96 views
1

我在Cake PHP中做了一個招標網站。我面臨的主要問題是我需要在服務器上運行CRON JOBS。但我不知道它爲什麼會擾亂我。控制器稱爲'deamons',它有4種不同的動作,我想每分鐘都在服務器上連續運行,以便我們可以運行該出價站點的每個用戶設置的Autobidder。 的Cron作業我設立是... 捲曲-s -o的/ dev/null的http://www.domain.com/app/webroot/daemons/bidbutler
捲曲-s -o的/ dev/null的http://www.domain.com/app/webroot/daemons/extend 捲曲-s -o的/ dev/null的http://www.domain.com/app/webroot/daemons/autobid
捲曲 - s -o/dev/null http://www.domain.com/app/webroot/daemons/closeCron Jobs執行問題

和正在處理所有東西的控制器附在下面.... !!! 請建議我一些解決方案,這使

如果專家要測試it..the網址是www.domain.com/app/webroot

這裏是代碼...這我試圖穿過CRONS ...... !!!

<?php 

類DaemonsController擴展的AppController {

var $name = 'Daemons'; 

var $uses = array('Auction', 'Setting'); 

function beforeFilter(){ 
    $email='[email protected]'; 
    $secondemail='[email protected]'; 
    $mess='It works'; 
    //@mail($email, 'Test', $mess, "From: ".$secondemail); 

    parent::beforeFilter(); 

    if(!empty($this->Auth)) { 
     $this->Auth->allow('bidbutler', 'extend', 'autobid', 'close'); 
    } 
    ini_set('max_execution_time', ($this->appConfigurations['cronTime'] * 60) + 1); 
} 

/** 
* The function makes the bid butler magic happen 
* 
* @return array Affected Auction 
*/ 
function bidbutler() { 

    $this->layout = 'js/ajax'; 

    $data  = array(); 
    $setting = array(); 
    $auctions = array(); 

    // Get the bid butler time 
    $bidButlerTime = $this->Setting->get('bid_butler_time'); 

    // Get various settings needed 
    $data['bid_debit']    = $this->Setting->get('bid_debit'); 
    $data['auction_price_increment'] = $this->Setting->get('auction_price_increment'); 
    $data['auction_time_increment'] = $this->Setting->get('auction_time_increment'); 
    $data['auction_peak_start']  = $this->Setting->get('auction_peak_start'); 
    $data['auction_peak_end']  = $this->Setting->get('auction_peak_end'); 

    $expireTime = time() + ($this->appConfigurations['cronTime'] * 60); 

    while (time() < $expireTime) { 
     // Formating the conditions 
     $conditions = array(
      'Auction.end_time < \''. date('Y-m-d H:i:s', time() + $bidButlerTime). '\'', 
      'Auction.closed' => 0, 
      'Bidbutler.bids >' => 0 
     ); 

     // Find the bidbutler entry - we get them from the lowest price to the maximum price so that they all run! 
     $this->Auction->Bidbutler->contain('Auction'); 
     $bidbutlers = $this->Auction->Bidbutler->find('all', array('conditions' => $conditions, 'order' => 'rand()', 'fields' => array('Auction.id', 'Auction.start_price', 'Bidbutler.id', 'Bidbutler.minimum_price', 'Bidbutler.maximum_price', 'Bidbutler.user_id'), 'contain' => 'Auction')); 

     if(!empty($bidbutlers)) { 
      // Walk through bidbutler entries 
      foreach($bidbutlers as $bidbutler) { 
       if($bidbutler['Bidbutler']['minimum_price'] >= $bidbutler['Auction']['start_price'] && 
        $bidbutler['Bidbutler']['maximum_price'] < $bidbutler['Auction']['start_price']) { 

        // Add more information 
        $data['auction_id'] = $bidbutler['Auction']['id']; 
        $data['user_id'] = $bidbutler['Bidbutler']['user_id']; 
        $data['bid_butler'] = $bidbutler['Bidbutler']['id']; 

        // Bid the auction 
        $result = $this->Auction->bid($data); 
       } 
      } 
     } 
     usleep(900000); 
    } 
} 

/** 
* The function auto extends auctions and bids for an auto bid if neccessary 
* 
* @return array Affected Auction 
*/ 
function extend() { 
    $this->layout = 'js/ajax'; 

    $data  = array(); 
    $setting = array(); 
    $auctions = array(); 

    $data['bid_debit']    = $this->Setting->get('bid_debit'); 
    $data['auction_price_increment'] = $this->Setting->get('auction_price_increment'); 
    $data['auction_time_increment'] = $this->Setting->get('auction_time_increment'); 
    $data['auction_peak_start']  = $this->Setting->get('auction_peak_start'); 
    $data['auction_peak_end']  = $this->Setting->get('auction_peak_end'); 

    $data['isPeakNow'] = $this->isPeakNow(); 

    $expireTime = time() + ($this->appConfigurations['cronTime'] * 60); 

    while (time() < $expireTime) { 
     // now check for auto extends 
     $auctions = Cache::read('daemons_extend_auctions'); 
     if(empty($auctions)) { 
      $auctions = $this->Auction->find('all', array('contain' => '', 'conditions' => "(Auction.extend_enabled = 1 OR Auction.autobid = 1) AND (Auction.start_price < Auction.minimum_price) AND Auction.winner_id = 0 AND Auction.closed = 0")); 
      Cache::write('daemons_extend_auctions', $auctions, '+1 day'); 
     } 

     if(!empty($auctions)) { 
      foreach($auctions as $auction) { 
       // lets see if we need to extend the auction 
       $endTime = strtotime($auction['Auction']['end_time']); 
       $extendTime = time() + ($auction['Auction']['time_before_extend']); 

       if($extendTime > $endTime) { 
        // lets see if autobid is enabled 
        // autobid will place a bid by a robot if another user is the highest bidder but hasn't meet the minimum price 
        if($auction['Auction']['autobid'] == 1) { 
         if($auction['Auction']['extend_enabled'] == 1) { 
          // lets only bid if the limit is less than te autobid limit when the autobid limit is set 
          if($auction['Auction']['autobid_limit'] > 0) { 
           if($auction['Auction']['current_limit'] <= $auction['Auction']['autobid_limit']) { 
            $this->Auction->Autobid->check($auction['Auction']['id'], $auction['Auction']['end_time'], $data); 
           } 
          } else { 
           $this->Auction->Autobid->check($auction['Auction']['id'], $auction['Auction']['end_time'], $data); 
          } 
         } else { 
          $bid = $this->Auction->Bid->lastBid($auction['Auction']['id']); 
          // lets set the autobid 
          if(!empty($bid) && ($bid['autobidder'] == 0)) { 
           $this->Auction->Autobid->check($auction['Auction']['id'], $auction['Auction']['end_time'], $data); 
          } 
         } 
        } elseif($auction['Auction']['extend_enabled'] == 1) { 
         unset($auction['Auction']['modified']); 
         $auction['Auction']['end_time'] = date('Y-m-d H:i:s', $endTime + ($auction['Auction']['time_extended'])); 

         // lets do a quick check to make sure the new end time isn't less than the current time 
         $newEndTime = strtotime($auction['Auction']['end_time']); 
         if($newEndTime < time()) { 
          $auction['Auction']['end_time'] = date('Y-m-d H:i:s', time() + ($auction['Auction']['time_extended'])); 
         } 

         $this->Auction->save($auction); 
        } 
       } 
      } 
     } 
     usleep(800000); 
    } 
} 

/** 
* The function auto extends auctions in the last IF the extend function fails 
* 
* @return array Affected Auction 
*/ 
function autobid() { 
    $data['bid_debit']    = $this->Setting->get('bid_debit'); 
    $data['auction_time_increment'] = $this->Setting->get('auction_time_increment'); 
    $data['auction_price_increment'] = $this->Setting->get('auction_price_increment'); 
    $data['auction_peak_start']  = $this->Setting->get('auction_peak_start'); 
    $data['auction_peak_end']  = $this->Setting->get('auction_peak_end'); 
    $data['isPeakNow']    = $this->isPeakNow(); 
    $isPeakNow = $this->isPeakNow(); 

    $expireTime = time() + ($this->appConfigurations['cronTime'] * 60); 

    while (time() < $expireTime) { 
     // lets start by getting all the auctions that have closed 
     $auctions = $this->Auction->find('all', array('fields' => array('Auction.id', 'Auction.peak_only'), 'contain' => '', 'conditions' => "Auction.winner_id = 0 AND Auction.end_time <= '" . date('Y-m-d H:i:s', time() + 4) . "' AND Auction.closed = 0")); 

     if(!empty($auctions)) { 
      foreach($auctions as $auction) { 
       // before we declare this user the winner, lets run some test to make sure the auction can definitely close 
       if($this->Auction->checkCanClose($auction['Auction']['id'], $isPeakNow, false) == false) { 
        // lets check to see if the reason we can't close it, is because its now offpeak and this is a peak auction 
        if($auction['Auction']['peak_only'] == 1 && !$isPeakNow) { 
         continue; 
        } else { 
         $this->Auction->Autobid->placeAutobid($auction['Auction']['id'], $data); 
        } 
       } 
      } 
     } 
     usleep(900000); 
    } 
} 

/** 
* The function closes the auctions 
* 
* @return array Affected Auction 
*/ 
function close() { 
    $expireTime = time() + ($this->appConfigurations['cronTime'] * 60); 

    while (time() < $expireTime) { 
     // lets start by getting all the auctions that have closed 
     $auctions = $this->Auction->find('all', array('contain' => '', 'conditions' => "Auction.winner_id = 0 AND Auction.end_time <= '" . date('Y-m-d H:i:s') . "' AND Auction.closed = 0")); 

     if(!empty($auctions)) { 
      foreach($auctions as $auction) { 
       $isPeakNow = $this->isPeakNow(); 

       // before we declare this user the winner, lets run some test to make sure the auction can definitely close 
       if($this->Auction->checkCanClose($auction['Auction']['id'], $isPeakNow) == false) { 
        // lets check to see if the reason we can't close it, is because its now offpeak and this is a peak auction 
        if($auction['Auction']['peak_only'] == 1 && !$isPeakNow) { 
         $peak = $this->nonPeakDates(); 

         //Calculate how many seconds auction will end after peak end 
         $seconds_after_peak = strtotime($auction['Auction']['end_time']) - strtotime($peak['peak_end']); 
         $end_time = strtotime($peak['peak_start']) + $seconds_after_peak; 

         $auction['Auction']['end_time'] = date('Y-m-d H:i:s', $end_time); 
         $this->Auction->save($auction); 

        } else { 
         // lets check just how far ago this auction closed, and either place an autobid or extend the time 
         $data['auction_time_increment'] = $this->Setting->get('auction_time_increment'); 

         $newEndTime = strtotime($auction['Auction']['end_time']); 
         if($newEndTime < time() - $data['auction_time_increment']) { 
          $auction['Auction']['end_time'] = date('Y-m-d H:i:s', time() + ($auction['Auction']['time_extended'])); 
          $this->Auction->save($auction); 
         } else { 
          //lets extend it by placing an autobid 
          $data['bid_debit']    = $this->Setting->get('bid_debit'); 
          $data['auction_price_increment'] = $this->Setting->get('auction_price_increment'); 
          $data['auction_peak_start']  = $this->Setting->get('auction_peak_start'); 
          $data['auction_peak_end']  = $this->Setting->get('auction_peak_end'); 
          $data['isPeakNow']    = $this->isPeakNow(); 

          $this->Auction->Autobid->placeAutobid($auction['Auction']['id'], $data); 
         } 
        } 
        continue; 
       } 

       $bid = $this->Auction->Bid->find('first', array('conditions' => array('Bid.auction_id' => $auction['Auction']['id']), 'order' => array('Bid.id' => 'desc'))); 
       if(!empty($bid)) { 
        if($bid['User']['autobidder'] == 0) { 
         // send the email to the winner 
         $data['Auction']    = $auction['Auction']; 
         $data['Bid']     = $bid['Bid']; 
         $data['User']     = $bid['User']; 
         $data['to']     = $data['User']['email']; 
         $data['subject']    = sprintf(__('%s - You have won an auction', true), $this->appConfigurations['name']); 
         $data['template']    = 'auctions/won_auction'; 
         $this->_sendEmail($data); 

         $auction['Auction']['status_id'] = 1; 
        } 

        $auction['Auction']['winner_id'] = $bid['Bid']['user_id']; 
       } 
       unset($auction['Auction']['modified']); 
       $auction['Auction']['closed'] = 1; 
       $this->Auction->save($auction); 
      } 
     } 
     usleep(900000); 
    } 
} 

} ?>

+0

究竟是什麼問題? 發佈你的'crontab -l' 你是否已經進入了curl可執行文件的絕對路徑? ('curl') – astropanic 2009-09-09 08:38:12

+0

你可能需要解釋你有什麼問題,除了「它煩你」之外。 cron作業沒有運行?沒有以正確的頻率運行?代碼不起作用? – 2009-09-09 08:44:58

+0

以及我已經嘗試在服務器上設置Crons。但顯然它正在運行......這是主要問題。如果您的老年人無法理解這種簡單的語言,那麼它不是我的錯...... !!! 現在,如果有人有答案,請幫助我。 – 2009-09-09 20:12:59

回答

2

CakePHP的方式來運行crons是建立自己的殼完成的任務。 Shell允許您通過命令提示符完全訪問所有控制器。請務必閱讀本文檔開始時:

http://book.cakephp.org/view/108/The-CakePHP-Console

它表明你如何建立自己的殼(應用程序/供應商/殼/),如何組織你彈入任務,以及如何正確地運行shell作爲cron作業。

我這樣做與文檔描述略有不同。我的cron語句是這樣的:

* * * * * (cd /path/to/my/cake/app; sh ../cake/console/cake daily;) 1> /dev/null 2>&1 

從那裏,我只是有一個稱爲殼應用/供應商/殼/ daily.php

<?php 
class DailyShell extends Shell { 

    var $uses = array('User'); 

     function main() { 

       $this->User->processDailyTasks(); 

     } 
} 
?> 

這比在cron使用curl更好,更穩定工作。

+0

這不起作用,先生。我已經完成了你以上所述的工作。 – 2009-09-10 19:23:56