2 # ---------------------------------------------------------------
3 # Copyright (C) 2012 Equinox Software, Inc
4 # Author: Bill Erickson <berick@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 # ---------------------------------------------------------------
21 use OpenSRF::AppSession;
22 use OpenILS::Utils::CStoreEditor;
23 use OpenILS::Utils::RemoteAccount;
24 use OpenILS::Utils::Fieldmapper;
26 my $osrf_config = '/openils/conf/opensrf_core.xml';
27 my $start_date = DateTime->now->strftime('%F');
31 my $local_dir = '/tmp'; # where to keep local copies of generated files
33 my $cleanup = 0; # cleanup generated files
38 'osrf-config=s' => \$osrf_config,
39 'start-date=s' => \$start_date,
40 'end-date=s' => \$end_date,
41 'event-defs=s' => \$event_defs,
42 'output-file=s' => \$output_file,
43 'remote-acct=s' => \$remote_acct,
44 'local-dir=s' => \$local_dir,
45 'cleanup' => \$cleanup,
46 'verbose' => \$verbose,
53 Action/Trigger Aggregator Script
55 Collect template output from one or more event-definitions and stitch the
56 results together in a single file. The file may be optionally stored locally
57 and/or delivered to a remote ftp/scp account.
59 This script is useful for generating a single mass notification (e.g. overdue)
60 file and sending the file to a 3rd party for processing and/or for local
61 processing. The anticipated use case would be to stitch together lines of CSV
66 # Collect a week of notifications for 3 event definitions
69 --event-defs 104,105,106 \
70 --start-date 2012-01-01 \
71 --end-date 2012-01-07 \
72 --output-file 2012-01-07.notify.csv \
73 --local-dir /var/run/evergreen/csv \
79 action_trigger.event_definition IDs
82 Only collect output for events whose run_time is on or after this ISO date
85 Only collect output for events whose run_time occurred before this IDO date
87 --output-file [default STDOUT]
88 Output goes to this file.
90 --local-dir [default /tmp]
91 Local directory where the output-file is placed. If --cleanup is
92 set, this is used as the tmp directory for file generation
95 Evergreen config.remote_account ID
96 If set, the output-file will be sent via sFTP/SCP to this server.
104 my @event_defs = split(/,/, $event_defs);
105 die "--event-defs required\n" unless @event_defs;
107 my $local_file = $output_file ? "$local_dir/$output_file" : '&STDOUT';
109 open(OUTFILE, ">$local_file") or
110 die "unable to open out-file '$local_file' for writing: $!\n";
111 binmode(OUTFILE, ":utf8");
113 OpenSRF::System->bootstrap_client(config_file => $osrf_config);
114 Fieldmapper->import(IDL =>
115 OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
116 OpenILS::Utils::CStoreEditor::init();
117 my $editor = OpenILS::Utils::CStoreEditor->new;
120 $date_filter{run_time} = {'>=' => $start_date} if $start_date;
121 $date_filter{run_time} = {'<' => $end_date} if $end_date;
123 # collect the event tempate output data
124 # use a real session here so we can stream results directly to the output file
125 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
126 my $req = $ses->request(
127 'open-ils.cstore.json_query', {
128 select => {ateo => ['data']},
129 from => {ateo => { atev => {
130 filter => {state => 'complete', %date_filter},
131 join => {atevdef => {filter => {
139 # use a large timeout since this is likely to be a hefty query
140 while (my $resp = $req->recv(timeout => 3600)) {
141 die $req->failed . "\n" if $req->failed;
142 my $content = $resp->content or next;
143 print OUTFILE $content->{data};
147 # send the file to the remote account
149 my $racct = $editor->retrieve_config_remote_account($remote_acct);
150 die "No such remote account $remote_acct" unless $racct;
153 my $host = $racct->host;
154 ($host =~ s/^(S?FTP)://i and $type = uc($1)) or
155 ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
158 my $acct = OpenILS::Utils::RemoteAccount->new(
160 remote_host => $host,
161 account_object => $racct,
162 local_file => $local_file,
163 remote_file => $output_file
166 my $res = $acct->put;
168 die "Unable to push to remote server [$remote_acct] : " .
169 $acct->error . "\n" unless $res;
171 print "Pushed file to $res\n" if $verbose;
175 unlink($local_file) or
176 die "Unable to clean up file '$local_file' : $!\n";