Page 1 of 1

PHP Execution Time

Posted: Tue Dec 28, 2010 10:55 am
by MythX
Hi, I'm setting up a site for a client of mine that regularly needs to retrieve information from another website using cUrl. It then uses this information to update records in a database (MySQL). The time it takes to retrieve this information and then update the associated record averages out to about 2 seconds. The problem I'm running into is that there's 16,000+ records in the database and it will grow. Doing the math, this script should take just under 9 hours to execute. Since the hosting site sets the maximum execution time for a PHP script to 30 seconds (and doesn't allow it to change), I have set the script to track the execution time as it loops through and once the script has been running for 15 seconds, it breaks the loop, uses JavaScript to issue a window.location.reload() and then picks up where it left off.

This works, but I'm not satisfied that it's the best solution. I like the idea that the script will pick up where it left off, I don't like the idea of an entire page refresh, and there's always the possibility that the cUrl will hang for longer than 30 seconds and the script will end before it gets a chance to hit the break code. I was told an Ajax load bar is the way to go, but when I search for one, I don't find anything that will help with my situation and I'm not an Ajax expert by any means.

Thanks in advance.

Jason

Re: PHP Execution Time

Posted: Tue Dec 28, 2010 11:20 am
by rcrd.ortiz
Hi,

Do you have access to your clients computer? If you do what I would do is launch the script from command line locally or make a cron for it. That way you avoid the php execution time as well. You could also try php's set_time_limit function.

8)

Re: PHP Execution Time

Posted: Tue Dec 28, 2010 11:48 am
by MythX
rcrd.ortiz wrote:Hi,

Do you have access to your clients computer? If you do what I would do is launch the script from command line locally or make a cron for it. That way you avoid the php execution time as well. You could also try php's set_time_limit function.

8)
Thanks for the replay, but it's being hosted with GoDaddy. Great for personal websites and such, but limited when it comes to professional/enterprise level sites. GoDaddy sets the PHP max execution time to 30 seconds and they don't allow you to adjust it. At least not within their managed shared hosting products.

Re: PHP Execution Time

Posted: Wed Dec 29, 2010 6:00 am
by rcrd.ortiz
Then ajax is the way, you could make a progress bar and update it after the ajax call returns data. I will use jquery in the example, I also built a small background service that checks if processing is being done before requesting again.

Code: Select all

<script type="text/javascript">
var isWorking = false;
var currentRequest = 0;

function doYourStuff( paramIfNeeded )
{
    $.ajax({
        url: "/app/your.action?param=" + paramIfNeeded, 
        dataType: "json",
        success: function( data ) 
        {
        	isWorking = false;
            if( data.status == 'ok' )
            {
            	// Update the progress bar
            }
            else
            {
        		// Notify an error and ask the user if he want's to restart or try to recover or whatever.			
            },
        error: function()
        {
        	isWorking = false;
        	// Ajax status was ko. Your could retry at this point or do nothing		
        }
}
function doWork()
{
    if( isWorking == false )
    {
    	isWorking = true;
    		
    	doYourStuff( currentRequest++ );
    }
    	setTimeout( doWork, 1000 );
}
</script>
You would need to setup your page so that when its done it returns json (if you use the code above), you could throw back how the process went, for example
{
'status': 'ok',
'more': 'true',
'progression': '70.25%'
}
Then you can use this data to update the progress bar, and to know if more requests are to be made, etc.

Hope this helped