Objects:
- Object Sale
- n Object User will participate on Object Sale
- Each User can create one Object Agent
- Each Agent will generate n Object Licitation (from time to time and with some conditions) for the object Sale
Goal:
Create a function that when a Sale has only 5 seconds left to end, will check all the Agents that there are for this Sale and post a licitation from the Agent that hasn't made a licitation on this sale the longest
This is something that needs to be continuously running since this agents would be in charge to replace users on posting licitations allowing them to be away during auctions.
My question
Updated (My question wasn't clear enough):
At index page of the sales I have a script with javascript that calculates the time left on a sale, so I can know when a sale should call the function to check for agents and place their bids, but my newbie question is: if I make a call for the function at this page, will this only work if the user has the page open? Or if he closes the function won't be called anymore? Because I need this to still work even the user closes webpage. DaMacc already answered that this doesn't work
Updated (05/01/2010)
I've already created this function. The function will find all the sales that have 5 seconds left to end, then it will search all the agents and then place a bid from the agent that hasn't made a licitation on the selected sale the longest. Now i need this function to be called every second. I was going to use cron but cron has 1-minute boundaries. And I need this function to run on the server and not depend on the user that owns the agent.
Any suggestions?
PS: there开发者_如何学编程 are some auctions websites that have this kind of bidagents i'm trying to do, I could reffer one to use as example... But i'm not sure i'm allowed to do that here... :S
Thanks.
Create your normal function to do whatever it is you need to do and then use something like cron
to set it up as a task to run every X amount of time.
Edit to expand on comments
In that case you are probably better off combining a few solutions. As mentioned in this question, I would recommend that you use cron to call a script every minute and in that script you run your process in a loop.
Some things to consider:
- How long will each execution take?
- Will you need to time_sleep_until at the end of each loop or will your script take longer than 1 second to run, in which case you will need to be calling the script multiple times from cron.
- Be sure to keep track of how long your script has been running for as you don't want to have the situation where every minute you are taking up more and more resources as the script called the previous minute hasn't finished yet.
At the start of the script, take note of the current time, then at the start of each loop, check whether a minute has passed since the start of the script, if it has, exit the script (as another script will have taken over now thanks to cron).
Hope this helps. Let me know if this doesn't make a lot of sense and I'll try to clear it up a bit.
I wonder if this is what you are looking for:
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows") {
pclose(popen("start /B ". $cmd, "r"));
} else {
exec($cmd . " > /dev/null &");
}
}
You can then start a php process in the background using the following method:
execInBackground("php path/to/file.php " . $param01 . " " . $param02);
// where $param01 and $param02 are optional values that you may want to
// send to the page. Equivalent to GET parameters of URL.
// You can include as many parameter values as you want.
//Example:
execInBackground("php automation/bidChecker.php daniel 53.25");
// automation/bidChecker.php is the file to be executed
// daniel can be the username
// 53.25 can be the bid value
And in the PHP file that runs in the background, you can access the parameter values using the following method:
$param01 = $argv[1]; // assigns daniel as value
$param02 = $argv[2]; // assigns 53.25 as value
This process can be started from within a script run when the user does something. It will also keep on running even if the user leaves the page and until you programatically break the operation.
I really don't know if this is what you are looking for. If so, you got it now.
Use a Queue, like Zend_Queue: http://framework.zend.com/manual/en/zend.queue.html . Just run an infinite loop (or two) and dispatch messages sent from your web application
Have you looked into using Javascript at all for this? If you have certain events that trigger the need to run this check, it may be the way to go.
You could write a js function included on every page that uses the the setInterval/clearInterval
javascript functions to send an AJAX request to your server every few seconds and could send a different response back to the browser based on whether the conditions were met or not. (bandwidth may be an issue with this though)
I would recommend looking into jQuery and using it's AJAX functions for this.
精彩评论