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