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 require 'oils_header.pl';
23 my $opt_lockfile = '/tmp/action-trigger-LOCK';
24 my $opt_osrf_config = '/openils/conf/opensrf_core.xml';
25 my $opt_run_pending = 0;
26 my $opt_debug_stdout = 0;
28 my $opt_custom_filter;
32 'osrf-config=s' => \$opt_osrf_config,
33 'run-pending' => \$opt_run_pending,
34 'hooks=s' => \$opt_hooks,
35 'debug-stdout' => \$opt_debug_stdout,
36 'custom-filter=s' => \$opt_custom_filter,
37 'lock-file=s' => \$opt_lockfile,
42 # typical passive hook filters
45 # overdue circulations
47 context_org => 'circ_lib',
49 checkin_time => undef,
51 {stop_fines => ['MAXFINES', 'LONGOVERDUE']},
61 $0 : Create and process action/trigger events
63 --osrf-config <config_file>
67 Run pending action_trigger.event's
69 --hooks hook1[,hook2,hook3,...]
70 hooks to generate events for
73 Print server responses to stdout (as JSON) for debugging
75 --lock-file <file_name>
83 # To run all pending events. This is what you tell CRON to run at regular intervals
84 perl $0 --osrf-config /openils/conf/opensrf_core.xml --run-pending
86 # To batch create all "checkout.due" events
87 perl $0 --osrf-config /openils/conf/opensrf_core.xml --hooks checkout.due
93 # create events for the specified hooks using the configured filters and context orgs
96 my @hooks = ($opt_hooks) ? split(',', $opt_hooks) : ();
97 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
99 for my $hook (@hooks) {
101 my $config = $hook_handlers{$hook} or next;
102 my $method = 'open-ils.trigger.passive.event.autocreate.batch';
103 $method =~ s/passive/active/ if $config->{active};
105 my $filter = ($opt_custom_filter) ? OpenSRF::Utils::JSON->JSON2Perl($opt_custom_filter) : $config->{filter};
107 my $req = $ses->request($method, $hook, $config->{context_org}, $filter);
108 while(my $resp = $req->recv(timeout => 1800)) {
109 if($opt_debug_stdout) {
110 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
117 return unless $opt_run_pending;
118 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
119 my $req = $ses->request('open-ils.trigger.event.run_all_pending');
120 while(my $resp = $req->recv(timeout => 600)) {
121 if($opt_debug_stdout) {
122 print OpenSRF::Utils::JSON->perl2JSON($resp->content) . "\n";
127 help() and exit if $opt_help;
128 help() and exit unless ($opt_run_pending or $opt_hooks);
130 # check / set the lockfile
131 die "I'm already running with lockfile $opt_lockfile\n" if -e $opt_lockfile;
132 open(F, ">$opt_lockfile") or die "Unable to open lockfile $opt_lockfile for writing\n";
137 osrf_connect($opt_osrf_config);
144 unlink $opt_lockfile;