2 # ---------------------------------------------------------------
3 # Copyright (C) 2009 Equinox Software, Inc
4 # Author: Bill Erickson <erickson@esilibrary.com>
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.
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 # ---------------------------------------------------------------
20 use OpenSRF::AppSession;
21 use OpenSRF::Utils::JSON;
22 use OpenSRF::EX qw(:try);
23 use OpenILS::Utils::Fieldmapper;
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;
32 my $opt_process_hooks = 0;
33 my $opt_granularity = undef;
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,
48 # typical passive hook filters
51 # default overdue circulations
53 context_org => 'circ_lib',
55 checkin_time => undef,
57 {stop_fines => ['MAXFINES', 'LONGOVERDUE']},
64 if ($opt_custom_filter) {
65 open FILTERS, $opt_custom_filter;
66 $hook_handlers = OpenSRF::Utils::JSON->JSON2perl(join('',(<FILTERS>)));
73 $0 : Create and process action/trigger events
75 --osrf-config=<config_file>
76 OpenSRF core config file. Defaults to:
77 /openils/conf/opensrf_core.xml
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
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.
95 Run events with {label} granularity setting, or no granularity setting
98 Print server responses to stdout (as JSON) for debugging
100 --lock-file=<file_name>
108 # To run all pending events. This is what you tell CRON to run at
110 perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
112 # To batch create all "checkout.due" events
113 perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
119 # create events for the specified hooks using the configured filters and context orgs
121 return unless $opt_process_hooks;
123 my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : keys(%$hook_handlers);
124 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
126 for my $hook (@hooks) {
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};
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";
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);
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;
152 unlink $opt_lockfile if ($contents == $$);
155 if($opt_debug_stdout) {
156 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
161 help() and exit if $opt_help;
162 help() and exit unless ($opt_run_pending or $opt_process_hooks);
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";
171 OpenSRF::System->bootstrap_client(config_file => $opt_osrf_config);
172 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
180 if (-e $opt_lockfile) {
181 open LF, $opt_lockfile;
184 unlink $opt_lockfile if ($contents == $$);