Re: Re: Problem with timeout

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



// sorry for posting my answer off list... //

4 maj 2007 kl. 17.35 skrev Emmanuel Raulo-Kumagai:

Frank Arensmeier a écrit :
Hello.
I am currently working on a script that parses a given http adress by looking for anchor tags, background images and so on - a crawler if you like. The downloaded content is temporarily stored on the server (Mac OS X Server 10.4.9 with PHP 5) and, when the script is done, the content is packed into a ZIP archive. It seems that I am having trouble with my Apache timeout setting. Because the script downloads between 250 and 300MB of html, pdf, css files and so on, it is terminated automatically after the specified timeout setting (currently 1200 seconds). Setting max execution time to -1 in the script has no effect (which has been noticed previously on the php.net manual page). Is there any other way (with PHP) to come around this problem besides setting the time-out in the Apache config to more than 1200 seconds? The Apache manual says that the time out value can only be changed within the core configuration scope. My initial idea was to set the value in a .htaccess file which unfortunately is not allowed. I might also add that the script is already optimized for speed so to say.
Hope you get what I mean.
//frank

Hello Frank

Are you really sure you need an Apache-spawned PHP script to do all that
long stuff ? Even if Apache does not give up on your PHP script, the
HTTP client might do so. This is also not a good idea for a site with
several clients because Apache could easily run low on available
sockets, causing a DoS.

I suggest you just spawn a background process from your PHP script,
through some shell command like batch and nohup. The background process
can still be coded in PHP with the CLI interface.

Thank you for sharing your suggestions.

The idea of running the script as a background process seems very elegant to me, I have to admit. Since my script is executed only once a week, it would be sufficient to set up a simple cron job. Modifications to the script are also rather small, since I already have e.g. functions for output logging, process locking and so on.

At the end of the long process, you alert the user job was done (e.g.
by mail or by some kind of AJAX mechanism) and let him download the
result.

In the Apache-spawned script:

<?php
// This will launch the process in background.
// Be sure to set $job_id.
$cmd_path = "/path/to/some/job/dir/job$job_id.sh";
$cmd = fopen( $cmd_path, 'w' );
fwrite(
  $cmd,
  "#!/bin/sh\n".
  "nohup php -f /path/to/your/scripts/long_process.php someargs\n".
  "rm -f $cmd_path" );
fclose( $cmd );
shell_exec( "batch -f $cmd_path" );

// Tell the user the job was scheduled...
?>

In long_process.php:

<?php
// Do your stuff...

// Send an e-mail to the user or change a persistent server-side
// state so that a script called periodically by the client will let
// her know it was done.
?>

You may need to give the right to www-data, or whatever account running
Apache, to create jobs through at/batch.

Regards

--
Emmanuel

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux