csv notify fetching script
authorBill Erickson <berick@esilibrary.com>
Thu, 6 Dec 2012 21:47:09 +0000 (16:47 -0500)
committerBill Erickson <berick@esilibrary.com>
Thu, 6 Dec 2012 21:47:09 +0000 (16:47 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/support-scripts/csv_notify_fetcher.pl [new file with mode: 0755]
Open-ILS/src/support-scripts/csv_notify_pusher.pl

diff --git a/Open-ILS/src/support-scripts/csv_notify_fetcher.pl b/Open-ILS/src/support-scripts/csv_notify_fetcher.pl
new file mode 100755 (executable)
index 0000000..b033a26
--- /dev/null
@@ -0,0 +1,85 @@
+#!/usr/bin/perl
+use strict; 
+use warnings;
+use DateTime;
+use OpenSRF::AppSession;
+use OpenILS::Utils::CStoreEditor;
+use OpenILS::Utils::Cronscript;
+use OpenILS::Utils::Fieldmapper;
+use OpenSRF::Utils::Logger qw/$logger/;
+binmode(STDOUT, ":utf8");
+
+my %defaults = (
+    'osrf-config=s' => '/openils/conf/opensrf_core.xml',
+    'response-file=s' => '',
+    'verbose'       => 0
+);
+
+sub help {
+    print <<HELP;
+
+Collect CSV notification output and deliver to 3rd party
+
+# fetch notification status/response file
+$0 
+
+HELP
+    exit;
+}
+
+my $o = OpenILS::Utils::Cronscript->new(\%defaults);
+my $opts = $o->MyGetOptions();
+$o->bootstrap;
+
+my $file = $opts->{'response-file'};
+open(FILE, $file) or
+    die "Unable to open response file: '$file' : $!\n";
+    
+my $e = $o->editor(connect => 1);
+
+while (<FILE>) {
+    chomp;
+
+    my ($id, $stat) = /"(.+)","(.+)"/g;
+    next unless $id and $stat;
+
+    $logger->info("csv: processing event $id; stat $stat");
+
+    my $event = $e->retrieve_action_trigger_event($id);
+
+    if (!$event) {
+        $logger->warn("csv: unable to find event $id");
+        next;
+    }
+
+    if ($event->async_output) {
+        $logger->info("csv: skipping event $id; async_output already set");
+        next;
+    }
+
+    $e->xact_begin;
+
+    # store the response output
+    my $output = Fieldmapper::action_trigger::event_output->new;
+    $output->data($stat);
+
+    unless ($e->create_action_trigger_event_output($output)) {
+        $logger->warn("csv: error creating event ".
+            "output for event $id: ". $e->die_event);
+        next;
+    }
+
+    # link the async response output to the original event
+    $event->async_output($output->id);
+
+    unless ($e->update_action_trigger_event($event)) {
+        $logger->warn("csv: error updating event $id: ". $e->die_event);
+        next;
+    }
+
+    $e->xact_commit;
+}
+
+$e->disconnect;
+
+
index 8f14746..9cfef0e 100755 (executable)
@@ -42,6 +42,8 @@ my $end_date = $opts->{'end-date'} || '';
 
 my @event_defs = split(/,/, $opts->{'event-defs'});
 
+die "--event-defs required\n" unless @event_defs;
+
 if ($verbose) {
     print "processing event_defs = @event_defs\n";
     print "start_date = $start_date\n";
@@ -85,6 +87,10 @@ print "\n";
 
 # dump the CSV chunk from each event output
 while (my $resp = $req->recv(timeout => 12000)) {
+    if ($req->failed) {
+        warn $req->failed;
+        last;
+    };
     my $content = $resp->content or next;
     print $content->{data};
 }