4 use strict; use warnings;
8 use OpenSRF::Utils::JSON; # for the oils_requestor approach
9 use IPC::Open2 qw/open2/; # for the oils_requestor approach
10 use Net::Domain qw/hostfqdn/; # for the oils_requestor approach
12 # use OpenSRF::EX qw(:try); # for traditional approach
13 use OpenSRF::System; # for traditional approach
14 use OpenSRF::AppSession; # for traditional approach
19 my $defhost = hostfqdn();
24 Generate a file of blocked barcodes for offline use. There are two styles of
25 invocation, old and new (using oils_requestor). See WARNING.
27 By default, all known blocked barcodes are output. Override with --barcodes option.
31 --oldstyle Use traditional OpenSRF calls default: OFF.
32 --verbose give feedback on STDERR, including number of barcodes fetched
33 --help print this message
35 --config =[file] core config file default: /openils/conf/opensrf_core.xml
36 --requestor=[/path/to/requstor] default: /openils/bin/oils_requestor
37 --hostname =[my.fqdn.com] default: hostfqdn()
38 (Only used by new style.)
39 May be necessary if hostfqdn does not match router configs.
40 Currently your hostfqdn is '$defhost'.
42 --barcodes [key=eg_code] default: ALL
43 Specify what kind of barcodes to fetch and how to tag them in the output.
44 The key is the (one letter) tag used in the offline file,
45 and the eg_code is the component of the SRF call that targets the barcodes
46 (like "lost"). NOTE: This option can be specified multiple times.
51 # Use the old style with a custom config
52 $0 --config /openils/conf/test_core.xml --oldstyle
54 # Append just lost and barred barcodes to file, showing feedback on STDERR
55 $0 --verbose --barcodes L=lost --barcodes B=barred >>file
58 The new style offers performance benefits but seems to lose one line of data per call.
65 my $config = '/openils/conf/opensrf_core.xml';
66 my $oils_reqr = '/openils/bin/oils_requestor';
67 my $context = 'opensrf';
68 my $hostname = hostfqdn();
75 "barcodes=s" => \%types,
76 "config:s" => \$config,
77 "oldstyle" => \$approach,
78 "hostname:s" => \$hostname,
79 "requestor:s" => \$oils_reqr,
80 "verbose" => \$verbose,
86 print usage() and exit if $help;
88 (-r $config) or die "Cannot read config file\n";
90 %types or %types = ( # If you don't specify, you get'm all.
92 E => 'expired', # Possibly too many, making the file too large for download
98 foreach (keys %types) {
99 $counts{$_} = 0; # initialize count
105 print STDERR "verbose feedback is ON\n";
106 print STDERR "hostname: $hostname\n";
107 print STDERR "barcodes types:\n";
108 foreach (sort keys %types) {
109 print STDERR " $_ ==> $types{$_}\n";
111 print STDERR "Using the ", ($approach ? 'traditional' : 'new oils'), " approach\n";
114 ### Engine of the new style piped approach
115 ### Note, this appears to LOSE DATA, specifically one barcode value from each call.
120 my $command = "echo \"open-ils.storage $method\" | $oils_reqr -f $config -c $context -h $hostname";
121 $verbose and print STDERR "\nCOMMAND:\n-> $command\n";
123 my ($child_stdout, $child_stdin);
124 my $pid = open2($child_stdout, $child_stdin, $command);
125 for my $barcode (<$child_stdout>) {
126 next if $barcode =~ /^oils/o; # hack to chop out the oils_requestor prompt
127 next if $barcode =~ /^Connected to OpenSRF/o;
129 $barcode = OpenSRF::Utils::JSON->JSON2perl($barcode);
130 print "$barcode $key\n" if $barcode;
133 close($child_stdout);
135 waitpid($pid, 0); # don't leave any zombies (see ipc::open2)
141 # ------------------------------------------------------------
142 # This sends the method calls to storage via oils_requestor,
143 # which is able to process the results much faster
144 # Make this the default for now.
145 # ------------------------------------------------------------
147 foreach my $key (keys %types) {
148 runmethod('open-ils.storage.actor.user.' . $types{$key} . '_barcodes', $key);
152 # ------------------------------------------------------------
153 # Uses the traditional opensrf Perl API approach
154 # ------------------------------------------------------------
156 OpenSRF::System->bootstrap_client( config_file => $config );
158 my $ses = OpenSRF::AppSession->connect( 'open-ils.storage' );
160 foreach my $key (keys %types) {
161 my $req = $ses->request( 'open-ils.storage.actor.user.' . $types{$key} . '_barcodes' );
162 while (my $resp = $req->recv) {
163 print $resp->content, " $key\n";
174 print STDERR "\nBarcodes retrieved:\n";
175 foreach (sort keys %types) {
176 printf STDERR " %s ==> %9s ==> %d\n", $_, $types{$_}, $counts{$_};
178 print STDERR "\ndone\n";