background script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scion
Forum Newbie
Posts: 5
Joined: Mon Jan 05, 2015 3:05 pm

background script

Post by scion »

Hello! First of all, sorry for my poor english

So question is:

I develope penny auction, from front-end I pool every second request for fresh data about auctions on the page.

And then i realized that what if nobody open site page, how this all should work? Maybe I need some sort of background script

which will be handle all active auctions - close lots, open lots, set auto bids if they are and so on.

Next what I am think about - write some script do that job and run it with cron job.

But

1)Cron job running minimum only one per minute. So what if I need to open auction (or smth similar action) somewhere between two minutes?

2)And next that i thinking of - that script will not run 1 minute exactly, mb more mb less and this causing that another cron script coming into play! That is very bad!

So what you can advice me in this situation? What would be the better choice using poor PHP + JQuery(Ajax) + MySQL? Asking just about approach and your experience.

Many thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: background script

Post by requinix »

The cronjob can handle cleanup work for when an auction ends, and running every minute is enough for that. For example, it can update database information or send emails.

The rest of your code needs to be aware of when the auction is running or when it has expired, and that can be as simple as "is the current time before or after the auction expiration time?" When someone views a page, it checks if the auction should be over or not and displays the page accordingly. When someone tries to bid, it checks if the auction is still running before allowing the bid.

By the way, an automatic bid (there should only ever be one active bid in effect) is handled when someone else tries to bid - not with a cronjob:
- If the new bid is less than the automatic bid cap then the new price is the new bid + the minimum increment (or whatever your policy is)
- If the new bid is higher than the cap then the new price is the new bid and the automatic bid stops

You should also learn about concurrency and transactions. They help you solve the problem of what happens when two things happen at the same time, like two people bidding at once.
scion
Forum Newbie
Posts: 5
Joined: Mon Jan 05, 2015 3:05 pm

Re: background script

Post by scion »

requinix wrote:The cronjob can handle cleanup work for when an auction ends, and running every minute is enough for that. For example, it can update database information or send emails.

The rest of your code needs to be aware of when the auction is running or when it has expired, and that can be as simple as "is the current time before or after the auction expiration time?" When someone views a page, it checks if the auction should be over or not and displays the page accordingly. When someone tries to bid, it checks if the auction is still running before allowing the bid.

By the way, an automatic bid (there should only ever be one active bid in effect) is handled when someone else tries to bid - not with a cronjob:
- If the new bid is less than the automatic bid cap then the new price is the new bid + the minimum increment (or whatever your policy is)
- If the new bid is higher than the cap then the new price is the new bid and the automatic bid stops

You should also learn about concurrency and transactions. They help you solve the problem of what happens when two things happen at the same time, like two people bidding at once.

Many thanks for answer! So the point of auto bids that they should sets even when nobody is present on the page with current auction. I can set autobids for many auctions and just turn off

computer - they must work!

What about cron - if cron start auctions, handle autobids, send emails and so on, next minute another cron script will be started!

After all, thank you very much!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: background script

Post by requinix »

scion wrote:Many thanks for answer! So the point of auto bids that they should sets even when nobody is present on the page with current auction. I can set autobids for many auctions and just turn off

computer - they must work!
Right, but it won't matter until someone (else) tries to bid. So that's when you handle it.
scion wrote:What about cron - if cron start auctions, handle autobids, send emails and so on, next minute another cron script will be started!
- Cron wouldn't start an auction. Either the auction starts immediately, or you simply use another date field for a start date and the code checks both that and the end date when deciding what to do.
- If you did need something to happen when the auction starts, like sending an email or whatever, then you can use cron for that part specifically. But not for actually starting the auction. As in you don't have to go into the database to mark the auction as running or not - date fields tell you that information.
- Autobids, like I said, don't need cron. They only matter when a user is performing an action (ie, bidding) so you can handle them at that time.
- Emails, yes.

The cronjob should handle specific actions. It doesn't scan the database looking for something to do but rather queries the database for an action to perform.
When you implement it that way, the script gets the next pending action to perform from the database, marks it as in progress, does it, and marks it as completed. Using transactions. That way even if another script starts up, the previous script's action is "in progress" so the new one won't try to do it.

Code: Select all

id | state       | pid  | address           | data...
---+-------------+------+-------------------+--------
 1  | completed   | 1234 | alice@example.com |
 2  | completed   | 1234 | bob@example.com   |
 3  | in progress | 1234 | cindy@example.com |
 4  | in progress | 1235 | david@example.com |
 5  | pending     |      | evan@example.com  |
This table shows five emailing actions:
- The first two were completed by one script
- The third is in progress by that same script
- The fourth is being handled by a new script that started
- The last one hasn't been started yet
When script 1234 or 1235 finishes its current email and tries to grab a new action, it will get #5. It doesn't matter which. Then when either script tries to get the next action, it won't find anything and can quit.
scion
Forum Newbie
Posts: 5
Joined: Mon Jan 05, 2015 3:05 pm

Re: background script

Post by scion »

Thanks for idea. About autobids - they must work background because if auction has few players that make they auto bids on the same auction, then they bids must sets randomly until they ends.

Very greateful for your help
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: background script

Post by requinix »

scion wrote:About autobids - they must work background because if auction has few players that make they auto bids on the same auction, then they bids must sets randomly until they ends.
Maybe you're doing autobids differently than I'm used to?

Say the minimum bid increment is $1.
Alice bids $10 normally. Bob bids $11 normally. Alice sets up an autobid at $20 and it starts immediately with $12. Bob bids $13, but at that moment the autobid kicks in and raises it to $14. He tries again at $15 but the autobid raises it to $16.
Now Bob sets an autobid at $20. Alice's autobid was first so it goes to $20 and Bob's autobid ends. He sets up a new autobid for $25 and it starts at $21.
Alice comes back, sets an autobid for $30. Bob's autobid goes to $25, then Alice's bid goes to $26.

During that entire exchange, only one autobid was ever "active": the one with the highest maximum. When someone tries to set up a new one, the higher autobid wins and continues running while the lower autobid ends. And all that work only happens when a user takes an action, either a manual bid or setting up an autobid.

So. What kind of autobidding system are you thinking of?
scion
Forum Newbie
Posts: 5
Joined: Mon Jan 05, 2015 3:05 pm

Re: background script

Post by scion »

requinix wrote:
scion wrote:About autobids - they must work background because if auction has few players that make they auto bids on the same auction, then they bids must sets randomly until they ends.
Maybe you're doing autobids differently than I'm used to?

Say the minimum bid increment is $1.
Alice bids $10 normally. Bob bids $11 normally. Alice sets up an autobid at $20 and it starts immediately with $12. Bob bids $13, but at that moment the autobid kicks in and raises it to $14. He tries again at $15 but the autobid raises it to $16.
Now Bob sets an autobid at $20. Alice's autobid was first so it goes to $20 and Bob's autobid ends. He sets up a new autobid for $25 and it starts at $21.
Alice comes back, sets an autobid for $30. Bob's autobid goes to $25, then Alice's bid goes to $26.

During that entire exchange, only one autobid was ever "active": the one with the highest maximum. When someone tries to set up a new one, the higher autobid wins and continues running while the lower autobid ends. And all that work only happens when a user takes an action, either a manual bid or setting up an autobid.

So. What kind of autobidding system are you thinking of?

Yes, almost like you write, but autobids must working when Bob and Alice when they both turn off computer or go to sleep.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: background script

Post by Christopher »

scion wrote:Yes, almost like you write, but autobids must working when Bob and Alice when they both turn off computer or go to sleep.
It seems like once a second autobid is created, you just automatically take them to their max bid in the order they were registered. Why go through every step? It will just get to the highest max anyway...
(#10850)
scion
Forum Newbie
Posts: 5
Joined: Mon Jan 05, 2015 3:05 pm

Re: background script

Post by scion »

Christopher wrote:
scion wrote:Yes, almost like you write, but autobids must working when Bob and Alice when they both turn off computer or go to sleep.
It seems like once a second autobid is created, you just automatically take them to their max bid in the order they were registered. Why go through every step? It will just get to the highest max anyway...

Thanks for answer. Because that is how penny auctions works:)
Post Reply