]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent.pm
TPac: Multiple holds in staff client place-holds session
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / AddedContent.pm
1 package OpenILS::WWW::AddedContent;
2 use strict; use warnings;
3
4 use lib qw(/usr/lib/perl5/Bundle/);
5
6 use CGI;
7 use Apache2::Log;
8 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
9 use APR::Const    -compile => qw(:error SUCCESS);
10 use Apache2::RequestRec ();
11 use Apache2::RequestIO ();
12 use Apache2::RequestUtil;
13 use Data::Dumper;
14 use UNIVERSAL::require;
15
16 use OpenSRF::EX qw(:try);
17 use OpenSRF::Utils::Cache;
18 use OpenSRF::System;
19 use OpenSRF::Utils::Logger qw/$logger/;
20
21 use LWP::UserAgent;
22 use MIME::Base64;
23
24 my $AC = __PACKAGE__;
25
26
27 # set the bootstrap config when this module is loaded
28 my $bs_config;
29
30 sub import {
31     my $self = shift;
32     $bs_config = shift;
33 }
34
35
36 my $handler; # added content handler class handle
37 my $cache; # memcache handle
38 my $net_timeout; # max seconds to wait for a response from the added content vendor
39 my $max_errors; # max consecutive lookup failures before added content is temporarily disabled
40 my $error_countdown; # current consecutive errors countdown
41
42 # number of seconds to wait before next lookup 
43 # is attempted after lookups have been disabled
44 my $error_retry_timeout;
45
46
47 sub child_init {
48
49     OpenSRF::System->bootstrap_client( config_file => $bs_config );
50     $cache = OpenSRF::Utils::Cache->new;
51
52     my $sclient = OpenSRF::Utils::SettingsClient->new();
53     my $ac_data = $sclient->config_value("added_content");
54
55     return unless $ac_data;
56     my $ac_handler = $ac_data->{module};
57     return unless $ac_handler;
58
59     $net_timeout = $ac_data->{timeout} || 1;
60     $error_countdown = $max_errors = $ac_data->{max_errors} || 10;
61     $error_retry_timeout = $ac_data->{retry_timeout} || 600;
62
63     $logger->debug("Attempting to load Added Content handler: $ac_handler");
64
65     $ac_handler->use;
66
67     if($@) {    
68         $logger->error("Unable to load Added Content handler [$ac_handler]: $@"); 
69         return; 
70     }
71
72     $handler = $ac_handler->new($ac_data);
73     $logger->debug("added content loaded handler: $handler");
74 }
75
76
77 sub handler {
78
79     my $r   = shift;
80     return Apache2::Const::DECLINED if (-e $r->filename);
81
82     my $cgi = CGI->new;
83     my $path = $r->path_info;
84     my $res;
85
86     my( undef, $type, $format, $key ) = split(/\//, $r->path_info);
87
88     child_init() unless $handler;
89
90     return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $key;
91
92     my $err;
93     my $data;
94     my $method = "${type}_${format}";
95
96     return Apache2::Const::NOT_FOUND unless $handler->can($method);
97     return $res if defined($res = $AC->serve_from_cache($type, $format, $key));
98     return Apache2::Const::NOT_FOUND unless $AC->lookups_enabled;
99
100     try {
101         $data = $handler->$method($key);
102     } catch Error with { 
103         $err = shift; 
104         decr_error_countdown();
105         $logger->debug("added content handler failed: $method($key) => $err");
106     };
107
108     return Apache2::Const::NOT_FOUND if $err;
109
110     if(!$data) {
111         # if the AC lookup found no corresponding data, cache that information
112         $logger->debug("added content handler returned no results $method($key)") unless $data;
113         $AC->cache_result($type, $format, $key, {nocontent=>1});
114         return Apache2::Const::NOT_FOUND;
115     }
116     
117     $AC->print_content($data);
118     $AC->cache_result($type, $format, $key, $data);
119
120     reset_error_countdown();
121     return Apache2::Const::OK;
122 }
123
124 sub print_content {
125     my($class, $data, $from_cache) = @_;
126     return Apache2::Const::NOT_FOUND if $data->{nocontent};
127
128     my $ct = $data->{content_type};
129     my $content = $data->{content};
130     print "Content-type: $ct\n\n";
131
132     if($data->{binary}) {
133         binmode STDOUT;
134         # if it hasn't been cached yet, it's still in binary form
135         print( ($from_cache) ? decode_base64($content) : $content );
136     } else {
137         print $content;
138     }
139
140
141     return Apache2::Const::OK;
142 }
143
144
145
146
147 # returns an HTPP::Response object
148 sub get_url {
149     my( $self, $url ) = @_;
150
151     $logger->info("added content getting [timeout=$net_timeout, errors_remaining=$error_countdown] URL = $url");
152     my $agent = LWP::UserAgent->new(timeout => $net_timeout);
153
154     my $res = $agent->get($url); 
155     $logger->info("added content request returned with code " . $res->code);
156     die "added content request failed: " . $res->status_line ."\n" unless $res->is_success;
157
158     return $res;
159 }
160
161 sub lookups_enabled {
162     if( $cache->get_cache('ac.no_lookup') ) {
163         $logger->info("added content lookup disabled");
164         return undef;
165     }
166     return 1;
167 }
168
169 sub disable_lookups {
170     $cache->put_cache('ac.no_lookup', 1, $error_retry_timeout);
171 }
172
173 sub decr_error_countdown {
174     $error_countdown--;
175     if($error_countdown < 1) {
176         $logger->warn("added content error count exhausted.  Disabling lookups for $error_retry_timeout seconds");
177         $AC->disable_lookups;
178     }
179 }
180
181 sub reset_error_countdown {
182     $error_countdown = $max_errors;
183 }
184
185 sub cache_result {
186     my($class, $type, $format, $key, $data) = @_;
187     $logger->debug("caching $type/$format/$key");
188     $data->{content} = encode_base64($data->{content}) if $data->{binary};
189     return $cache->put_cache("ac.$type.$format.$key", $data);
190 }
191
192 sub serve_from_cache {
193     my($class, $type, $format, $key) = @_;
194     my $data = $cache->get_cache("ac.$type.$format.$key");
195     return undef unless $data;
196     $logger->debug("serving $type/$format/$key from cache");
197     return $class->print_content($data, 1);
198 }
199
200
201
202 1;