LP#1549505: Provide a cron-able script to perform badge recalculation
authorMike Rylander <mrylander@gmail.com>
Wed, 3 Feb 2016 20:46:52 +0000 (15:46 -0500)
committerKathy Lussier <klussier@masslnc.org>
Fri, 29 Jul 2016 20:56:10 +0000 (16:56 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/biblio.pm
Open-ILS/src/support-scripts/badge_score_generator.pl [new file with mode: 0755]

index c285489..3f52baf 100644 (file)
@@ -185,6 +185,33 @@ __PACKAGE__->register_method(
     api_level   => 1,
 );
 
+
+sub regenerate_badge_list {
+    my $self = shift;
+    my $client = shift;
+
+    my $sth = biblio::record_entry->db_Main->prepare_cached( <<"    SQL" );
+        SELECT  r.id AS badge
+          FROM  rating.badge r
+          WHERE r.last_calc < NOW() - r.recalc_interval
+                OR r.last_calc IS NULL
+          ORDER BY r.last_calc ASC NULLS FIRST -- oldest first
+    SQL
+
+    $sth->execute;
+    while ( my $row = $sth->fetchrow_hashref ) {
+        $client->respond( $row->{badge} );
+    }
+    return undef;
+}
+__PACKAGE__->register_method(
+    api_name    => 'open-ils.storage.biblio.regenerate_badge_list',
+    method      => 'regenerate_badge_list',
+    api_level   => 1,
+    cachable    => 1,
+);
+
+
 sub record_by_barcode {
     my $self = shift;
     my $client = shift;
diff --git a/Open-ILS/src/support-scripts/badge_score_generator.pl b/Open-ILS/src/support-scripts/badge_score_generator.pl
new file mode 100755 (executable)
index 0000000..5196b48
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+# ---------------------------------------------------------------------
+# Badge score generator
+# ./badge_score_generator.pl <bootstrap_config> <lockfile>
+# ---------------------------------------------------------------------
+
+use strict; 
+use warnings;
+use OpenSRF::Utils::JSON;
+use OpenSRF::System;
+use OpenSRF::Utils::SettingsClient;
+use OpenSRF::MultiSession;
+
+my $config = shift || die "bootstrap config required\n";
+my $lockfile = shift || "/tmp/generate_badge_scores-LOCK";
+
+if (-e $lockfile) {
+        open(F,$lockfile);
+        my $pid = <F>;
+        close F;
+
+        open(F,'/bin/ps axo pid|');
+        while ( my $p = <F>) {
+                chomp($p);
+                if ($p =~ s/\s*(\d+)$/$1/o && $p == $pid) {
+                        die "I seem to be running already at pid $pid.  If not, try again\n";
+                }
+        }
+        close F;
+}
+
+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( badge_score_generator => 'parallel' ) || 1; 
+
+my $multi_generator = OpenSRF::MultiSession->new(
+    app => 'open-ils.cstore', 
+    cap => $parallel, 
+    api_level => 1,
+);
+
+my $storage = OpenSRF::AppSession->create("open-ils.storage");
+my $r = $storage->request('open-ils.storage.biblio.regenerate_badge_list');
+
+while (my $resp = $r->recv) {
+    my $badge_id = $resp->content;
+    $multi_generator->request(
+        'open-ils.cstore.json_query',
+        { from => [ 'rating.recalculate_badge_score' => $badge_id ] }
+    );
+}
+$storage->disconnect();
+$multi_generator->session_wait(1);
+$multi_generator->disconnect;
+
+unlink $lockfile;