From b6dd839f6c292849017cdc8a2f0ff590facd70cc Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Wed, 3 Feb 2016 15:46:52 -0500 Subject: [PATCH] LP#1549505: Provide a cron-able script to perform badge recalculation Signed-off-by: Mike Rylander Signed-off-by: Galen Charlton Signed-off-by: Kathy Lussier --- .../Application/Storage/Publisher/biblio.pm | 27 ++++++++++ .../src/support-scripts/badge_score_generator.pl | 60 ++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100755 Open-ILS/src/support-scripts/badge_score_generator.pl diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/biblio.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/biblio.pm index c2854897b0..3f52bafb24 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/biblio.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/biblio.pm @@ -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 index 0000000000..5196b48afd --- /dev/null +++ b/Open-ILS/src/support-scripts/badge_score_generator.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl +# --------------------------------------------------------------------- +# Badge score generator +# ./badge_score_generator.pl +# --------------------------------------------------------------------- + +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 = ; + close F; + + open(F,'/bin/ps axo pid|'); + while ( my $p = ) { + 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; -- 2.11.0