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.0201';
3 2     2   1696 use parent qw/ Plack::Middleware Process::SizeLimit::Core /;
  2         5  
  2         16  
4              
5 2     2   52948 use strict;
  2         5  
  2         61  
6 2     2   16 use warnings;
  2         3  
  2         385  
7              
8             $Plack::Middleware::LogHarakiri::VERSION = '0.0201';
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 is a companion to L that
45             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             Note that this middleware must be enabled before plugins that set the
53             "harakiri" flag.
54              
55             =head1 SEE ALSO
56              
57             L
58              
59             =head1 AUTHOR
60              
61             Robert Rothenberg C<< rrwo@thermeon.com >>
62              
63             =head1 COPYRIGHT
64              
65             Copyright 2015, Thermeon Worldwide, PLC.
66              
67             This library is free software; you can redistribute it and/or modify
68             it under the same terms as Perl itself.
69              
70             This program is distributed in the hope that it will be useful, but
71             without any warranty; without even the implied warranty of
72             merchantability or fitness for a particular purpose.
73              
74             =cut
75              
76             sub call {
77 4     4 1 48013 my ($self, $env) = @_;
78              
79 4         31 my $res = $self->app->($env);
80              
81 4 50 66     327 return $res
82             unless $env->{'psgix.harakiri'} or $env->{'psgix.harakiri.supported'};
83              
84 4 100       16 my $harakiri = $env->{'psgix.harakiri.supported'} ? # Legacy
85             $env->{'psgix.harakiri'} :
86             $env->{'psgix.harakiri.commit'};
87              
88 4 100       13 if ($harakiri) {
89 2         18 my $message = sprintf(
90             'pid %d committed harakiri (size: %d, shared: %d, unshared: %d) at %s',
91             $$, $self->_check_size, $env->{REQUEST_URI},
92             );
93 2 50       20458 if (my $logger = $env->{'psgix.logger'}) {
94 0         0 $logger->( { message => $message, level => 'warn' } );
95             }
96             else {
97 2         178 warn "$message\n";
98             }
99             }
100              
101 4         40 return $res;
102             };
103              
104             1;