Changeset 10

Show
Ignore:
Timestamp:
10/04/06 15:32:02 (7 years ago)
Author:
lincoln
Message:

some code refactoring, cutdown writes to config_file if nothing changed, first pass at making XMLTV parsing more bulletproof (not there yet)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • shepherd

    r9 r10  
    4040use DateTime::Format::Strptime; 
    4141use POSIX qw(strftime); 
     42use Time::HiRes qw(gettimeofday tv_interval); 
    4243 
    4344# --------------------------------------------------------------------------- 
     
    152153    update($progname, $version); 
    153154    set_order(1) if $made_changes; 
    154     write_config_file() unless ($opt->{configure}); 
     155    write_config_file() if (($made_changes) && (! $opt->{configure})) 
    155156} 
    156157 
     
    190191    my $need_more_data = 1; 
    191192 
     193    printf "\nGrabber stage:\n"; 
     194 
    192195    # iterate across grabbers until we have all our data we want (or need) 
    193196    foreach my $grabber (sort { $grabbers->{$a}->{order} <=> $grabbers->{$b}->{order} } keys %$grabbers) { 
     
    198201        $grabbers->{$grabber}->{laststatus} = "unknown"; 
    199202 
    200         printf "SHEPHERD: Using grabber: (%d) %s\n",$grabbers->{$grabber}->{order},$grabber; 
     203        printf "\nSHEPHERD: Using grabber: (%d) %s\n",$grabbers->{$grabber}->{order},$grabber; 
    201204 
    202205        my $output = "$GRABBER_DIR/$grabber/output.xmltv"; 
     
    222225 
    223226        # soak up the data we just collected 
    224         &soak_up_data($grabber, $output); 
     227        &soak_up_data($grabber, $output, "grabber"); 
    225228        $grabbers->{$grabber}->{laststatus} = $plugin_data->{$grabber}->{laststatus}; 
    226229 
    227230        # check to see if we have all the data we want 
    228         $need_more_data = &analyze_plugin_data($channel_ok_threshold_percent); 
     231        $need_more_data = &analyze_plugin_data($channel_ok_threshold_percent, "AGGREGATE GRABBER"); 
    229232 
    230233        last if (!$need_more_data); 
     
    250253sub soak_up_data 
    251254{ 
    252     my ($grabber, $output) = @_; 
    253     eval { $plugin_data->{$grabber}->{xmltv} = XMLTV::parsefiles($output); }; 
    254  
    255     if (defined $plugin_data->{$grabber}->{xmltv}) { 
    256         $plugin_data->{$grabber}->{valid} = 1; 
    257  
    258         my $xmltv = $plugin_data->{$grabber}->{xmltv}; 
     255    my ($plugin, $output, $plugintype) = @_; 
     256 
     257    if (! -r $output) { 
     258        printf "SHEPHERD: Warning: plugin '%s' output file '%s' does not exist\n",$plugin,$output; 
     259        return; 
     260    } 
     261 
     262    my $parse_start_time = [gettimeofday]; 
     263    printf STDERR "SHEPHERD: Started parsing XMLTV from '%s' in '%s' .. any errors below are from parser:\n",$plugin,$output; 
     264    eval { 
     265        local $SIG{__DIE__},  
     266        $plugin_data->{$plugin}->{xmltv} = XMLTV::parsefiles($output); 
     267    }; 
     268    printf STDERR "SHEPHERD: Warning: Parser returned the following: $@\n" if $@; 
     269    printf STDERR "SHEPHERD: Completed XMLTV parsing from '%s' in %0.2f seconds\n",$plugin,tv_interval($parse_start_time); 
     270 
     271    if (defined $plugin_data->{$plugin}->{xmltv}) { 
     272        $plugin_data->{$plugin}->{valid} = 1; 
     273 
     274        my $xmltv = $plugin_data->{$plugin}->{xmltv}; 
    259275        my ($encoding, $credits, $chan, $progs) = @$xmltv; 
    260         $plugin_data->{$grabber}->{total_duration} = 0; 
    261         $plugin_data->{$grabber}->{programmes} = 0; 
     276        $plugin_data->{$plugin}->{total_duration} = 0; 
     277        $plugin_data->{$plugin}->{programmes} = 0; 
    262278 
    263279        my $strptime = new DateTime::Format::Strptime( pattern => "%Y%m%d%H%M %z"); 
     
    276292                next if (!$t1 || !$t2); # if we can't parse stop/start then clearly THIS data is bunk! 
    277293 
    278                 # store grabber-specific stats 
    279                 $plugin_data->{$grabber}->{programmes}++; 
    280                 $plugin_data->{$grabber}->{total_duration} += ($t2->epoch - $t1->epoch); 
     294                # store plugin-specific stats 
     295                $plugin_data->{$plugin}->{programmes}++; 
     296                $plugin_data->{$plugin}->{total_duration} += ($t2->epoch - $t1->epoch); 
    281297                $seen_progs_on_this_channel++; 
    282                 $plugin_data->{$grabber}->{earliest_data_seen} = $t1->epoch if (!defined $plugin_data->{$grabber}->{earliest_data_seen}); 
    283                 $plugin_data->{$grabber}->{earliest_data_seen} = $t1->epoch if ($t1->epoch < $plugin_data->{$grabber}->{earliest_data_seen}); 
    284                 $plugin_data->{$grabber}->{latest_data_seen} = $t2->epoch if (!defined $plugin_data->{$grabber}->{latest_data_seen}); 
    285                 $plugin_data->{$grabber}->{latest_data_seen} = $t2->epoch if ($t2->epoch > $plugin_data->{$grabber}->{latest_data_seen}); 
     298                $plugin_data->{$plugin}->{earliest_data_seen} = $t1->epoch if (!defined $plugin_data->{$plugin}->{earliest_data_seen}); 
     299                $plugin_data->{$plugin}->{earliest_data_seen} = $t1->epoch if ($t1->epoch < $plugin_data->{$plugin}->{earliest_data_seen}); 
     300                $plugin_data->{$plugin}->{latest_data_seen} = $t2->epoch if (!defined $plugin_data->{$plugin}->{latest_data_seen}); 
     301                $plugin_data->{$plugin}->{latest_data_seen} = $t2->epoch if ($t2->epoch > $plugin_data->{$plugin}->{latest_data_seen}); 
    286302 
    287303                # store channel-specific stats 
     
    315331 
    316332        # print some stats about what we saw! 
    317         printf "SHEPHERD: Grabber '%s' returned data for %d channels, %d programmes, %dd%02dh%02dm%02ds duration, %s%s\n", 
    318             $grabber, $seen_channels_with_data, $plugin_data->{$grabber}->{programmes}, 
    319             int($plugin_data->{$grabber}->{total_duration} / 86400),            # days 
    320             int(($plugin_data->{$grabber}->{total_duration} % 86400) / 3600),   # hours 
    321             int(($plugin_data->{$grabber}->{total_duration} % 3600) / 60),      # mins 
    322             int($plugin_data->{$grabber}->{total_duration} % 60),               # sec 
    323             (defined $plugin_data->{$grabber}->{earliest_data_seen} ? (strftime "%a %e %b %H:%M - ", localtime($plugin_data->{$grabber}->{earliest_data_seen})) : 'no data'), 
    324             (defined $plugin_data->{$grabber}->{latest_data_seen} ? (strftime "%a %e %b %H:%M", localtime($plugin_data->{$grabber}->{latest_data_seen})) : ''); 
    325         $plugin_data->{$grabber}->{laststatus} = sprintf "%dch/%dpr/%dhrs %s-%s", 
    326             $seen_channels_with_data, $plugin_data->{$grabber}->{programmes}, 
    327             int($plugin_data->{$grabber}->{total_duration} / 3600), 
    328             (defined $plugin_data->{$grabber}->{earliest_data_seen} ? (strftime "%a%d%b%H:%M", localtime($plugin_data->{$grabber}->{earliest_data_seen})) : 'no'), 
    329             (defined $plugin_data->{$grabber}->{latest_data_seen} ? (strftime "%a%d%b%H:%M", localtime($plugin_data->{$grabber}->{latest_data_seen})) : 'data'); 
     333        printf "SHEPHERD: %s '%s' returned data for %d channels, %d programmes, %dd%02dh%02dm%02ds duration, %s%s\n", 
     334            ucfirst($plugintype), $plugin, $seen_channels_with_data, $plugin_data->{$plugin}->{programmes}, 
     335            int($plugin_data->{$plugin}->{total_duration} / 86400),             # days 
     336            int(($plugin_data->{$plugin}->{total_duration} % 86400) / 3600),    # hours 
     337            int(($plugin_data->{$plugin}->{total_duration} % 3600) / 60),       # mins 
     338            int($plugin_data->{$plugin}->{total_duration} % 60),                # sec 
     339            (defined $plugin_data->{$plugin}->{earliest_data_seen} ? (strftime "%a %e %b %H:%M - ", localtime($plugin_data->{$plugin}->{earliest_data_seen})) : 'no data'), 
     340            (defined $plugin_data->{$plugin}->{latest_data_seen} ? (strftime "%a %e %b %H:%M", localtime($plugin_data->{$plugin}->{latest_data_seen})) : ''); 
     341        $plugin_data->{$plugin}->{laststatus} = sprintf "%dch/%dpr/%dhrs %s-%s", 
     342            $seen_channels_with_data, $plugin_data->{$plugin}->{programmes}, 
     343            int($plugin_data->{$plugin}->{total_duration} / 3600), 
     344            (defined $plugin_data->{$plugin}->{earliest_data_seen} ? (strftime "%a%d%b%H:%M", localtime($plugin_data->{$plugin}->{earliest_data_seen})) : 'no'), 
     345            (defined $plugin_data->{$plugin}->{latest_data_seen} ? (strftime "%a%d%b%H:%M", localtime($plugin_data->{$plugin}->{latest_data_seen})) : 'data'); 
    330346 
    331347    } else { 
    332         printf "WARNING: Plugin %s didn't seem to return any valid XMLTV!\n",$grabber; 
    333         delete $plugin_data->{$grabber}->{valid}; 
     348        printf "WARNING: Plugin %s didn't seem to return any valid XMLTV!\n",$plugin; 
     349        delete $plugin_data->{$plugin}->{valid}; 
    334350    } 
    335351} 
     
    340356sub analyze_plugin_data 
    341357{ 
    342     my $threshold = shift; 
     358    my ($threshold,$analysistype) = @_; 
    343359    my $retval = 0; # until proven otherwise 
    344360    my $total_data_percent = 0, my $total_channels = 0; 
     
    376392 
    377393    # print some stats about what our analysis says! 
    378     printf "SHEPHERD: ANALYSIS: %sTOTAL %0.2f%% %s %0.2f%%: %s\n", $statusstring, $total_data_percent, 
     394    printf "SHEPHERD: %s ANALYSIS: %sTOTAL %0.2f%% %s %0.2f%%: %s\n", 
     395        uc($analysistype), $statusstring, $total_data_percent, 
    379396        ($total_data_percent >= $channel_ok_threshold_percent ? ">" : "<"), $channel_ok_threshold_percent, 
    380397        ($retval ? "WANT MORE DATA" : "COMPLETE"); 
     
    416433    my $need_more_data; 
    417434 
     435    printf "\nPostprocessing stage:\n"; 
     436 
    418437    foreach my $postprocessor (sort { $postprocessors->{$a} <=> $postprocessors->{$b} } keys %$postprocessors) { 
    419438        next if ($postprocessors->{$postprocessor}->{disabled}); 
     
    422441        $postprocessors->{$postprocessor}->{laststatus} = "unknown"; 
    423442 
    424         printf "SHEPHERD: Using postprocessor: %s\n",$postprocessor; 
     443        printf "\nSHEPHERD: Using postprocessor: %s\n",$postprocessor; 
    425444 
    426445        my $output = "$POSTPROCESSOR_DIR/$postprocessor/output.xmltv"; 
     
    455474 
    456475        # process and analyze it! 
    457         &soak_up_data($postprocessor, $output); 
    458         $need_more_data = &analyze_plugin_data($postprocessor_ok_threshold_percent); 
     476        &soak_up_data($postprocessor, $output, "postprocessor"); 
     477        $need_more_data = &analyze_plugin_data($postprocessor_ok_threshold_percent, "POSTPROCESSOR"); 
    459478 
    460479        $postprocessors->{$postprocessor}->{laststatus} = $plugin_data->{$postprocessor}->{laststatus}; 
     
    472491                $postprocessor, 
    473492                $postprocessors->{$postprocessor}->{conescutive_failures}, 
    474                 $postprocessor_disable_failure_threshold; 
     493                ($postprocessor_disable_failure_threshold - $postprocessors->{$postprocessor}->{conescutive_failures}); 
    475494 
    476495            if ($postprocessors->{$postprocessor}->{conescutive_failures} >= $postprocessor_disable_failure_threshold) { 
     
    521540sub update 
    522541{ 
    523     print "\nFetching status file: $STATUS.\n"; 
     542    printf "\nChecking for updates:\n\n"; 
     543 
     544    print "Fetching status file: $STATUS."; 
    524545    my $data = LWP::Simple::get($STATUS); 
    525546    unless ($data) 
     
    561582{ 
    562583    my ($proggy, $latestversion, $progtype) = @_; 
    563  
    564     print "\n"; 
    565584 
    566585    # handle new installs..