File Coverage

blib/lib/Plack/Middleware/LogHarakiri.pm
Criterion Covered Total %
statement 12 13 92.3
branch 6 8 75.0
condition 2 3 66.6
subroutine 2 2 100.0
pod 1 1 100.0
total 23 27 85.1


line stmt bran cond sub pod time code
1             package Plack::Middleware::LogHarakiri;
2             $Plack::Middleware::LogHarakiri::VERSION = '0.0200';
3 2     2   1318 use parent qw/ Plack::Middleware Process::SizeLimit::Core /;
  2         3  
  2         12  
4              
5             $Plack::Middleware::LogHarakiri::VERSION = '0.0200';
6              
7             =head1 NAME
8              
9             Plack::Middleware::LogHarakiri - log when a process is killed
10              
11             =for readme plugin version
12              
13             =head1 SYNOPSIS
14              
15             use Plack::Builder;
16              
17             builder {
18              
19             enable "LogHarakiri";
20             enable "SizeLimit", ...;
21              
22             $app;
23             };
24              
25             =begin :readme
26              
27             =head1 INSTALLATION
28              
29             See
30             L.
31              
32             =for readme plugin requires heading-level=2 title="Required Modules"
33              
34             =for readme plugin changes
35              
36             =end :readme
37              
38              
39             =head1 DESCRIPTION
40              
41             This middleware is a companion to L that
42             emits a warning when a process is killed.
43              
44             When it detects that the current process is killed, it will emit a
45             warning with diagnostic information of the form:
46              
47             pid %d committed harakiri (size: %d, shared: %d, unshared: %d) at %s
48              
49             Note that this middleware must be enabled before plugins that set the
50             "harakiri" flag.
51              
52             =head1 SEE ALSO
53              
54             L
55              
56             =head1 AUTHOR
57              
58             Robert Rothenberg C<< rrwo@thermeon.com >>
59              
60             =head1 COPYRIGHT
61              
62             Copyright 2015, Thermeon Worldwide, PLC.
63              
64             This library is free software; you can redistribute it and/or modify
65             it under the same terms as Perl itself.
66              
67             This program is distributed in the hope that it will be useful, but
68             without any warranty; without even the implied warranty of
69             merchantability or fitness for a particular purpose.
70              
71             =cut
72              
73             sub call {
74 4     4 1 64834 my ($self, $env) = @_;
75              
76 4         31 my $res = $self->app->($env);
77              
78 4 50 66     361 return $res
79             unless $env->{'psgix.harakiri'} or $env->{'psgix.harakiri.supported'};
80              
81 4 100       13 my $harakiri = $env->{'psgix.harakiri.supported'} ? # Legacy
82             $env->{'psgix.harakiri'} :
83             $env->{'psgix.harakiri.commit'};
84              
85 4 100       15 if ($harakiri) {
86 2         57 my $message = sprintf(
87             'pid %d committed harakiri (size: %d, shared: %d, unshared: %d) at %s',
88             $$, $self->_check_size, $env->{REQUEST_URI},
89             );
90 2 50       19732 if (my $logger = $env->{'psgix.logger'}) {
91 0         0 $logger->( { message => $message, level => 'warn' } );
92             }
93             else {
94 2         235 warn "$message\n";
95             }
96             }
97              
98 4         44 return $res;
99             };
100              
101             1;