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;
34 'osrf-config=s' => \$opt_osrf_config,
35 'run-pending' => \$opt_run_pending,
36 'hooks=s' => \$opt_hooks,
37 'process-hooks' => \$opt_process_hooks,
38 'debug-stdout' => \$opt_debug_stdout,
39 'custom-filters=s' => \$opt_custom_filter,
40 'lock-file=s' => \$opt_lockfile,
45 # typical passive hook filters
48 # default overdue circulations
50 context_org => 'circ_lib',
52 checkin_time => undef,
54 {stop_fines => ['MAXFINES', 'LONGOVERDUE']},
61 if ($opt_custom_filter) {
62 open FILTERS, $opt_custom_filter;
63 $hook_handlers = OpenSRF::Utils::JSON->JSON2perl(join('',(<FILTERS>)));
70 $0 : Create and process action/trigger events
72 --osrf-config=<config_file>
73 OpenSRF core config file. Defaults to:
74 /openils/conf/opensrf_core.xml
76 --custom-filters=<filter_file>
77 File containing a JSON Object which describes any hooks that should
78 use a user-defined filter to find their target objects. Defaults to:
79 /openils/conf/action_trigger_filters.json
87 --hooks=hook1[,hook2,hook3,...]
88 Define which hooks to create events for. If none are defined,
89 it defaults to the list of hooks defined in the --custom-filters option.
92 Print server responses to stdout (as JSON) for debugging
94 --lock-file=<file_name>
102 # To run all pending events. This is what you tell CRON to run at
104 perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
106 # To batch create all "checkout.due" events
107 perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
113 # create events for the specified hooks using the configured filters and context orgs
115 return unless $opt_process_hooks;
117 my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : keys(%$hook_handlers);
118 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
120 for my $hook (@hooks) {
122 my $config = $$hook_handlers{$hook} or next;
123 my $method = 'open-ils.trigger.passive.event.autocreate.batch';
124 $method =~ s/passive/active/ if $config->{active};
126 my $req = $ses->request($method, $hook, $config->{context_org}, $config->{filter});
127 while(my $resp = $req->recv(timeout => 1800)) {
128 if($opt_debug_stdout) {
129 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
136 return unless $opt_run_pending;
137 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
138 my $req = $ses->request('open-ils.trigger.event.run_all_pending');
139 while(my $resp = $req->recv(timeout => 600)) {
140 if($opt_debug_stdout) {
141 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
146 help() and exit if $opt_help;
147 help() and exit unless ($opt_run_pending or $opt_process_hooks);
149 # check / set the lockfile
150 die "I'm already running with lockfile $opt_lockfile\n" if -e $opt_lockfile;
151 open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writing\n";
156 osrf_connect($opt_osrf_config);
164 unlink $opt_lockfile;