File Coverage

blib/lib/Plack/Middleware/LogHarakiri.pm
Criterion Covered Total %
statement 18 19 94.7
branch 6 8 75.0
condition 2 3 66.6
subroutine 4 4 100.0
pod 1 1 100.0
total 31 35 88.5


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