]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/action_trigger_runner.pl
757b1a50dc80a341857b9264d0a825901dddc939
[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     open FILTERS, $opt_custom_filter;
66     $hook_handlers = OpenSRF::Utils::JSON->JSON2perl(join('',(<FILTERS>)));
67     close FILTERS;
68 }
69
70 sub help {
71     print <<HELP;
72
73 $0 : Create and process action/trigger events
74
75     --osrf-config=<config_file>
76         OpenSRF core config file.  Defaults to:
77             /openils/conf/opensrf_core.xml
78
79     --custom-filters=<filter_file>
80         File containing a JSON Object which describes any hooks that should
81         use a user-defined filter to find their target objects.  Defaults to:
82             /openils/conf/action_trigger_filters.json
83
84     --run-pending
85         Run pending events
86
87     --process-hooks
88         Create hook events
89
90     --hooks=hook1[,hook2,hook3,...]
91         Define which hooks to create events for.  If none are defined,
92         it defaults to the list of hooks defined in the --custom-filters option.
93
94     --granularity=label
95         Run events with {label} granularity setting, or no granularity setting
96
97     --debug-stdout
98         Print server responses to stdout (as JSON) for debugging
99
100     --lock-file=<file_name>
101         Lock file
102
103     --help
104         Show this help
105
106     Examples:
107
108         # To run all pending events.  This is what you tell CRON to run at
109         # regular intervals
110         perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
111
112         # To batch create all "checkout.due" events
113         perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
114
115 HELP
116 }
117
118
119 # create events for the specified hooks using the configured filters and context orgs
120 sub process_hooks {
121     return unless $opt_process_hooks;
122
123     my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : keys(%$hook_handlers);
124     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
125
126     for my $hook (@hooks) {
127     
128         my $config = $$hook_handlers{$hook} or next;
129         my $method = 'open-ils.trigger.passive.event.autocreate.batch';
130         $method =~ s/passive/active/ if $config->{active};
131         
132         my $req = $ses->request($method, $hook, $config->{context_org}, $config->{filter}, $opt_granularity);
133         while(my $resp = $req->recv(timeout => 1800)) {
134             if($opt_debug_stdout) {
135                 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
136             }
137         }
138     }
139 }
140
141 sub run_pending {
142     return unless $opt_run_pending;
143     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
144     my $req = $ses->request('open-ils.trigger.event.run_all_pending' => $opt_granularity);
145
146     my $check_lockfile = 1;
147     while(my $resp = $req->recv(timeout => 7200)) {
148         if ($check_lockfile && -e $opt_lockfile) {
149             open LF, $opt_lockfile;
150             my $contents = <LF>;
151             close LF;
152             unlink $opt_lockfile if ($contents == $$);
153             $check_lockfile = 0;
154         }
155         if($opt_debug_stdout) {
156             print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
157         }
158     }
159 }
160
161 help() and exit if $opt_help;
162 help() and exit unless ($opt_run_pending or $opt_process_hooks);
163
164 # check / set the lockfile
165 die "I'm already running with lockfile $opt_lockfile\n" if -e $opt_lockfile;
166 open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writing\n";
167 print F $$;
168 close F;
169
170 try {
171         OpenSRF::System->bootstrap_client(config_file => $opt_osrf_config);
172         Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
173     process_hooks();
174     run_pending();
175 } otherwise {
176     my $e = shift;
177     warn "$e\n";
178 };
179
180 if (-e $opt_lockfile) {
181     open LF, $opt_lockfile;
182     my $contents = <LF>;
183     close LF;
184     unlink $opt_lockfile if ($contents == $$);
185 }
186