Forum Moderators: coopster

Message Too Old, No Replies

Skip task if it takes too long

Methods to skip part of processing if not complete in specific time

         

punisa

12:14 pm on May 18, 2009 (gmt 0)

10+ Year Member



Hello everyone !

On my news site I have a small script that gathers RSS headlines from several news sources and stores them in my DB.

my code:


// $sources is an array with list of my sources
foreach($sources as $source) {
include('../sources/'.$source.'.php');
}

Now the code runs sources one by one and performs necessary tasks which are defined in each source include.
This works fine, but sometimes a particular source is unavailable or down.
Let's say my first 4 sources run fine and the fifth one is www.examplenews.com which is temporarily unavailable.

My script just hangs trying to connect to it and eventually times out. I've set timeout is set to 420 seconds.

Is there any way I can have PHP skip the particular bad source and proceed with the next one? Let's say if it can't connect within 10 seconds or so?

penders

1:55 pm on May 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is it possible to perhaps test the connection with cURL first and check the error code returned? Or just use cURL [uk2.php.net] instead?

Jonesy

5:01 pm on May 18, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Maybe something like this?

$timeout = 10;
$mox_nix = ini_set('default_socket_timeout', $timeout);

Jonesy

henry0

6:03 pm on May 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could (untested)
first segment your four RSSs
add a benchmarkto each segment:

//At the beginning of the script:
$start = microtime();

//end od script:
//$time_imparted is whatever exec time you agree on
$duration = $time_imparted + microtime();
//then compare $start and $duration
// if it exceeds $time_imparted then break (read below)

set your four RSSs as an array

create a switch and use BREAK() [php.net]

review the second example from the top first demo

<edit>second thoughts</edit>
Not sure the switch will resume if it breaks
as such you could in addition envision a few more IF

if it breaks in position 2 you know that 1 is OK so reload 3 & 4 etc..
see where I go ...

the whole thing is quite heavy I'll love to see a better solution.

<edit>more to it</edit>
Or create each RSSs as a function, then use exit()
exiting a function does not stop the whole script
you may even simplify it by creating that concept as a class.