Changeset 50
- Timestamp:
- 23.11.2006 19:36:27 (2 years ago)
- Files:
-
- trunk/devel/FuzzyOcr.pm (modified) (2 diffs)
- trunk/devel/FuzzyOcr/Config.pm (modified) (3 diffs)
- trunk/devel/FuzzyOcr/Misc.pm (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/devel/FuzzyOcr.pm
r49 r50 20 20 21 21 use 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);22 use 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); 23 23 use FuzzyOcr::Hashing qw(check_image_hash_db check_image_hash_db add_image_hash_db calc_image_hash); 24 24 use FuzzyOcr::Deanimate qw(deanimate); … … 62 62 if ($t->timed_out()) { 63 63 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 } 64 73 return 0; 65 74 } trunk/devel/FuzzyOcr/Config.pm
r49 r50 3 3 4 4 use base 'Exporter'; 5 our @EXPORT_OK = qw(get_pms 5 our @EXPORT_OK = qw( 6 set_pid 7 unset_pid 8 kill_pid 9 get_pms 6 10 save_pms 7 11 get_timeout … … 36 40 our $ddb; 37 41 our $timeout; 42 our $pid; 38 43 39 44 our @bin_utils = qw/gifsicle … … 68 73 } 69 74 return $timeout; 75 } 76 77 sub set_pid { 78 $pid = shift; 79 } 80 81 sub unset_pid { 82 $pid = 0; 83 } 84 85 sub kill_pid { 86 if ($pid) { 87 my $ret = kill POSIX::SIGTERM $pid; 88 return ($ret, $pid); 89 } else { 90 return (-1, 0); 91 } 70 92 } 71 93 trunk/devel/FuzzyOcr/Misc.pm
r18 r50 6 6 7 7 use lib "../"; 8 use FuzzyOcr::Config qw( get_pms get_config set_config debuglog logfile);;8 use FuzzyOcr::Config qw(set_pid unset_pid get_timeout get_pms get_config set_config debuglog logfile);; 9 9 10 10 # Provide some misc helper functions … … 38 38 } 39 39 40 sub 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 40 108 1;
