]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/action_trigger_runner.pl
general purpose action/trigger event creator and runner. see --help for usage
[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 require 'oils_header.pl';
22
23 my $opt_lockfile = '/tmp/action-trigger-LOCK';
24 my $opt_osrf_config = '/openils/conf/opensrf_core.xml';
25 my $opt_run_pending = 0;
26 my $opt_debug_stdout = 0;
27 my $opt_help = 0;
28 my $opt_custom_filter;
29 my $opt_hooks;
30
31 GetOptions(
32     'osrf-config=s' => \$opt_osrf_config,
33     'run-pending' => \$opt_run_pending,
34     'hooks=s' => \$opt_hooks,
35     'debug-stdout' => \$opt_debug_stdout,
36     'custom-filter=s' => \$opt_custom_filter,
37     'lock-file=s' => \$opt_lockfile,
38     'help' => \$opt_help,
39 );
40
41
42 # typical passive hook filters
43 my %hook_handlers = (
44
45     # overdue circulations
46     'checkout.due' => {
47         context_org => 'circ_lib',
48         filter => {
49             checkin_time => undef, 
50             '-or' => [
51                 {stop_fines => ['MAXFINES', 'LONGOVERDUE']}, 
52                 {stop_fines => undef}
53             ]
54         }
55     }
56 );
57
58 sub help {
59     print <<HELP;
60
61 $0 : Create and process action/trigger events
62
63     --osrf-config <config_file>
64         OpenSRF config file
65
66     --run-pending
67         Run pending action_trigger.event's
68
69     --hooks hook1[,hook2,hook3,...]
70         hooks to generate events for
71
72     --debug-stdout
73         Print server responses to stdout (as JSON) for debugging
74
75     --lock-file <file_name>
76         Lock file
77
78     --help
79         Show this help
80
81     Examples:
82
83         # To run all pending events.  This is what you tell CRON to run at regular intervals
84         perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
85
86         # To batch create all "checkout.due" events
87         perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
88
89 HELP
90 }
91
92
93 # create events for the specified hooks using the configured filters and context orgs
94 sub process_hooks {
95
96     my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : ();
97     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
98
99     for my $hook (@hooks) {
100     
101         my $config = $hook_handlers{$hook} or next;
102         my $method = 'open-ils.trigger.passive.event.autocreate.batch';
103         $method =~ s/passive/active/ if $config->{active};
104         
105         my $filter = ($opt_custom_filter) ? OpenSRF::Utils::JSON->JSON2Perl($opt_custom_filter) : $config->{filter};
106     
107         my $req = $ses->request($method, $hook, $config->{context_org}, $filter);
108         while(my $resp = $req->recv(timeout => 1800)) {
109             if($opt_debug_stdout) {
110                 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
111             }
112         }
113     }
114 }
115
116 sub run_pending {
117     return unless $opt_run_pending;
118     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
119     my $req = $ses->request('open-ils.trigger.event.run_all_pending');
120     while(my $resp = $req->recv(timeout => 600)) {
121         if($opt_debug_stdout) {
122             print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
123         }
124     }
125 }
126
127 help() and exit if $opt_help;
128 help() and exit unless ($opt_run_pending or $opt_hooks);
129
130 # check / set the lockfile
131 die "I'm already running with lockfile $opt_lockfile\n" if -e $opt_lockfile;
132 open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writing\n";
133 print F $$;
134 close F;
135
136 eval {
137     osrf_connect($opt_osrf_config);
138     process_hooks();
139     run_pending();
140 };
141
142 warn "$@\n" if $@;
143
144 unlink $opt_lockfile;
145
146
147