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