From 6701a98f9c3326b7b72e60f2dca0e331abba2182 Mon Sep 17 00:00:00 2001 From: miker Date: Tue, 27 Oct 2009 16:57:29 +0000 Subject: [PATCH] move the hook configuration out to a config file (action_trigger_filters.json.example); default to the list of hooks in that file if none are specified; use try/otherwise instead of eval/$@ git-svn-id: svn://svn.open-ils.org/ILS/trunk@14629 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../examples/action_trigger_filters.json.example | 15 +++++++ .../src/support-scripts/action_trigger_runner.pl | 52 ++++++++++++++-------- 2 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 Open-ILS/examples/action_trigger_filters.json.example 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..800664308d --- /dev/null +++ b/Open-ILS/examples/action_trigger_filters.json.example @@ -0,0 +1,15 @@ +{ + +'checkout.due' : + { 'context_org' => 'circ_lib', + 'filter' => + { 'checkin_time' => undef, + '-or' => + [ { 'stop_fines' => ['MAXFINES', 'LONGOVERDUE'] }, + { 'stop_fines' => undef } + ] + } + } + +} + diff --git a/Open-ILS/src/support-scripts/action_trigger_runner.pl b/Open-ILS/src/support-scripts/action_trigger_runner.pl index 02e3738733..f6f26bd325 100755 --- a/Open-ILS/src/support-scripts/action_trigger_runner.pl +++ b/Open-ILS/src/support-scripts/action_trigger_runner.pl @@ -18,14 +18,15 @@ 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_custom_filter; my $opt_hooks; GetOptions( @@ -33,16 +34,16 @@ GetOptions( 'run-pending' => \$opt_run_pending, 'hooks=s' => \$opt_hooks, 'debug-stdout' => \$opt_debug_stdout, - 'custom-filter=s' => \$opt_custom_filter, + 'custom-filters=s' => \$opt_custom_filter, 'lock-file=s' => \$opt_lockfile, 'help' => \$opt_help, ); # typical passive hook filters -my %hook_handlers = ( +my $hook_handlers = { - # overdue circulations + # default overdue circulations 'checkout.due' => { context_org => 'circ_lib', filter => { @@ -53,26 +54,39 @@ my %hook_handlers = ( ] } } -); +}; + +if ($opt_custom_filter) { + open FILTERS, $opt_custom_filter; + $hook_handlers = OpenSRF::Utils::JSON->JSON2Perl(join('',())); + close FILTERS; +} sub help { print < - OpenSRF config file + --osrf-config= + 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 to generate events for + --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= Lock file --help @@ -80,7 +94,8 @@ $0 : Create and process action/trigger events Examples: - # To run all pending events. This is what you tell CRON to run at regular intervals + # 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 @@ -93,18 +108,16 @@ HELP # create events for the specified hooks using the configured filters and context orgs sub process_hooks { - my @hooks = ($opt_hooks) ? split(',', $opt_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 $config = $$hook_handlers{$hook} or next; my $method = 'open-ils.trigger.passive.event.autocreate.batch'; $method =~ s/passive/active/ if $config->{active}; - my $filter = ($opt_custom_filter) ? OpenSRF::Utils::JSON->JSON2Perl($opt_custom_filter) : $config->{filter}; - - my $req = $ses->request($method, $hook, $config->{context_org}, $filter); + 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"; @@ -133,14 +146,15 @@ open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writ print F $$; close F; -eval { +try { osrf_connect($opt_osrf_config); process_hooks(); run_pending(); +} otherwise { + my $e = shift; + warn "$e\n"; }; -warn "$@\n" if $@; - unlink $opt_lockfile; -- 2.11.0