]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/action_trigger_runner.pl
Patch from Joe Atzberger to verify custom filter file opened successfully
[working/Evergreen.git] / Open-ILS / src / support-scripts / action_trigger_runner.pl
1 #!/usr/bin/perl
2 # ---------------------------------------------------------------
3 # Copyright (C) 2009 Equinox Software, Inc
4 # Author: Bill Erickson <erickson@esilibrary.com>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------
16 use strict;
17 use warnings;
18 use Getopt::Long;
19 use OpenSRF::System;
20 use OpenSRF::AppSession;
21 use OpenSRF::Utils::JSON;
22 use OpenSRF::EX qw(:try);
23 use OpenILS::Utils::Fieldmapper;
24
25 my $opt_lockfile = '/tmp/action-trigger-LOCK';
26 my $opt_osrf_config = '/openils/conf/opensrf_core.xml';
27 my $opt_custom_filter = '/openils/conf/action_trigger_filters.json';
28 my $opt_run_pending = 0;
29 my $opt_debug_stdout = 0;
30 my $opt_help = 0;
31 my $opt_hooks;
32 my $opt_process_hooks = 0;
33 my $opt_granularity = undef;
34
35 GetOptions(
36     'osrf-config=s' => \$opt_osrf_config,
37     'run-pending' => \$opt_run_pending,
38     'hooks=s' => \$opt_hooks,
39     'granularity=s' => \$opt_granularity,
40     'process-hooks' => \$opt_process_hooks,
41     'debug-stdout' => \$opt_debug_stdout,
42     'custom-filters=s' => \$opt_custom_filter,
43     'lock-file=s' => \$opt_lockfile,
44     'help' => \$opt_help,
45 );
46
47
48 # typical passive hook filters
49 my $hook_handlers = {
50
51     # default overdue circulations
52     'checkout.due' => {
53         context_org => 'circ_lib',
54         filter => {
55             checkin_time => undef, 
56             '-or' => [
57                 {stop_fines => ['MAXFINES', 'LONGOVERDUE']}, 
58                 {stop_fines => undef}
59             ]
60         }
61     }
62 };
63
64 if ($opt_custom_filter) {
65     if (open FILTERS, $opt_custom_filter) {
66         $hook_handlers = OpenSRF::Utils::JSON->JSON2perl(join('',(<FILTERS>)));
67         close FILTERS;
68     } else {
69         die "Cannot read filter file '$opt_custom_filter'";
70     }
71 }
72
73 sub help {
74     print <<HELP;
75
76 $0 : Create and process action/trigger events
77
78     --osrf-config=<config_file>
79         OpenSRF core config file.  Defaults to:
80             /openils/conf/opensrf_core.xml
81
82     --custom-filters=<filter_file>
83         File containing a JSON Object which describes any hooks that should
84         use a user-defined filter to find their target objects.  Defaults to:
85             /openils/conf/action_trigger_filters.json
86
87     --run-pending
88         Run pending events
89
90     --process-hooks
91         Create hook events
92
93     --hooks=hook1[,hook2,hook3,...]
94         Define which hooks to create events for.  If none are defined,
95         it defaults to the list of hooks defined in the --custom-filters option.
96
97     --granularity=label
98         Run events with {label} granularity setting, or no granularity setting
99
100     --debug-stdout
101         Print server responses to stdout (as JSON) for debugging
102
103     --lock-file=<file_name>
104         Lock file
105
106     --help
107         Show this help
108
109     Examples:
110
111         # To run all pending events.  This is what you tell CRON to run at
112         # regular intervals
113         perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
114
115         # To batch create all "checkout.due" events
116         perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
117
118 HELP
119 }
120
121
122 # create events for the specified hooks using the configured filters and context orgs
123 sub process_hooks {
124     return unless $opt_process_hooks;
125
126     my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : keys(%$hook_handlers);
127     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
128
129     for my $hook (@hooks) {
130     
131         my $config = $$hook_handlers{$hook} or next;
132         my $method = 'open-ils.trigger.passive.event.autocreate.batch';
133         $method =~ s/passive/active/ if $config->{active};
134         
135         my $req = $ses->request($method, $hook, $config->{context_org}, $config->{filter}, $opt_granularity);
136         while(my $resp = $req->recv(timeout => 1800)) {
137             if($opt_debug_stdout) {
138                 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
139             }
140         }
141     }
142 }
143
144 sub run_pending {
145     return unless $opt_run_pending;
146     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
147     my $req = $ses->request('open-ils.trigger.event.run_all_pending' => $opt_granularity);
148
149     my $check_lockfile = 1;
150     while(my $resp = $req->recv(timeout => 7200)) {
151         if ($check_lockfile && -e $opt_lockfile) {
152             open LF, $opt_lockfile;
153             my $contents = <LF>;
154             close LF;
155             unlink $opt_lockfile if ($contents == $$);
156             $check_lockfile = 0;
157         }
158         if($opt_debug_stdout) {
159             print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
160         }
161     }
162 }
163
164 help() and exit if $opt_help;
165 help() and exit unless ($opt_run_pending or $opt_process_hooks);
166
167 # check / set the lockfile
168 die "I'm already running with lockfile $opt_lockfile\n" if -e $opt_lockfile;
169 open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writing\n";
170 print F $$;
171 close F;
172
173 try {
174         OpenSRF::System->bootstrap_client(config_file => $opt_osrf_config);
175         Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
176     process_hooks();
177     run_pending();
178 } otherwise {
179     my $e = shift;
180     warn "$e\n";
181 };
182
183 if (-e $opt_lockfile) {
184     open LF, $opt_lockfile;
185     my $contents = <LF>;
186     close LF;
187     unlink $opt_lockfile if ($contents == $$);
188 }
189