]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/action_trigger_runner.pl
added explicit --process-hooks flag to turn on hook event creation. This allows...
[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::AppSession;
20 use OpenSRF::Utils::JSON;
21 use OpenSRF::EX qw(:try);
22 require 'oils_header.pl';
23
24 my $opt_lockfile = '/tmp/action-trigger-LOCK';
25 my $opt_osrf_config = '/openils/conf/opensrf_core.xml';
26 my $opt_custom_filter = '/openils/conf/action_trigger_filters.json';
27 my $opt_run_pending = 0;
28 my $opt_debug_stdout = 0;
29 my $opt_help = 0;
30 my $opt_hooks;
31 my $opt_process_hooks = 0;
32
33 GetOptions(
34     'osrf-config=s' => \$opt_osrf_config,
35     'run-pending' => \$opt_run_pending,
36     'hooks=s' => \$opt_hooks,
37     'process-hooks' => \$opt_process_hooks,
38     'debug-stdout' => \$opt_debug_stdout,
39     'custom-filters=s' => \$opt_custom_filter,
40     'lock-file=s' => \$opt_lockfile,
41     'help' => \$opt_help,
42 );
43
44
45 # typical passive hook filters
46 my $hook_handlers = {
47
48     # default overdue circulations
49     'checkout.due' => {
50         context_org => 'circ_lib',
51         filter => {
52             checkin_time => undef, 
53             '-or' => [
54                 {stop_fines => ['MAXFINES', 'LONGOVERDUE']}, 
55                 {stop_fines => undef}
56             ]
57         }
58     }
59 };
60
61 if ($opt_custom_filter) {
62     open FILTERS, $opt_custom_filter;
63     $hook_handlers = OpenSRF::Utils::JSON->JSON2perl(join('',(<FILTERS>)));
64     close FILTERS;
65 }
66
67 sub help {
68     print <<HELP;
69
70 $0 : Create and process action/trigger events
71
72     --osrf-config=<config_file>
73         OpenSRF core config file.  Defaults to:
74             /openils/conf/opensrf_core.xml
75
76     --custom-filters=<filter_file>
77         File containing a JSON Object which describes any hooks that should
78         use a user-defined filter to find their target objects.  Defaults to:
79             /openils/conf/action_trigger_filters.json
80
81     --run-pending
82         Run pending events
83
84     --process-hooks
85         Create hook events
86
87     --hooks=hook1[,hook2,hook3,...]
88         Define which hooks to create events for.  If none are defined,
89         it defaults to the list of hooks defined in the --custom-filters option.
90
91     --debug-stdout
92         Print server responses to stdout (as JSON) for debugging
93
94     --lock-file=<file_name>
95         Lock file
96
97     --help
98         Show this help
99
100     Examples:
101
102         # To run all pending events.  This is what you tell CRON to run at
103         # regular intervals
104         perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
105
106         # To batch create all "checkout.due" events
107         perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
108
109 HELP
110 }
111
112
113 # create events for the specified hooks using the configured filters and context orgs
114 sub process_hooks {
115     return unless $opt_process_hooks;
116
117     my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : keys(%$hook_handlers);
118     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
119
120     for my $hook (@hooks) {
121     
122         my $config = $$hook_handlers{$hook} or next;
123         my $method = 'open-ils.trigger.passive.event.autocreate.batch';
124         $method =~ s/passive/active/ if $config->{active};
125         
126         my $req = $ses->request($method, $hook, $config->{context_org}, $config->{filter});
127         while(my $resp = $req->recv(timeout => 1800)) {
128             if($opt_debug_stdout) {
129                 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
130             }
131         }
132     }
133 }
134
135 sub run_pending {
136     return unless $opt_run_pending;
137     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
138     my $req = $ses->request('open-ils.trigger.event.run_all_pending');
139     while(my $resp = $req->recv(timeout => 600)) {
140         if($opt_debug_stdout) {
141             print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
142         }
143     }
144 }
145
146 help() and exit if $opt_help;
147 help() and exit unless ($opt_run_pending or $opt_hooks);
148
149 # check / set the lockfile
150 die "I'm already running with lockfile $opt_lockfile\n" if -e $opt_lockfile;
151 open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writing\n";
152 print F $$;
153 close F;
154
155 try {
156     osrf_connect($opt_osrf_config);
157     process_hooks();
158     run_pending();
159 } otherwise {
160     my $e = shift;
161     warn "$e\n";
162 };
163
164 unlink $opt_lockfile;
165
166
167