473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CakePHP query

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!
Sep 13 '09 #1
2 3968
Dormilich
8,658 Expert Mod 8TB
have you tried the code outside the cron job?
Sep 13 '09 #2
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.
Sep 19 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
0
by: PHPBABY3 | last post by:
Need Consultant to Help with CakePHP Hey, I know PHP but need to learn CakePHP right away. Where's the best documentation? Looking for a consultant to help via phone, email or chat. p h...
0
by: grezlik | last post by:
Hi! I have major problems with cakephp & smarty, (cake ver is 1.2 and smarty 2.6.18 ) there is a code (from cake/libs/view/view.php class SmartyView { .. .. ..
6
by: aadsaca | last post by:
Hi, I just want to know about how to make a program using CakePHP with EXTJS. Like displaying database contents with EXTJS' cool tables you know tnx, arjel
3
by: Animesh K | last post by:
Hello All: After having some experience with PHP, I am thinking to use one of the frameworks for my website. CakePHP looks promising (among others). Cake requires some .htacess settings and I...
3
by: jerrystar | last post by:
how to learn cakephp. where to get cakephp step by step tutorial
0
by: arunj82 | last post by:
Hi All, I am doing a project in cakephp. What my problem is when ever i close my browser and when i reopen it, session id(i.e userAgent) in cakephp is not changing. I want to use this session id...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.