File Coverage

blib/lib/DBIx/Class/QueryLog/NotifyOnMax.pm
Criterion Covered Total %
statement 15 15 100.0
branch 2 2 100.0
condition 5 6 83.3
subroutine 5 5 100.0
pod 3 3 100.0
total 30 31 96.7


line stmt bran cond sub pod time code
1             package DBIx::Class::QueryLog::NotifyOnMax;
2             $DBIx::Class::QueryLog::NotifyOnMax::VERSION = '1.005001';
3 1     1   78229 use Moo;
  1         8847  
  1         4  
4              
5             extends 'DBIx::Class::QueryLog';
6              
7             has _max_count => (
8             is => 'ro',
9 1     1   16 builder => sub { 1_000 },
10             init_arg => 'max_count',
11             );
12              
13             has notified => ( is => 'rw' );
14              
15             sub notify {
16 2     2 1 5 my $self = shift;
17              
18 2         5 my $max = $self->_max_count;
19 2         35 warn "Max query count ($max) exceeded; did you forget to ->reset your query logger?";
20 2         19 $self->notified(1);
21             }
22              
23             sub query_end {
24 4000     4000 1 361832 my ($self, @rest) = @_;
25              
26 4000         53825 my $had_cur = $self->current_query;
27              
28 4000         23586 $self->next::method(@rest);
29              
30 4000 100 66     81289 $self->notify
      100        
31             if defined $had_cur &&
32             !$self->notified &&
33             $self->count > $self->_max_count
34             }
35              
36             sub reset {
37 1     1 1 1212 my ($self, @rest) = @_;
38              
39 1         5 $self->next::method(@rest);
40              
41 1         797 $self->notified(undef);
42             }
43              
44             1;
45              
46             __END__
47              
48             =pod
49              
50             =encoding UTF-8
51              
52             =head1 NAME
53              
54             DBIx::Class::QueryLog::NotifyOnMax
55              
56             =head1 VERSION
57              
58             version 1.005001
59              
60             =head1 SYNOPSIS
61              
62             my $schema = ... # Get your schema!
63             my $ql = DBIx::Class::QueryLog::NotifyOnMax->new(
64             max_count => 100,
65             );
66             $schema->storage->debugobj($ql);
67             $schema->storage->debug(1);
68             ... # get warning when you do more than 100 queries
69              
70             =head1 DESCRIPTION
71              
72             More than once I've run into memory leaks that are caused by the user using
73             L<DBIx::Class::QueryLog> and forgetting to call L<DBIx::Class::QueryLog/reset>.
74             This subclass of C<DBIx::Class::QueryLog> simply warns after C<1_000> queries
75             have gone through it. If you want to do something more complex, subclasses
76             which override the L</notify> method are a good idea.
77              
78             =head1 METHODS
79              
80             =head2 new
81              
82             Overridden version of L<DBIx::Class::QueryLog/new>, simply adds an optional
83             C<max_count> parameter which defaults to C<1_000>.
84              
85             =head2 notify
86              
87             This is the method that runs when the C<max_count> has been exceeded. Takes no
88             parameters. Make sure to call C<< $self->notified(1) >> if you want the event
89             to only take place once after the threshold has been exceeded.
90              
91             =head1 AUTHORS
92              
93             =over 4
94              
95             =item *
96              
97             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
98              
99             =item *
100              
101             Cory G Watson <gphat at cpan.org>
102              
103             =back
104              
105             =head1 COPYRIGHT AND LICENSE
106              
107             This software is copyright (c) 2015 by Cory G Watson <gphat at cpan.org>.
108              
109             This is free software; you can redistribute it and/or modify it under
110             the same terms as the Perl 5 programming language system itself.
111              
112             =cut