Connecting Tech Pros Worldwide Forums | Help | Site Map

CakePHP query

Newbie
 
Join Date: May 2009
Posts: 2
#1: Sep 13 '09
Hi all,

I am using CakePHP to develop a bidding site. I have created a controller called 'daemons' and it performs 4 different tasks which I need to run continuously on the server every minute. I have set this up as 4 cron jobs but something is not working right and my crons just fail to work!

Could be a problem with the controller, just not able to place it. My controller code is pasted below:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class DaemonsController extends AppController {
  3.  
  4.     var $name = 'Daemons';
  5.  
  6.     var $uses = array('Auction', 'Setting');
  7.  
  8.     function beforeFilter(){
  9.         $email='abc@gmail.com';
  10.         $secondemail='no-reply@xyz.com';
  11.         $mess='It works';
  12.         //@mail($email, 'Test', $mess, "From: ".$secondemail);
  13.  
  14.         parent::beforeFilter();
  15.  
  16.         if(!empty($this->Auth)) {
  17.             $this->Auth->allow('bidbutler', 'extend', 'autobid', 'close');
  18.         }
  19.         ini_set('max_execution_time', ($this->appConfigurations['cronTime'] * 60) + 1);
  20.     }
  21.  
  22.     /**
  23.      * The function makes the bid butler magic happen
  24.      *
  25.      * @return array Affected Auction
  26.      */
  27.     function bidbutler() {
  28.  
  29.         $this->layout = 'js/ajax';
  30.  
  31.         $data       = array();
  32.         $setting  = array();
  33.         $auctions = array();
  34.  
  35.         // Get the bid butler time
  36.         $bidButlerTime = $this->Setting->get('bid_butler_time');
  37.  
  38.         // Get various settings needed
  39.         $data['bid_debit']                  = $this->Setting->get('bid_debit');
  40.         $data['auction_price_increment'] = $this->Setting->get('auction_price_increment');
  41.         $data['auction_time_increment']  = $this->Setting->get('auction_time_increment');
  42.         $data['auction_peak_start']         = $this->Setting->get('auction_peak_start');
  43.         $data['auction_peak_end']          = $this->Setting->get('auction_peak_end');
  44.  
  45.         $expireTime = time() + ($this->appConfigurations['cronTime'] * 60);
  46.  
  47.         while (time() < $expireTime) {
  48.             // Formating the conditions
  49.             $conditions = array(
  50.                 'Auction.end_time < \''. date('Y-m-d H:i:s', time() + $bidButlerTime). '\'',
  51.                 'Auction.closed' => 0,
  52.                 'Bidbutler.bids >' => 0
  53.             );
  54.  
  55.             // Find the bidbutler entry - we get them from the lowest price to the maximum price so that they all run!
  56.             $this->Auction->Bidbutler->contain('Auction');
  57.             $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'));
  58.  
  59.             if(!empty($bidbutlers)) {
  60.                 // Walk through bidbutler entries
  61.                 foreach($bidbutlers as $bidbutler) {
  62.                     if($bidbutler['Bidbutler']['minimum_price'] >= $bidbutler['Auction']['start_price'] &&
  63.                        $bidbutler['Bidbutler']['maximum_price'] < $bidbutler['Auction']['start_price']) {
  64.  
  65.                         // Add more information
  66.                         $data['auction_id']    = $bidbutler['Auction']['id'];
  67.                         $data['user_id']    = $bidbutler['Bidbutler']['user_id'];
  68.                         $data['bid_butler']    = $bidbutler['Bidbutler']['id'];
  69.  
  70.                         // Bid the auction
  71.                         $result = $this->Auction->bid($data);
  72.                     }
  73.                 }
  74.             }
  75.             usleep(900000);
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * The function auto extends auctions and bids for an auto bid if neccessary
  81.      *
  82.      * @return array Affected Auction
  83.      */
  84.     function extend() {
  85.         $this->layout = 'js/ajax';
  86.  
  87.         $data       = array();
  88.         $setting  = array();
  89.         $auctions = array();
  90.  
  91.         $data['bid_debit']                  = $this->Setting->get('bid_debit');
  92.         $data['auction_price_increment'] = $this->Setting->get('auction_price_increment');
  93.         $data['auction_time_increment']  = $this->Setting->get('auction_time_increment');
  94.         $data['auction_peak_start']      = $this->Setting->get('auction_peak_start');
  95.         $data['auction_peak_end']          = $this->Setting->get('auction_peak_end');
  96.  
  97.         $data['isPeakNow']  = $this->isPeakNow();
  98.  
  99.         $expireTime = time() + ($this->appConfigurations['cronTime'] * 60);
  100.  
  101.         while (time() < $expireTime) {
  102.             // now check for auto extends
  103.             $auctions = Cache::read('daemons_extend_auctions');
  104.             if(empty($auctions)) {
  105.                 $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"));
  106.                 Cache::write('daemons_extend_auctions', $auctions, '+1 day');
  107.             }
  108.  
  109.             if(!empty($auctions)) {
  110.                 foreach($auctions as $auction) {
  111.                     // lets see if we need to extend the auction
  112.                     $endTime = strtotime($auction['Auction']['end_time']);
  113.                     $extendTime = time() + ($auction['Auction']['time_before_extend']);
  114.  
  115.                     if($extendTime > $endTime) {
  116.                         // lets see if autobid is enabled
  117.                         // autobid will place a bid by a robot if another user is the highest bidder but hasn't meet the minimum price
  118.                         if($auction['Auction']['autobid'] == 1) {
  119.                             if($auction['Auction']['extend_enabled'] == 1) {
  120.                                 // lets only bid if the limit is less than te autobid limit when the autobid limit is set
  121.                                 if($auction['Auction']['autobid_limit'] > 0) {
  122.                                     if($auction['Auction']['current_limit'] <= $auction['Auction']['autobid_limit']) {
  123.                                         $this->Auction->Autobid->check($auction['Auction']['id'], $auction['Auction']['end_time'], $data);
  124.                                     }
  125.                                 } else {
  126.                                     $this->Auction->Autobid->check($auction['Auction']['id'], $auction['Auction']['end_time'], $data);
  127.                                 }
  128.                             } else {
  129.                                 $bid = $this->Auction->Bid->lastBid($auction['Auction']['id']);
  130.                                 // lets set the autobid
  131.                                 if(!empty($bid) && ($bid['autobidder'] == 0)) {
  132.                                     $this->Auction->Autobid->check($auction['Auction']['id'], $auction['Auction']['end_time'], $data);
  133.                                 }
  134.                             }
  135.                         } elseif($auction['Auction']['extend_enabled'] == 1) {
  136.                             unset($auction['Auction']['modified']);
  137.                             $auction['Auction']['end_time'] = date('Y-m-d H:i:s', $endTime + ($auction['Auction']['time_extended']));
  138.  
  139.                             // lets do a quick check to make sure the new end time isn't less than the current time
  140.                             $newEndTime = strtotime($auction['Auction']['end_time']);
  141.                             if($newEndTime < time()) {
  142.                                 $auction['Auction']['end_time'] = date('Y-m-d H:i:s', time() + ($auction['Auction']['time_extended']));
  143.                             }
  144.  
  145.                             $this->Auction->save($auction);
  146.                         }
  147.                     }
  148.                 }
  149.             }
  150.             usleep(800000);
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * The function auto extends auctions in the last IF the extend function fails
  156.      *
  157.      * @return array Affected Auction
  158.      */
  159.     function autobid() {
  160.         $data['bid_debit']                   = $this->Setting->get('bid_debit');
  161.         $data['auction_time_increment']  = $this->Setting->get('auction_time_increment');
  162.         $data['auction_price_increment'] = $this->Setting->get('auction_price_increment');
  163.         $data['auction_peak_start']      = $this->Setting->get('auction_peak_start');
  164.         $data['auction_peak_end']          = $this->Setting->get('auction_peak_end');
  165.         $data['isPeakNow']               = $this->isPeakNow();
  166.         $isPeakNow = $this->isPeakNow();
  167.  
  168.         $expireTime = time() + ($this->appConfigurations['cronTime'] * 60);
  169.  
  170.         while (time() < $expireTime) {
  171.             // lets start by getting all the auctions that have closed
  172.             $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"));
  173.  
  174.             if(!empty($auctions)) {
  175.                 foreach($auctions as $auction) {
  176.                     // before we declare this user the winner, lets run some test to make sure the auction can definitely close
  177.                     if($this->Auction->checkCanClose($auction['Auction']['id'], $isPeakNow, false) == false) {
  178.                         // lets check to see if the reason we can't close it, is because its now offpeak and this is a peak auction
  179.                         if($auction['Auction']['peak_only'] == 1 && !$isPeakNow) {
  180.                             continue;
  181.                         } else {
  182.                             $this->Auction->Autobid->placeAutobid($auction['Auction']['id'], $data);
  183.                         }
  184.                     }
  185.                 }
  186.             }
  187.             usleep(900000);
  188.         }
  189.     }
  190.  
  191.     /**
  192.      * The function closes the auctions
  193.      *
  194.      * @return array Affected Auction
  195.      */
  196.     function close() {
  197.         $expireTime = time() + ($this->appConfigurations['cronTime'] * 60);
  198.  
  199.         while (time() < $expireTime) {
  200.             // lets start by getting all the auctions that have closed
  201.             $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"));
  202.  
  203.             if(!empty($auctions)) {
  204.                 foreach($auctions as $auction) {
  205.                     $isPeakNow = $this->isPeakNow();
  206.  
  207.                     // before we declare this user the winner, lets run some test to make sure the auction can definitely close
  208.                     if($this->Auction->checkCanClose($auction['Auction']['id'], $isPeakNow) == false) {
  209.                         // lets check to see if the reason we can't close it, is because its now offpeak and this is a peak auction
  210.                         if($auction['Auction']['peak_only'] == 1 && !$isPeakNow) {
  211.                             $peak = $this->nonPeakDates();
  212.  
  213.                             //Calculate how many seconds auction will end after peak end
  214.                             $seconds_after_peak = strtotime($auction['Auction']['end_time']) - strtotime($peak['peak_end']);
  215.                             $end_time = strtotime($peak['peak_start']) + $seconds_after_peak;
  216.  
  217.                             $auction['Auction']['end_time'] = date('Y-m-d H:i:s', $end_time);
  218.                             $this->Auction->save($auction);
  219.  
  220.                         } else {
  221.                             // lets check just how far ago this auction closed, and either place an autobid or extend the time
  222.                             $data['auction_time_increment']  = $this->Setting->get('auction_time_increment');
  223.  
  224.                             $newEndTime = strtotime($auction['Auction']['end_time']);
  225.                             if($newEndTime < time() - $data['auction_time_increment']) {
  226.                                 $auction['Auction']['end_time'] = date('Y-m-d H:i:s', time() + ($auction['Auction']['time_extended']));
  227.                                 $this->Auction->save($auction);
  228.                             } else {
  229.                                 //lets extend it by placing an autobid
  230.                                 $data['bid_debit']                  = $this->Setting->get('bid_debit');
  231.                                 $data['auction_price_increment'] = $this->Setting->get('auction_price_increment');
  232.                                 $data['auction_peak_start']      = $this->Setting->get('auction_peak_start');
  233.                                 $data['auction_peak_end']          = $this->Setting->get('auction_peak_end');
  234.                                 $data['isPeakNow']               = $this->isPeakNow();
  235.  
  236.                                 $this->Auction->Autobid->placeAutobid($auction['Auction']['id'], $data);
  237.                             }
  238.                         }
  239.                         continue;
  240.                     }
  241.  
  242.                     $bid = $this->Auction->Bid->find('first', array('conditions' => array('Bid.auction_id' => $auction['Auction']['id']), 'order' => array('Bid.id' => 'desc')));
  243.                     if(!empty($bid)) {
  244.                         if($bid['User']['autobidder'] == 0) {
  245.                             // send the email to the winner
  246.                             $data['Auction']                = $auction['Auction'];
  247.                             $data['Bid']                    = $bid['Bid'];
  248.                             $data['User']                    = $bid['User'];
  249.                             $data['to']                    = $data['User']['email'];
  250.                             $data['subject']                = sprintf(__('%s - You have won an auction', true), $this->appConfigurations['name']);
  251.                             $data['template']                = 'auctions/won_auction';
  252.                             $this->_sendEmail($data);
  253.  
  254.                             $auction['Auction']['status_id'] = 1;
  255.                         }
  256.  
  257.                         $auction['Auction']['winner_id'] = $bid['Bid']['user_id'];
  258.                     }
  259.                     unset($auction['Auction']['modified']);
  260.                     $auction['Auction']['closed'] = 1;
  261.                     $this->Auction->save($auction);
  262.                 }
  263.             }
  264.             usleep(900000);
  265.         }
  266.     }
  267. }
  268. ?>
Anyone notice any bugs in this..?

Thanks!

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#2: Sep 13 '09

re: CakePHP query


have you tried the code outside the cron job?
Newbie
 
Join Date: May 2009
Posts: 2
#3: Sep 19 '09

re: CakePHP query


Thanks for that reply Dormilich and sorry for responding late.

And yes, I did try the code outside the cron job, it would work.
Checked the whole function by placing a mail function in it. Mailing from the beforeFilter() function apparently worked. Seems there is some other bug in the controller which wont allow the crons to place an autobid.

Still not able to figure this out though.
Reply

Tags
cakephp


Similar PHP bytes