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 if (open FILTERS, $opt_custom_filter) {
66 $hook_handlers = OpenSRF::Utils::JSON->JSON2perl(join('',(<FILTERS>)));
69 die "Cannot read filter file '$opt_custom_filter'";
76 $0 : Create and process action/trigger events
78 --osrf-config=<config_file>
79 OpenSRF core config file. Defaults to:
80 /openils/conf/opensrf_core.xml
82 --custom-filters=<filter_file>
83 File containing a JSON Object which describes any hooks that should
84 use a user-defined filter to find their target objects. Defaults to:
85 /openils/conf/action_trigger_filters.json
93 --hooks=hook1[,hook2,hook3,...]
94 Define which hooks to create events for. If none are defined,
95 it defaults to the list of hooks defined in the --custom-filters option.
98 Run events with {label} granularity setting, or no granularity setting
101 Print server responses to stdout (as JSON) for debugging
103 --lock-file=<file_name>
111 # To run all pending events. This is what you tell CRON to run at
113 perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
115 # To batch create all "checkout.due" events
116 perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
122 # create events for the specified hooks using the configured filters and context orgs
124 return unless $opt_process_hooks;
126 my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : keys(%$hook_handlers);
127 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
129 for my $hook (@hooks) {
131 my $config = $$hook_handlers{$hook} or next;
132 my $method = 'open-ils.trigger.passive.event.autocreate.batch';
133 $method =~ s/passive/active/ if $config->{active};
135 my $req = $ses->request($method, $hook, $config->{context_org}, $config->{filter}, $opt_granularity);
136 while(my $resp = $req->recv(timeout => 1800)) {
137 if($opt_debug_stdout) {
138 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
145 return unless $opt_run_pending;
146 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
147 my $req = $ses->request('open-ils.trigger.event.run_all_pending' => $opt_granularity);
149 my $check_lockfile = 1;
150 while(my $resp = $req->recv(timeout => 7200)) {
151 if ($check_lockfile && -e $opt_lockfile) {
152 open LF, $opt_lockfile;
155 unlink $opt_lockfile if ($contents == $$);
158 if($opt_debug_stdout) {
159 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
164 help() and exit if $opt_help;
165 help() and exit unless ($opt_run_pending or $opt_process_hooks);
167 # check / set the lockfile
168 die "I'm already running with lockfile $opt_lockfile\n" if -e $opt_lockfile;
169 open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writing\n";
174 OpenSRF::System->bootstrap_client(config_file => $opt_osrf_config);
175 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
183 if (-e $opt_lockfile) {
184 open LF, $opt_lockfile;
187 unlink $opt_lockfile if ($contents == $$);