From 176c19f0a4e13193ec870917fe56acc3a2180db4 Mon Sep 17 00:00:00 2001 From: erickson Date: Fri, 12 Feb 2010 13:29:27 +0000 Subject: [PATCH] Patch from Joe Atzberger to add getop support for defininging the call style, hostname, etc. and a fix to bypass some extraneous oils_requestor output that was dying during json parsing git-svn-id: svn://svn.open-ils.org/ILS/trunk@15508 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../src/support-scripts/offline-blocked-list.pl | 211 +++++++++++++++------ 1 file changed, 149 insertions(+), 62 deletions(-) diff --git a/Open-ILS/src/support-scripts/offline-blocked-list.pl b/Open-ILS/src/support-scripts/offline-blocked-list.pl index 5d2480cf61..e7ca25510b 100755 --- a/Open-ILS/src/support-scripts/offline-blocked-list.pl +++ b/Open-ILS/src/support-scripts/offline-blocked-list.pl @@ -1,93 +1,180 @@ #!/usr/bin/perl +# + use strict; use warnings; -my $config = shift || die "Please specify a config file\n"; -my $context = shift || 'opensrf'; +use Getopt::Long; + +use OpenSRF::Utils::JSON; # for the oils_requestor approach +use IPC::Open2 qw/open2/; # for the oils_requestor approach +use Net::Domain qw/hostfqdn/; # for the oils_requestor approach + +# use OpenSRF::EX qw(:try); # for traditional approach +use OpenSRF::System; # for traditional approach +use OpenSRF::AppSession; # for traditional approach + +### USAGE + +sub usage { + my $defhost = hostfqdn(); + return <>file + +WARNING: +The new style offers performance benefits but seems to lose one line of data per call. + +END_OF_USAGE +} + +### DEFAULTS + +my $config = '/openils/conf/opensrf_core.xml'; +my $oils_reqr = '/openils/bin/oils_requestor'; +my $context = 'opensrf'; +my $hostname = hostfqdn(); +my $help = 0; +my $verbose = 0; +my $approach = 0; +my %types = (); + +GetOptions( + "barcodes=s" => \%types, + "config" => \$config, + "oldstyle" => \$approach, + "hostname" => \$hostname, + "requestor" => \$oils_reqr, + "verbose" => \$verbose, + "help" => \$help, +); + +### SANITY CHECK + +print usage() and exit if $help; + +(-r $config) or die "Cannot read config file\n"; + +%types or %types = ( # If you don't specify, you get'm all. + L => 'lost', + E => 'expired', # Possibly too many, making the file too large for download + B => 'barred', + D => 'penalized', +); + +my %counts = (); +foreach (keys %types) { + $counts{$_} = 0; # initialize count +} + +### FEEDBACK + +if ($verbose) { + print STDERR "verbose feedback is ON\n"; + print STDERR "hostname: $hostname\n"; + print STDERR "barcodes types:\n"; + foreach (sort keys %types) { + print STDERR " $_ ==> $types{$_}\n"; + } + print STDERR "Using the ", ($approach ? 'traditional' : 'new oils'), " approach\n"; +} + +### Engine of the new style piped approach +### Note, this appears to LOSE DATA, specifically one barcode value from each call. + +sub runmethod { + my $method = shift; + my $key = shift; + my $command = "echo \"open-ils.storage $method\" | $oils_reqr -f $config -c $context -h $hostname"; + $verbose and print STDERR "\nCOMMAND:\n-> $command\n"; + + my ($child_stdout, $child_stdin); + my $pid = open2($child_stdout, $child_stdin, $command); + for my $barcode (<$child_stdout>) { + next if $barcode =~ /^oils/o; # hack to chop out the oils_requestor prompt + next if $barcode =~ /^Connected to OpenSRF/o; + chomp $barcode; + $barcode = OpenSRF::Utils::JSON->JSON2perl($barcode); + print "$barcode $key\n" if $barcode; + $counts{$key}++; + } + close($child_stdout); + close($child_stdin); + waitpid($pid, 0); # don't leave any zombies (see ipc::open2) +} + +### MAIN + +if (! $approach) { # ------------------------------------------------------------ # This sends the method calls to storage via oils_requestor, # which is able to process the results much faster # Make this the default for now. # ------------------------------------------------------------ - use OpenSRF::Utils::JSON; - use IPC::Open2 qw/open2/; - use Net::Domain qw/hostfqdn/; - - sub runmethod { - my $method = shift; - my $flag = shift; - my $hostname = hostfqdn(); - my $command = "echo \"open-ils.storage $method\" | $oils_reqr -f $config -c $context -h $hostname"; - warn "-> $command\n"; - - my ($child_stdout, $child_stdin); - my $pid = open2($child_stdout, $child_stdin, $command); - my $x = 0; - for my $barcode (<$child_stdout>) { - next if $barcode =~ /^oils/o; # hack to chop out the oils_requestor prompt - chomp $barcode; - $barcode = OpenSRF::Utils::JSON->JSON2perl($barcode); - print "$barcode $flag\n" if $barcode; - } - close($child_stdout); - close($child_stdin); - waitpid($pid, 0); # don't leave any zombies (see ipc::open2) + foreach my $key (keys %types) { + runmethod('open-ils.storage.actor.user.' . $types{$key} . '_barcodes', $key); } - runmethod('open-ils.storage.actor.user.lost_barcodes', 'L'); - runmethod('open-ils.storage.actor.user.barred_barcodes', 'B'); - runmethod('open-ils.storage.actor.user.penalized_barcodes', 'D'); - # too many, makes the file too large for download - #runmethod('open-ils.storage.actor.user.expired_barcodes', 'E'); - } else { - - # ------------------------------------------------------------ # Uses the traditional opensrf Perl API approach # ------------------------------------------------------------ - use OpenSRF::EX qw(:try); - use OpenSRF::System; - use OpenSRF::AppSession; - OpenSRF::System->bootstrap_client( config_file => $config ); my $ses = OpenSRF::AppSession->connect( 'open-ils.storage' ); - my $lost = $ses->request( 'open-ils.storage.actor.user.lost_barcodes' ); - while (my $resp = $lost->recv ) { - print $resp->content . " L\n"; - } - $lost->finish; - - if(0) { # XXX just too many... arg - my $expired = $ses->request( 'open-ils.storage.actor.user.expired_barcodes' ); - while (my $resp = $expired->recv ) { - print $resp->content . " E\n"; + foreach my $key (keys %types) { + my $req = $ses->request( 'open-ils.storage.actor.user.' . $types{$key} . '_barcodes' ); + while (my $resp = $req->recv) { + print $resp->content, " $key\n"; + $counts{$key}++; } - $expired->finish; - } - - my $barred = $ses->request( 'open-ils.storage.actor.user.barred_barcodes' ); - while (my $resp = $barred->recv ) { - print $resp->content . " B\n"; + $req->finish; } - $barred->finish; - - my $penalized = $ses->request( 'open-ils.storage.actor.user.penalized_barcodes' ); - while (my $resp = $penalized->recv ) { - print $resp->content . " D\n"; - } - $penalized->finish; $ses->disconnect; $ses->finish; +} +if ($verbose) { + print STDERR "\nBarcodes retrieved:\n"; + foreach (sort keys %types) { + printf STDERR " %s ==> %9s ==> %d\n", $_, $types{$_}, $counts{$_}; + } + print STDERR "\ndone\n"; } -- 2.11.0