Error Checking for Hold Targeter
authorThomas Berezansky <tsbere@mvlc.org>
Fri, 6 Apr 2012 18:39:28 +0000 (14:39 -0400)
committerBen Shum <bshum@biblio.org>
Mon, 11 Mar 2013 21:50:15 +0000 (17:50 -0400)
Because having it leave lock files around is annoying.

Signed-off-by: Thomas Berezansky <tsbere@mvlc.org>
Signed-off-by: Ben Shum <bshum@biblio.org>
Open-ILS/src/support-scripts/hold_targeter.pl

index cd62227..2ca196e 100755 (executable)
@@ -10,6 +10,7 @@ use OpenSRF::Utils::JSON;
 use OpenSRF::System;
 use OpenSRF::Utils::SettingsClient;
 use OpenSRF::MultiSession;
+use OpenSRF::EX qw(:try);
 
 my $config = shift || die "bootstrap config required\n";
 my $lockfile = shift || "/tmp/hold_targeter-LOCK";
@@ -22,54 +23,76 @@ open(F, ">$lockfile");
 print F $$;
 close F;
 
-OpenSRF::System->bootstrap_client( config_file => $config );
-my $settings = OpenSRF::Utils::SettingsClient->new;
-my $parallel = $settings->config_value( hold_targeter => 'parallel' ) || 1; 
+my $settings;
+my $parallel;
 
-if ($parallel == 1) {
+try {
+    OpenSRF::System->bootstrap_client( config_file => $config );
+    $settings = OpenSRF::Utils::SettingsClient->new;
+    $parallel = $settings->config_value( hold_targeter => 'parallel' ) || 1;
+} otherwise {
+    my $e = shift;
+    warn "$e\n";
+    unlink $lockfile;
+    exit 1;
+};
 
-    my $r = OpenSRF::AppSession
-               ->create( 'open-ils.storage' )
-               ->request( 'open-ils.storage.action.hold_request.copy_targeter' => '24h' );
+if ($parallel == 1) {
 
-    while (!$r->complete) { 
-        my $start = time;
-        $r->recv(timeout => 3600);
-        last if (time() - $start) >= 3600;
+    try {
+        my $r = OpenSRF::AppSession
+                   ->create( 'open-ils.storage' )
+                   ->request( 'open-ils.storage.action.hold_request.copy_targeter' => '24h' );
+
+        while (!$r->complete) { 
+            my $start = time;
+            $r->recv(timeout => 3600);
+            last if (time() - $start) >= 3600;
+        };
+    } otherwise {
+        my $e = shift;
+        warn "Failure in single-session targeter:\n$e\n";
     };
 
 } else {
 
-    my $multi_targeter = OpenSRF::MultiSession->new(
-        app => 'open-ils.storage', 
-        cap => $parallel, 
-        api_level => 1,
-        session_hash_function => sub {
-            my $ses = shift;
-            my $req = shift;
-            return $_[-1]; # last parameter is the ID of the metarecord associated with the
-                           # request's target; using this as the hash function value ensures
-                           # that parallel targeters won't try to simultaneously handle two
-                           # hold requests that have overlapping pools of copies that could
-                           # fill those requests
-        }
-    );
-
-    my $storage = OpenSRF::AppSession->create("open-ils.storage");
-
-    my $r = $storage->request('open-ils.storage.action.hold_request.targetable_holds.id_list', '24h');
-    while ( my $h = $r->recv ) {
-        die $r->failed->stringify . "\n" if $r->failed;
-        if (my $hold = $h->content) {
-            $multi_targeter->request( 'open-ils.storage.action.hold_request.copy_targeter', '', $hold->[0], $hold->[1]);
+    try {
+        my $multi_targeter = OpenSRF::MultiSession->new(
+            app => 'open-ils.storage', 
+            cap => $parallel, 
+            api_level => 1,
+            session_hash_function => sub {
+                my $ses = shift;
+                my $req = shift;
+                return $_[-1]; # last parameter is the ID of the metarecord associated with the
+                               # request's target; using this as the hash function value ensures
+                               # that parallel targeters won't try to simultaneously handle two
+                               # hold requests that have overlapping pools of copies that could
+                               # fill those requests
+            }
+        );
+    
+        my $storage = OpenSRF::AppSession->create("open-ils.storage");
+    
+        my $r = $storage->request('open-ils.storage.action.hold_request.targetable_holds.id_list', '24h');
+        while ( my $h = $r->recv ) {
+            if ($r->failed) {
+                print $r->failed->stringify . "\n";
+                last;
+            }
+            if (my $hold = $h->content) {
+                $multi_targeter->request( 'open-ils.storage.action.hold_request.copy_targeter', '', $hold->[0], $hold->[1]);
+            }
         }
+    
+        $storage->disconnect();
+    
+        $multi_targeter->session_wait(1);
+        $multi_targeter->disconnect;
+    } otherwise {
+        my $e = shift;
+        warn "Failure in multi-session targeter:\n$e\n";
     }
-
-    $storage->disconnect();
-
-    $multi_targeter->session_wait(1);
-    $multi_targeter->disconnect;
-
 }
 
 unlink $lockfile;