]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/action_trigger_runner.pl
wait to run-pending if a specific granularity used
[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::Utils::Logger qw/$logger/;
23 use OpenSRF::EX qw(:try);
24 use OpenILS::Utils::Fieldmapper;
25 my $req_timeout = 10800;
26
27 # DEFAULT values
28
29 my $opt_lockfile      = '/tmp/action-trigger-LOCK';
30 my $opt_osrf_config   = '/openils/conf/opensrf_core.xml';
31 my $opt_custom_filter = '/openils/conf/action_trigger_filters.json';
32 my $opt_max_sleep     = 3600;  # default to 1 hour
33 my $opt_run_pending   = 0;
34 my $opt_debug_stdout  = 0;
35 my $opt_help          = 0;
36 my $opt_verbose;
37 my $opt_hooks;
38 my $opt_process_hooks = 0;
39 my $opt_granularity   = undef;
40 my $opt_gran_only     = undef;
41
42 (-f $opt_custom_filter) or undef($opt_custom_filter);   # discard default if no file exists
43
44 GetOptions(
45     'max-sleep'        => \$opt_max_sleep,
46     'osrf-config=s'    => \$opt_osrf_config,
47     'run-pending'      => \$opt_run_pending,
48     'hooks=s'          => \$opt_hooks,
49     'granularity=s'    => \$opt_granularity,
50     'granularity-only' => \$opt_gran_only,
51     'process-hooks'    => \$opt_process_hooks,
52     'debug-stdout'     => \$opt_debug_stdout,
53     'custom-filters=s' => \$opt_custom_filter,
54     'lock-file=s'      => \$opt_lockfile,
55     'verbose'          => \$opt_verbose,
56     'help'             => \$opt_help,
57 );
58
59 my $max_sleep = $opt_max_sleep;
60
61 $opt_lockfile .= '.' . $opt_granularity if ($opt_granularity && $opt_gran_only);
62
63 # typical passive hook filters
64 my $hook_handlers = {
65
66     # default overdue circulations
67     'checkout.due' => {
68         context_org => 'circ_lib',
69         filter => {
70             checkin_time => undef, 
71             '-or' => [
72                 {stop_fines => ['MAXFINES', 'LONGOVERDUE']}, 
73                 {stop_fines => undef}
74             ]
75         }
76     }
77 };
78
79 if ($opt_custom_filter) {
80     open FILTERS, $opt_custom_filter or die "Cannot read custom filters at $opt_custom_filter";
81     $hook_handlers = OpenSRF::Utils::JSON->JSON2perl(join('',(<FILTERS>)));
82     close FILTERS;
83 }
84
85 sub help {
86     print <<HELP;
87
88 $0 : Create and process action/trigger events
89
90     --osrf-config=<config_file>
91         OpenSRF core config file.  Defaults to:
92             /openils/conf/opensrf_core.xml
93
94     --custom-filters=<filter_file>
95         File containing a JSON Object which describes any hooks that should
96         use a user-defined filter to find their target objects.  Defaults to:
97             /openils/conf/action_trigger_filters.json
98
99     --run-pending
100         Run pending events
101
102     --process-hooks
103         Create hook events
104
105     --max-sleep=<seconds>
106         When in process-hooks mode, wait up to <seconds> for the lock file to
107         go away.  Defaults to 3600 (1 hour).
108
109     --hooks=hook1[,hook2,hook3,...]
110         Define which hooks to create events for.  If none are defined,
111         it defaults to the list of hooks defined in the --custom-filters option.
112
113     --granularity=<label>
114         Run events with {label} granularity setting, or no granularity setting
115
116     --granularity-only
117         Used in combination with --granularity, prevents the running of events with no granularity setting
118
119     --debug-stdout
120         Print server responses to stdout (as JSON) for debugging
121
122     --lock-file=<file_name>
123         Lock file
124
125     --help
126         Show this help
127
128     Examples:
129
130         # To run all pending events.  This is what you tell CRON to run at
131         # regular intervals
132         perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
133
134         # To batch create all "checkout.due" events
135         perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
136
137 HELP
138 }
139
140
141 # create events for the specified hooks using the configured filters and context orgs
142 sub process_hooks {
143     $opt_verbose and print "process_hooks: " . ($opt_process_hooks ? '(start)' : 'SKIPPING') . "\n";
144     return unless $opt_process_hooks;
145
146     my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : keys(%$hook_handlers);
147     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
148
149     for my $hook (@hooks) {
150         my $config = $$hook_handlers{$hook};
151         $opt_verbose and print "process_hooks: $hook " . ($config ? ($opt_granularity || '') : ' NO HANDLER') . "\n";
152         $config or next;
153
154         my $method = 'open-ils.trigger.passive.event.autocreate.batch';
155         $method =~ s/passive/active/ if $config->{active};
156         
157         my $req = $ses->request($method, $hook, $config->{context_org}, $config->{filter}, $opt_granularity);
158
159         my $debug_hook = "'$hook' and filter ".OpenSRF::Utils::JSON->perl2JSON($config->{filter});
160         $logger->info("at_runner: creating events for $debug_hook");
161
162         my @event_ids;
163         while (my $resp = $req->recv(timeout => $req_timeout)) {
164             push(@event_ids, $resp->content);
165         }
166
167         if(@event_ids) {
168             $logger->info("at_runner: created ".scalar(@event_ids)." events for $debug_hook");
169         } elsif($req->complete) {
170             $logger->info("at_runner: no events to create for $debug_hook");
171         } else {
172             $logger->warn("at_runner: timeout occurred during event creation for $debug_hook");
173         }
174     }
175 }
176
177 sub run_pending {
178     $opt_verbose and print "run_pending: " .
179         ($opt_run_pending ?
180             ($opt_granularity ?
181                 ( $opt_granularity . (
182                     $opt_gran_only ?
183                         ' ONLY' : 
184                         ' and NON-GRANULAR'
185                     )
186                 ) :
187                 'NON-GRANULAR'
188             ) :
189             'SKIPPING'
190         ) . "\n";
191
192     return unless $opt_run_pending;
193     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
194     my $req = $ses->request('open-ils.trigger.event.run_all_pending' => $opt_granularity => $opt_gran_only);
195
196     my $check_lockfile = 1;
197     while (my $resp = $req->recv(timeout => $req_timeout)) {
198         if ($check_lockfile && -e $opt_lockfile) {
199             open LF, $opt_lockfile;
200             my $contents = <LF>;
201             close LF;
202             unlink $opt_lockfile if ($contents == $$);
203             $check_lockfile = 0;
204         }
205         $opt_debug_stdout and print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
206     }
207 }
208
209 help() and exit if $opt_help;
210 help() and exit unless ($opt_run_pending or $opt_process_hooks);
211
212 # check the lockfile
213 if (-e $opt_lockfile) {
214     die "I'm already running with lockfile $opt_lockfile\n" unless ($opt_process_hooks or $opt_granularity);
215     # sleeping loop if we're in --process-hooks mode
216     while ($max_sleep >= 0 && sleep(1)) {
217         last unless ( -e $opt_lockfile ); 
218         $max_sleep--;
219     }
220 }
221
222 # there's a tiny race condition here ... oh well
223 die "Someone else has been holding the lockfile $opt_lockfile for at least $opt_max_sleep. Giving up now ...\n" if (-e $opt_lockfile);
224
225 # set the lockfile
226 open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writing\n";
227 print F $$;
228 close F;
229
230 try {
231         OpenSRF::System->bootstrap_client(config_file => $opt_osrf_config);
232         Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
233     process_hooks();
234     run_pending();
235 } otherwise {
236     my $e = shift;
237     warn "$e\n";
238 };
239
240 if (-e $opt_lockfile) {
241     open LF, $opt_lockfile;
242     my $contents = <LF>;
243     close LF;
244     unlink $opt_lockfile if ($contents == $$);
245 }