From: erickson Date: Fri, 30 Oct 2009 01:55:06 +0000 (+0000) Subject: backporting max-delay-age validator, action_trigger runner script + trigger filters X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=1292ccf221dc77278656b0cdb09b499a07dd4358;p=Evergreen.git backporting max-delay-age validator, action_trigger runner script + trigger filters git-svn-id: svn://svn.open-ils.org/ILS/branches/rel_1_6_0@14692 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- diff --git a/Open-ILS/examples/action_trigger_filters.json.example b/Open-ILS/examples/action_trigger_filters.json.example new file mode 100644 index 0000000000..b35f5edf74 --- /dev/null +++ b/Open-ILS/examples/action_trigger_filters.json.example @@ -0,0 +1,15 @@ +{ + +'checkout.due' : + { 'context_org' : 'circ_lib', + 'filter' : + { 'checkin_time' : null, + '-or' : + [ { 'stop_fines' : ['MAXFINES', 'LONGOVERDUE'] }, + { 'stop_fines' : null } + ] + } + } + +} + diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor.pm b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor.pm index 6c2fcbb3e2..aa93eb1830 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor.pm @@ -55,19 +55,38 @@ my $_TT_helpers = { return $U->get_copy_price(new_editor(), $copy_id); }, + # given a copy, returns the title and author in a hash + get_copy_bib_basics => sub { + my $copy_id = shift; + my $copy = new_editor()->retrieve_asset_copy([ + $copy_id, + { + flesh => 2, + flesh_fields => { + acp => ['call_number'], + acn => ['record'] + } + } + ]); + if($copy->call_number->id == -1) { + return { + title => $copy->dummy_title, + author => $copy->dummy_author, + }; + } else { + my $mvr = $U->record_to_mvr($copy->call_number->record); + return { + title => $mvr->title, + author => $mvr->author + }; + } + }, + # returns the org unit setting value get_org_setting => sub { my($org_id, $setting) = @_; return $U->ou_ancestor_setting_value($org_id, $setting); }, - - # returns fines summary information for open transactions - get_user_fines_summary => sub { - my $user_id = shift; - return $U->simplereq( - 'open-ils.storage', - 'open-ils.storage.money.open_user_summary.search', $user_id); - } }; diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator.pm b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator.pm index b3f2389e4f..f0906b8f30 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator.pm @@ -13,7 +13,29 @@ sub CircIsOpen { my $self = shift; my $env = shift; - return defined($env->{target}->checkin_time) ? 0 : 1; + return 0 if (defined($env->{target}->checkin_time)); + return 0 if ($env->{params}->{max_delay_age} && !$self->MaxPassiveDelayAge($env)); + + return 1; +} + +sub MaxPassiveDelayAge { + my $self = shift; + my $env = shift; + my $target = $env->{target}; + my $delay_field = $env->{event}->event_def->delay_field; + + my $delay_field_ts = DateTime::Format::ISO8601->new->parse_datetime(clense_ISO8601($target->$delay_field())); + + # the cutoff date is the target timestamp + the delay + the max_delay_age + # This is also true for negative delays. For example: + # due_date + "-3 days" + "1 day" == -2 days old. + $delay_field_ts + ->add( seconds => interval_to_seconds( $env->{event}->event_def->delay ) ) + ->add( seconds => interval_to_seconds( $env->{params}->{max_delay_age} ) ); + + return 1 if $delay_field_ts > DateTime->now; + return 0; } sub CircIsOverdue { @@ -23,6 +45,7 @@ sub CircIsOverdue { return 0 if $circ->checkin_time; return 0 if $circ->stop_fines and not $circ->stop_fines =~ /MAXFINES|LONGOVERDUE/; + return 0 if ($env->{params}->{max_delay_age} && !$self->MaxPassiveDelayAge($env)); my $due_date = DateTime::Format::ISO8601->new->parse_datetime(clense_ISO8601($circ->due_date)); return 0 if $due_date > DateTime->now; diff --git a/Open-ILS/src/support-scripts/action_trigger_runner.pl b/Open-ILS/src/support-scripts/action_trigger_runner.pl new file mode 100755 index 0000000000..f6f26bd325 --- /dev/null +++ b/Open-ILS/src/support-scripts/action_trigger_runner.pl @@ -0,0 +1,161 @@ +#!/usr/bin/perl +# --------------------------------------------------------------- +# Copyright (C) 2009 Equinox Software, Inc +# Author: Bill Erickson +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# --------------------------------------------------------------- +use strict; +use warnings; +use Getopt::Long; +use OpenSRF::AppSession; +use OpenSRF::Utils::JSON; +use OpenSRF::EX qw(:try); +require 'oils_header.pl'; + +my $opt_lockfile = '/tmp/action-trigger-LOCK'; +my $opt_osrf_config = '/openils/conf/opensrf_core.xml'; +my $opt_custom_filter = '/openils/conf/action_trigger_filters.json'; +my $opt_run_pending = 0; +my $opt_debug_stdout = 0; +my $opt_help = 0; +my $opt_hooks; + +GetOptions( + 'osrf-config=s' => \$opt_osrf_config, + 'run-pending' => \$opt_run_pending, + 'hooks=s' => \$opt_hooks, + 'debug-stdout' => \$opt_debug_stdout, + 'custom-filters=s' => \$opt_custom_filter, + 'lock-file=s' => \$opt_lockfile, + 'help' => \$opt_help, +); + + +# typical passive hook filters +my $hook_handlers = { + + # default overdue circulations + 'checkout.due' => { + context_org => 'circ_lib', + filter => { + checkin_time => undef, + '-or' => [ + {stop_fines => ['MAXFINES', 'LONGOVERDUE']}, + {stop_fines => undef} + ] + } + } +}; + +if ($opt_custom_filter) { + open FILTERS, $opt_custom_filter; + $hook_handlers = OpenSRF::Utils::JSON->JSON2Perl(join('',())); + close FILTERS; +} + +sub help { + print < + OpenSRF core config file. Defaults to: + /openils/conf/opensrf_core.xml + + --custom-filters= + File containing a JSON Object which describes any hooks that should + use a user-defined filter to find their target objects. Defaults to: + /openils/conf/action_trigger_filters.json + + --run-pending + Run pending action_trigger.event's + + --hooks=hook1[,hook2,hook3,...] + Hooks for which events should be generated. Defaults to the list of + hooks defined in the --custom-filters option. + + --debug-stdout + Print server responses to stdout (as JSON) for debugging + + --lock-file= + Lock file + + --help + Show this help + + Examples: + + # To run all pending events. This is what you tell CRON to run at + # regular intervals + perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending + + # To batch create all "checkout.due" events + perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due + +HELP +} + + +# create events for the specified hooks using the configured filters and context orgs +sub process_hooks { + + my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : keys(%$hook_handlers); + my $ses = OpenSRF::AppSession->create('open-ils.trigger'); + + for my $hook (@hooks) { + + my $config = $$hook_handlers{$hook} or next; + my $method = 'open-ils.trigger.passive.event.autocreate.batch'; + $method =~ s/passive/active/ if $config->{active}; + + my $req = $ses->request($method, $hook, $config->{context_org}, $config->{filter}); + while(my $resp = $req->recv(timeout => 1800)) { + if($opt_debug_stdout) { + print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n"; + } + } + } +} + +sub run_pending { + return unless $opt_run_pending; + my $ses = OpenSRF::AppSession->create('open-ils.trigger'); + my $req = $ses->request('open-ils.trigger.event.run_all_pending'); + while(my $resp = $req->recv(timeout => 600)) { + if($opt_debug_stdout) { + print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n"; + } + } +} + +help() and exit if $opt_help; +help() and exit unless ($opt_run_pending or $opt_hooks); + +# check / set the lockfile +die "I'm already running with lockfile $opt_lockfile\n" if -e $opt_lockfile; +open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writing\n"; +print F $$; +close F; + +try { + osrf_connect($opt_osrf_config); + process_hooks(); + run_pending(); +} otherwise { + my $e = shift; + warn "$e\n"; +}; + +unlink $opt_lockfile; + + +