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;
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,
43 # typical passive hook filters
46 # default overdue circulations
48 context_org => 'circ_lib',
50 checkin_time => undef,
52 {stop_fines => ['MAXFINES', 'LONGOVERDUE']},
59 if ($opt_custom_filter) {
60 open FILTERS, $opt_custom_filter;
61 $hook_handlers = OpenSRF::Utils::JSON->JSON2perl(join('',(<FILTERS>)));
68 $0 : Create and process action/trigger events
70 --osrf-config=<config_file>
71 OpenSRF core config file. Defaults to:
72 /openils/conf/opensrf_core.xml
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
80 Run pending action_trigger.event's
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.
87 Print server responses to stdout (as JSON) for debugging
89 --lock-file=<file_name>
97 # To run all pending events. This is what you tell CRON to run at
99 perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
101 # To batch create all "checkout.due" events
102 perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
108 # create events for the specified hooks using the configured filters and context orgs
111 my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : keys(%$hook_handlers);
112 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
114 for my $hook (@hooks) {
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};
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";
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";
140 help() and exit if $opt_help;
141 help() and exit unless ($opt_run_pending or $opt_hooks);
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";
150 osrf_connect($opt_osrf_config);
158 unlink $opt_lockfile;