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 # ---------------------------------------------------------------
19 use OpenSRF::AppSession;
20 use OpenSRF::Utils::JSON;
21 use OpenSRF::EX qw(:try);
22 require 'oils_header.pl';
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;
31 my $opt_process_hooks = 0;
32 my $opt_granularity = undef;
35 'osrf-config=s' => \$opt_osrf_config,
36 'run-pending' => \$opt_run_pending,
37 'hooks=s' => \$opt_hooks,
38 'granularity=s' => \$opt_granularity,
39 'process-hooks' => \$opt_process_hooks,
40 'debug-stdout' => \$opt_debug_stdout,
41 'custom-filters=s' => \$opt_custom_filter,
42 'lock-file=s' => \$opt_lockfile,
47 # typical passive hook filters
50 # default overdue circulations
52 context_org => 'circ_lib',
54 checkin_time => undef,
56 {stop_fines => ['MAXFINES', 'LONGOVERDUE']},
63 if ($opt_custom_filter) {
64 open FILTERS, $opt_custom_filter;
65 $hook_handlers = OpenSRF::Utils::JSON->JSON2perl(join('',(<FILTERS>)));
72 $0 : Create and process action/trigger events
74 --osrf-config=<config_file>
75 OpenSRF core config file. Defaults to:
76 /openils/conf/opensrf_core.xml
78 --custom-filters=<filter_file>
79 File containing a JSON Object which describes any hooks that should
80 use a user-defined filter to find their target objects. Defaults to:
81 /openils/conf/action_trigger_filters.json
89 --hooks=hook1[,hook2,hook3,...]
90 Define which hooks to create events for. If none are defined,
91 it defaults to the list of hooks defined in the --custom-filters option.
94 Run events with {label} granularity setting, or no granularity setting
97 Print server responses to stdout (as JSON) for debugging
99 --lock-file=<file_name>
107 # To run all pending events. This is what you tell CRON to run at
109 perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
111 # To batch create all "checkout.due" events
112 perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
118 # create events for the specified hooks using the configured filters and context orgs
120 return unless $opt_process_hooks;
122 my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : keys(%$hook_handlers);
123 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
125 for my $hook (@hooks) {
127 my $config = $$hook_handlers{$hook} or next;
128 my $method = 'open-ils.trigger.passive.event.autocreate.batch';
129 $method =~ s/passive/active/ if $config->{active};
131 my $req = $ses->request($method, $hook, $config->{context_org}, $config->{filter}, $opt_granularity);
132 while(my $resp = $req->recv(timeout => 1800)) {
133 if($opt_debug_stdout) {
134 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
141 return unless $opt_run_pending;
142 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
143 my $req = $ses->request('open-ils.trigger.event.run_all_pending' => $opt_granularity);
145 my $check_lockfile = 1;
146 while(my $resp = $req->recv(timeout => 7200)) {
147 if ($check_lockfile && -e $opt_lockfile) {
148 open LF, $opt_lockfile;
151 unlink $opt_lockfile if ($contents == $$);
154 if($opt_debug_stdout) {
155 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
160 help() and exit if $opt_help;
161 help() and exit unless ($opt_run_pending or $opt_process_hooks);
163 # check / set the lockfile
164 die "I'm already running with lockfile $opt_lockfile\n" if -e $opt_lockfile;
165 open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writing\n";
170 osrf_connect($opt_osrf_config);
178 if (-e $opt_lockfile) {
179 open LF, $opt_lockfile;
182 unlink $opt_lockfile if ($contents == $$);