]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/hold_targeter.pl
Error Checking for Hold Targeter
[working/Evergreen.git] / Open-ILS / src / support-scripts / hold_targeter.pl
1 #!/usr/bin/perl
2 # ---------------------------------------------------------------------
3 # Usage:
4 #   hold_targeter.pl <config_file> <lock_file>
5 # ---------------------------------------------------------------------
6
7 use strict; 
8 use warnings;
9 use OpenSRF::Utils::JSON;
10 use OpenSRF::System;
11 use OpenSRF::Utils::SettingsClient;
12 use OpenSRF::MultiSession;
13 use OpenSRF::EX qw(:try);
14
15 my $config = shift || die "bootstrap config required\n";
16 my $lockfile = shift || "/tmp/hold_targeter-LOCK";
17
18 if (-e $lockfile) {
19     die "I seem to be running already. If not remove $lockfile, try again\n";
20 }
21
22 open(F, ">$lockfile");
23 print F $$;
24 close F;
25
26 my $settings;
27 my $parallel;
28
29 try {
30     OpenSRF::System->bootstrap_client( config_file => $config );
31     $settings = OpenSRF::Utils::SettingsClient->new;
32     $parallel = $settings->config_value( hold_targeter => 'parallel' ) || 1;
33 } otherwise {
34     my $e = shift;
35     warn "$e\n";
36     unlink $lockfile;
37     exit 1;
38 };
39
40 if ($parallel == 1) {
41
42     try {
43         my $r = OpenSRF::AppSession
44                    ->create( 'open-ils.storage' )
45                    ->request( 'open-ils.storage.action.hold_request.copy_targeter' => '24h' );
46
47         while (!$r->complete) { 
48             my $start = time;
49             $r->recv(timeout => 3600);
50             last if (time() - $start) >= 3600;
51         };
52     } otherwise {
53         my $e = shift;
54         warn "Failure in single-session targeter:\n$e\n";
55     };
56
57 } else {
58
59     try {
60         my $multi_targeter = OpenSRF::MultiSession->new(
61             app => 'open-ils.storage', 
62             cap => $parallel, 
63             api_level => 1,
64             session_hash_function => sub {
65                 my $ses = shift;
66                 my $req = shift;
67                 return $_[-1]; # last parameter is the ID of the metarecord associated with the
68                                # request's target; using this as the hash function value ensures
69                                # that parallel targeters won't try to simultaneously handle two
70                                # hold requests that have overlapping pools of copies that could
71                                # fill those requests
72             }
73         );
74     
75         my $storage = OpenSRF::AppSession->create("open-ils.storage");
76     
77         my $r = $storage->request('open-ils.storage.action.hold_request.targetable_holds.id_list', '24h');
78         while ( my $h = $r->recv ) {
79             if ($r->failed) {
80                 print $r->failed->stringify . "\n";
81                 last;
82             }
83             if (my $hold = $h->content) {
84                 $multi_targeter->request( 'open-ils.storage.action.hold_request.copy_targeter', '', $hold->[0], $hold->[1]);
85             }
86         }
87     
88         $storage->disconnect();
89     
90         $multi_targeter->session_wait(1);
91         $multi_targeter->disconnect;
92     } otherwise {
93         my $e = shift;
94         warn "Failure in multi-session targeter:\n$e\n";
95     }
96 }
97
98 unlink $lockfile;
99