Changeset 50

Show
Ignore:
Timestamp:
23.11.2006 19:36:27 (2 years ago)
Author:
decoder
Message:

Added function save_execute which will savely execute a helper application, without zombies

I will now replace the qx calls in the plugin by calls to this function

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/devel/FuzzyOcr.pm

    r49 r50  
    2020 
    2121use lib qw(.); # Allow placing of FuzzyOcr in siteconfigdir 
    22 use FuzzyOcr::Config qw(get_pms save_pms get_timeout save_timeout get_ddb get_thresholds get_scansets get_config get_wordlist set_config finish_parsing_end load_global_words load_personal_words debuglog logfile); 
     22use FuzzyOcr::Config qw(kill_pid get_pms save_pms get_timeout save_timeout get_ddb get_thresholds get_scansets get_config get_wordlist set_config finish_parsing_end load_global_words load_personal_words debuglog logfile); 
    2323use FuzzyOcr::Hashing qw(check_image_hash_db check_image_hash_db add_image_hash_db calc_image_hash); 
    2424use FuzzyOcr::Deanimate qw(deanimate); 
     
    6262        if ($t->timed_out()) { 
    6363            debuglog("Scan timed out after $conf->{focr_timeout} seconds."); 
     64            debuglog("Killing possibly running pid..."); 
     65            my ($ret, $pid) = kill_pid(); 
     66            if ($ret > 0) { 
     67                debuglog("Successfully killed PID $pid"); 
     68            } elsif ($ret < 0) { 
     69                debuglog("No processes left... exiting"); 
     70            } else { 
     71                debuglog("Failed to kill PID $pid, stale process!"); 
     72            } 
    6473            return 0; 
    6574        } 
  • trunk/devel/FuzzyOcr/Config.pm

    r49 r50  
    33 
    44use base 'Exporter'; 
    5 our @EXPORT_OK = qw(get_pms 
     5our @EXPORT_OK = qw( 
     6    set_pid 
     7    unset_pid 
     8    kill_pid 
     9    get_pms 
    610    save_pms  
    711    get_timeout 
     
    3640our $ddb; 
    3741our $timeout; 
     42our $pid; 
    3843 
    3944our @bin_utils = qw/gifsicle 
     
    6873    } 
    6974    return $timeout; 
     75} 
     76 
     77sub set_pid { 
     78    $pid = shift; 
     79} 
     80 
     81sub unset_pid { 
     82    $pid = 0; 
     83} 
     84 
     85sub kill_pid { 
     86    if ($pid) { 
     87        my $ret = kill POSIX::SIGTERM $pid; 
     88        return ($ret, $pid); 
     89    } else { 
     90        return (-1, 0); 
     91    } 
    7092} 
    7193 
  • trunk/devel/FuzzyOcr/Misc.pm

    r18 r50  
    66 
    77use lib "../"; 
    8 use FuzzyOcr::Config qw(get_pms get_config set_config debuglog logfile);; 
     8use FuzzyOcr::Config qw(set_pid unset_pid get_timeout get_pms get_config set_config debuglog logfile);; 
    99 
    1010# Provide some misc helper functions 
     
    3838} 
    3939 
     40sub save_execute { 
     41    my $conf = get_config(); 
     42    my $t = get_timeout(); 
     43    my ($cmd, $stdout, $stderr, $return_stdout) = @_; 
     44    my $retcode; 
     45    if ($conf->{'focr_global_timeout'}) { 
     46        my $pid = fork(); 
     47        if (not defined $pid) { 
     48            debuglog("Can't fork to execute external programs! Aborting"); 
     49            return -1; 
     50        } elsif (not $pid) { 
     51            open(STDOUT, $stdout); 
     52            open(STDERR, $stderr); 
     53            exec($cmd); 
     54            exit($?); 
     55        } else { 
     56            set_pid($pid); 
     57            wait(); 
     58            unset_pid(); 
     59            $retcode = $?; 
     60            if ($return_stdout) { 
     61                $stdout =~ tr/>|</   /; 
     62                open(INFILE, "<$stdout"); 
     63                my @stdout_data = <INFILE>; 
     64                close(INFILE); 
     65                return($retcode, @stdout_data); 
     66            } 
     67            return $retcode; 
     68        } 
     69    } else { 
     70        my @stdout_data; 
     71        my $pid; 
     72        $t->run_and_catch(sub { 
     73                $pid = fork(); 
     74                if (not defined $pid) { 
     75                    debuglog("Can't fork to execute external programs! Aborting"); 
     76                    return -1; 
     77                } elsif (not $pid) { 
     78                    open(STDOUT, $stdout); 
     79                    open(STDERR, $stderr); 
     80                    exec($cmd); 
     81                    exit($?); 
     82                } else { 
     83                    wait(); 
     84                    $retcode = $?; 
     85                    if ($return_stdout) { 
     86                        $stdout =~ tr/>|</   /; 
     87                        open(INFILE, "<$stdout"); 
     88                        @stdout_data = <INFILE>; 
     89                        close(INFILE); 
     90                    } 
     91                } 
     92        }); 
     93        if ($t->timed_out()) { 
     94            kill(POSIX::SIGTERM, $pid); 
     95            debuglog("Timeout occured, killed pid $pid. Consider increasing your timeout or decreasing your load."); 
     96            wait(); 
     97            return -1; 
     98        } else { 
     99            if ($return_stdout) { 
     100                return($retcode, @stdout_data); 
     101            } else { 
     102                return $retcode; 
     103            } 
     104        } 
     105    } 
     106} 
     107 
    401081;