I am making an auction website, which has an auto-bid system. This system lets people make biddings without having to be there.
My question is how to implement such a system. I have made the php files and everything is ready, I’ve made a html page which refreshes every second. It works, but I’m wondering if there’s a better solution.
The second option I tried was making an java application which opens the webpage every second. This consumes “a lot” of memory/CPU. Also, I think there is a memory leak somewhere.
I am running this application on a dedicated linux server with centOS and Plesk, so I am sure there are a lot of alternatives.
For example:
-
A cronjob that executes a php file every minute, where the php file loops for a minute with sleeps.
-
One php file that loops with 1 second sleeps.
-
One cronjob that executes and schedules another cronjob after a second.
-
MySQL scheduler, but I don’t know how to implement that, and I heard it locks the db for other writes.
-
I also heard about cli, perl, python scripts, but I am not familiar with any of them.
Please can someone with any knowledge of these options please shed some light for me on this subject. I am searching for a solution that is best suited in terms of speed and memory usage.
If someone has another alternative solution I would be glad to hear that also.
4
Answers
Create a cron job that executes the PHP script in question at a given time interval. You can execute the PHP script from the command line if it’s written in a way that is context agnostic (doesn’t require access to
$_GET
,$_POST
and similar).If you already have a page which does the job, you can schedule a cron that calls a curl command every minute (curl http://your/page.php). The only caveat is that you could need to protect your page if it gets called from outside, should that be a problem.
Every one second is such an small interval that cronjobs are a not an option, also i would strongly suggest against using a “webpage” as is, since performance is important just a script written for the CLI will do it.
What i would do is:
you should register that in a
database.
database for entries to be executed
at that moment.
Whoever, you need to be very careful of making the script sleep after each cycle or you will bring down your database.
On a side note, you really need it to be executed every second? In my mind auto-bids are triggered by events, i.e. someone bidding more than you while there’s 10 seconds left. That sound a lot nicer to implement and less resource intensive (and you can also use a daemon there, listening for auto-bids events, but that’s another story 🙂
Well, you could try some sort of “event-based publishing”, which basically means that once the data has been inserted into the database, it triggers something to happen.
Case in point, MySQL triggers or even a simple php script that ON INSERT for a new bid, you check if anyone else has placed maximum bid that is higher then that bid.
If they did, then update the new highest bid and the current top bidder.
If there are a number of bidders, you might want to do a loop and see who will get the highest bid.
The point is that you shouldn’t really run around and do scheduled jobs to figure out this business problem. I recommend you use triggers or events.