]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perlmods/OpenSRF/Application.pm
1dfeeaa58863856c6766e54c4f51ec3ba95773bd
[OpenSRF.git] / src / perlmods / OpenSRF / Application.pm
1 package OpenSRF::Application;
2 use base qw/OpenSRF/;
3 use OpenSRF::AppSession;
4 use OpenSRF::DomainObject::oilsMethod;
5 use OpenSRF::DomainObject::oilsResponse qw/:status/;
6 use OpenSRF::Utils::Logger qw/:level/;
7 use Time::HiRes qw/time/;
8 use vars qw/$_app $log %_METHODS/;
9 use OpenSRF::EX qw/:try/;
10 use strict;
11 use warnings;
12
13 $log = 'OpenSRF::Utils::Logger';
14
15 our $in_request = 0;
16 our @pending_requests;
17
18 sub application_implementation {
19         my $self = shift;
20         my $app = shift;
21
22         if (defined $app) {
23                 $_app = $app;
24                 eval "use $_app;";
25                 if( $@ ) {
26                         $log->error( "Error loading application_implementation: $app -> $@", ERROR);
27                 }
28
29         }
30
31         return $_app;
32 }
33
34 sub handler {
35         my ($self, $session, $app_msg) = @_;
36
37         $log->debug( "In Application::handler()", DEBUG );
38
39         my $app = $self->application_implementation;
40
41         if( $app ) {
42                 $log->debug( "Application is $app", DEBUG);
43         }
44         $log->debug( "Message is ".$app_msg->toString(1), INTERNAL);
45
46
47         if ($session->last_message_type eq 'REQUEST') {
48                 $log->debug( "We got a REQUEST: ". $app_msg->method, INFO );
49
50                 my $method_name = $app_msg->method;
51                 $log->debug( " * Looking up $method_name inside $app", DEBUG);
52
53                 my $method_proto = $session->last_message_protocol;
54                 $log->debug( " * Method API Level [$method_proto]", DEBUG);
55
56                 my $coderef = $app->method_lookup( $method_name, $method_proto );
57
58                 unless ($coderef) {
59                         $session->status( OpenSRF::DomainObject::oilsMethodException->new() );
60                         return 1;
61                 }
62
63                 $log->debug( " (we got coderef $coderef", DEBUG);
64
65                 unless ($session->continue_request) {
66                         $session->status(
67                                 OpenSRF::DomainObject::oilsConnectStatus->new(
68                                                 statusCode => STATUS_REDIRECTED(),
69                                                 status => 'Disconnect on max requests' ) );
70                         $session->kill_me;
71                         return 1;
72                 }
73
74                 if (ref $coderef) {
75                         my @args = $app_msg->params;
76                         my $appreq = OpenSRF::AppRequest->new( $session );
77
78                         $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", DEBUG );
79                         if( $in_request ) {
80                                 $log->debug( "Pushing onto pending requests: " . $appreq->threadTrace, DEBUG );
81                                 push @pending_requests, [ $appreq, \@args, $coderef ]; 
82                                 return 1;
83                         }
84
85
86                         $in_request++;
87
88                         $log->debug( "Executing coderef for {$method_name -> ".join(', ', @args)."}", INTERNAL );
89
90                         my $resp;
91                         try {
92                                 my $start = time();
93                                 $resp = $coderef->run( $appreq, @args); 
94                                 my $time = sprintf '%.3f', time() - $start;
95                                 $log->debug( "Method duration for {$method_name -> ".join(', ', @args)."}:  ". $time, DEBUG );
96                                 if( ref( $resp ) ) {
97                                         $log->debug( "Calling respond_complete: ". $resp->toString(), INTERNAL );
98                                         $appreq->respond_complete( $resp );
99                                 } else {
100                                         $appreq->status( OpenSRF::DomainObject::oilsConnectStatus->new(
101                                                                 statusCode => STATUS_COMPLETE(),
102                                                                 status => 'Request Complete' ) );
103                                 }
104                         } catch Error with {
105                                 my $e = shift;
106                                 $e = $e->{-text} || $e->message if (ref $e);
107                                 my $sess_id = $session->session_id;
108                                 $session->status(
109                                         OpenSRF::DomainObject::oilsMethodException->new(
110                                                         statusCode      => STATUS_INTERNALSERVERERROR(),
111                                                         status          => " *** Call to [$method_name] failed for session ".
112                                                                            "[$sess_id], thread trace [".$appreq->threadTrace."]:\n".$e
113                                         )
114                                 );
115                         };
116
117
118
119                         # ----------------------------------------------
120
121
122                         # XXX may need this later
123                         # $_->[1] = 1 for (@OpenSRF::AppSession::_CLIENT_CACHE);
124
125                         $in_request--;
126
127                         $log->debug( "Pending Requests: " . scalar(@pending_requests), INTERNAL );
128
129                         # cycle through queued requests
130                         while( my $aref = shift @pending_requests ) {
131                                 $in_request++;
132                                 my $resp;
133                                 try {
134                                         my $start = time;
135                                         my $response = $aref->[2]->run( $aref->[0], @{$aref->[1]} );
136                                         my $time = sprintf '%.3f', time - $start;
137                                         $log->debug( "Method duration for {[".$aref->[2]->name." -> ".join(', ',@{$aref->[1]}).'}:  '.$time, DEBUG );
138
139                                         $appreq = $aref->[0];   
140                                         if( ref( $response ) ) {
141                                                 $log->debug( "Calling respond_complete: ". $response->toString(), INTERNAL );
142                                                 $appreq->respond_complete( $response );
143                                         } else {
144                                                 $appreq->status( OpenSRF::DomainObject::oilsConnectStatus->new(
145                                                                         statusCode => STATUS_COMPLETE(),
146                                                                         status => 'Request Complete' ) );
147                                         }
148                                         $log->debug( "Executed: " . $appreq->threadTrace, DEBUG );
149                                 } catch Error with {
150                                         my $e = shift;
151                                         $session->status(
152                                                 OpenSRF::DomainObject::oilsMethodException->new(
153                                                                 statusCode => STATUS_INTERNALSERVERERROR(),
154                                                                 status => "Call to [".$aref->[2]->name."] faild:  ".$e->{-text}
155                                                 )
156                                         );
157                                 };
158                                 $in_request--;
159                         }
160
161                         return 1;
162                 } 
163                 my $res = OpenSRF::DomainObject::oilsMethodException->new;
164                 $session->send('ERROR', $res);
165                 $session->kill_me;
166                 return 1;
167
168         } else {
169                 $log->debug( "Pushing ". $app_msg->toString ." onto queue", INTERNAL );
170                 $session->push_queue([ $app_msg, $session->last_threadTrace ]);
171         }
172
173         $session->last_message_type('');
174         $session->last_message_protocol('');
175
176         return 1;
177 }
178
179 sub register_method {
180         my $self = shift;
181         my $app = ref($self) || $self;
182         my %args = @_;
183
184         throw OpenSRF::DomainObject::oilsMethodException unless ($args{method});
185         
186         $args{protocol} ||= 1;
187         $args{api_name} ||= $app . '.' . $args{method};
188         $args{code} ||= \&{$app . '::' . $args{method}};
189         
190         $_METHODS{$args{api_name}} = bless \%args => $app;
191 }
192
193
194 sub method_lookup {             
195         my $self = shift;
196         my $method = shift;
197         my $proto = shift;
198
199         my $super_lookup = $self->SUPER::method_lookup($method,$proto);
200         return $super_lookup if (ref $super_lookup);
201
202         my $class = ref($self) || $self;
203
204         $log->debug("Specialized lookup of [$method] in [$class]", INTERNAL);
205
206         if (exists $_METHODS{$method}) {
207                 return $_METHODS{$method} if ($_METHODS{$method}{protocol} == $proto);
208         }               
209
210         return undef; 
211 }
212
213 sub run {
214         my $self = shift;
215         $self->{code}->($self, @_);
216 }
217
218 1;