LP#1596595 open-ils.hold-targeter service
authorBill Erickson <berickxx@gmail.com>
Wed, 29 Jun 2016 22:09:49 +0000 (18:09 -0400)
committerBill Erickson <berickxx@gmail.com>
Fri, 26 Aug 2016 21:32:17 +0000 (17:32 -0400)
Standalone open-ils service for hold targeting.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/examples/opensrf.xml.example
Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/Circulate.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/Holds.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/HoldTargeter.pm [new file with mode: 0644]

index 87db4cc..167611a 100644 (file)
@@ -1176,6 +1176,27 @@ vim:et:ts=4:sw=4:
                 </app_settings>
             </open-ils.serial>
 
+            <open-ils.hold-targeter>
+                <keepalive>3</keepalive>
+                <stateless>1</stateless>
+                <language>perl</language>
+                <implementation>OpenILS::Application::HoldTargeter</implementation>
+                <max_requests>17</max_requests>
+                <unix_config>
+                    <unix_sock>open-ils.hold-targeter_unix.sock</unix_sock>
+                    <unix_pid>open-ils.hold-targeter_unix.pid</unix_pid>
+                    <max_requests>1000</max_requests>
+                    <unix_log>open-ils.hold-targeter_unix.log</unix_log>
+                    <min_children>1</min_children>
+                    <max_children>15</max_children>
+                    <min_spare_children>1</min_spare_children>
+                    <max_spare_children>5</max_spare_children>
+                </unix_config>
+                <app_settings>
+                </app_settings>
+            </open-ils.hold-targeter>
+
+
         </apps>
     </default>
 
@@ -1220,6 +1241,7 @@ vim:et:ts=4:sw=4:
                 <appname>open-ils.fielder</appname>  
                 <appname>open-ils.vandelay</appname>  
                 <appname>open-ils.serial</appname>  
+                <appname>open-ils.hold-targeter</appname>  
             </activeapps>
         </localhost>
     </hosts>
index e349c52..0f66884 100644 (file)
@@ -7,7 +7,6 @@ use OpenSRF::Utils::SettingsClient;
 use OpenSRF::Utils::Logger qw(:logger);
 use OpenILS::Const qw/:const/;
 use OpenILS::Application::AppUtils;
-use OpenILS::Utils::HoldTargeter;
 use DateTime;
 my $U = "OpenILS::Application::AppUtils";
 
@@ -2300,9 +2299,10 @@ sub checkin_retarget {
                 next if ($_->{hold_type} eq 'P');
             }
             # So much for easy stuff, attempt a retarget!
-            my $tresult = OpenILS::Utils::HoldTargeter->new->target(
-                hold => $_->{id}, 
-                find_copy => $self->copy->id
+            my $tresult = $U->simplereq(
+                'open-ils.hold-targeter',
+                'open-ils.hold-targeter.target', 
+                {hold => $_->{id}, find_copy => $self->copy->id}
             );
             if(ref $tresult eq "ARRAY" and scalar @$tresult) {
                 last if(exists $tresult->[0]->{found_copy} and $tresult->[0]->{found_copy});
@@ -3064,7 +3064,9 @@ sub do_hold_notify {
 sub retarget_holds {
     my $self = shift;
     $logger->info("circulator: retargeting holds @{$self->retarget} after opportunistic capture");
-    OpenILS::Utils::HoldTargeter->new->target(hold => $self->retarget);
+    my $ses = OpenSRF::AppSession->create('open-ils.hold-targeter');
+    $ses->request('open-ils.hold-targeter.target', {hold => $self->retarget});
+    # no reason to wait for the return value
     return;
 }
 
index 4705a55..ce0c4c0 100644 (file)
@@ -27,7 +27,6 @@ use OpenSRF::Utils;
 use OpenSRF::Utils::Logger qw(:logger);
 use OpenILS::Utils::CStoreEditor q/:funcs/;
 use OpenILS::Utils::PermitHold;
-use OpenILS::Utils::HoldTargeter;
 use OpenSRF::Utils::SettingsClient;
 use OpenILS::Const qw/:const/;
 use OpenILS::Application::Circ::Transit;
@@ -381,8 +380,9 @@ sub create_hold {
 
     $conn->respond_complete($hold->id);
 
-    OpenILS::Utils::HoldTargeter->new->target(hold => $hold->id) 
-        unless $U->is_true($hold->frozen);
+    $U->simplereq('open-ils.hold-targeter',
+        'open-ils.hold-targeter.target', {hold => $hold->id}
+    ) unless $U->is_true($hold->frozen);
 
     return undef;
 }
@@ -746,7 +746,8 @@ sub uncancel_hold {
     $e->update_action_hold_request($hold) or return $e->die_event;
     $e->commit;
 
-    OpenILS::Utils::HoldTargeter->new->target(hold => $hold_id);
+    $U->simplereq('open-ils.hold-targeter',
+        'open-ils.hold-targeter.target', {hold => $hold_id});
 
     return 1;
 }
@@ -1064,14 +1065,16 @@ sub update_hold_impl {
 
     if(!$U->is_true($hold->frozen) && $U->is_true($orig_hold->frozen)) {
         $logger->info("Running targeter on activated hold ".$hold->id);
-        OpenILS::Utils::HoldTargeter->new->target(hold => $hold->id);
+        $U->simplereq('open-ils.hold-targeter', 
+            'open-ils.hold-targeter.target', {hold => $hold->id});
     }
 
     # a change to mint-condition changes the set of potential copies, so retarget the hold;
     if($U->is_true($hold->mint_condition) and !$U->is_true($orig_hold->mint_condition)) {
         _reset_hold($self, $e->requestor, $hold)
     } elsif($need_retarget && !defined $hold->capture_time()) { # If needed, retarget the hold due to changes
-        OpenILS::Utils::HoldTargeter->new->target(hold => $hold->id);
+        $U->simplereq('open-ils.hold-targeter', 
+            'open-ils.hold-targeter.target', {hold => $hold->id});
     }
 
     return $hold->id;
@@ -1159,7 +1162,8 @@ sub update_hold_if_frozen {
     } else {
         if($U->is_true($orig_hold->frozen)) {
             $logger->info("Running targeter on activated hold ".$hold->id);
-            OpenILS::Utils::HoldTargeter->new->target(hold => $hold->id);
+            $U->simplereq('open-ils.hold-targeter', 
+                'open-ils.hold-targeter.target', {hold => $hold->id});
         }
     }
 }
@@ -1992,7 +1996,8 @@ sub _reset_hold {
     $e->update_action_hold_request($hold) or return $e->die_event;
     $e->commit;
 
-    OpenILS::Utils::HoldTargeter->new->target(hold => $hold->id);
+    $U->simplereq('open-ils.hold-targeter', 
+        'open-ils.hold-targeter.target', {hold => $hold->id});
 
     return undef;
 }
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/HoldTargeter.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/HoldTargeter.pm
new file mode 100644 (file)
index 0000000..037f230
--- /dev/null
@@ -0,0 +1,76 @@
+package OpenILS::Application::HoldTargeter;
+use strict; 
+use warnings;
+use OpenILS::Application;
+use base qw/OpenILS::Application/;
+use OpenILS::Utils::HoldTargeter;
+
+__PACKAGE__->register_method(
+    method    => 'hold_targeter',
+    api_name  => 'open-ils.hold-targeter.target',
+    api_level => 1,
+    argc      => 1,
+    stream    => 1,
+    # Caller is given control over how often to receive responses.
+    max_chunk_size => 0,
+    signature => {
+        desc     => q/Batch or single hold targeter./,
+        params   => [
+            {   name => 'args',
+                desc => 'Hash of targeter options',
+                type => 'hash'
+            }
+        ],
+        return => {
+            desc => q/
+                TODO
+            /
+        }
+    }
+);
+
+# args:
+#
+#   return_count - Return number of holds processed so far instead 
+#       of hold targeter result summary objects.
+#
+#   return_throttle - Only reply each time this many holds have been 
+#       targeted.  This prevents dumping a fast stream of responses
+#       at the client if the client doesn't need them.
+#
+#   See OpenILS::Utils::HoldTargeter::target() docs.
+
+sub hold_targeter {
+    my ($self, $client, $args) = @_;
+
+    my $targeter = OpenILS::Utils::HoldTargeter->new(%$args);
+
+    $targeter->init;
+
+    my $throttle = $args->{return_throttle} || 1;
+    my $count = 0;
+
+    for my $hold_id ($targeter->find_holds_to_target) {
+        $count++;
+
+        my $single = OpenILS::Utils::HoldTargeter::Single->new(
+            parent => $targeter,
+            skip_viable => $args->{skip_viable}
+        );
+
+        $single->target($hold_id);
+
+        if (($count % $throttle) == 0) { 
+            # Time to reply to the caller.  Return either the number
+            # processed thus far or the most recent summary object.
+
+            my $res = $args->{return_count} ? $count : $single->result;
+            $client->respond($res);
+        }
+    }
+
+    return undef;
+}
+
+1;
+