File Coverage

blib/lib/Net/Stomp/MooseHelpers/ReconnectOnFailure.pm
Criterion Covered Total %
statement 41 45 91.1
branch 4 8 50.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 56 64 87.5


line stmt bran cond sub pod time code
1             package Net::Stomp::MooseHelpers::ReconnectOnFailure;
2             $Net::Stomp::MooseHelpers::ReconnectOnFailure::VERSION = '2.9';
3             {
4             $Net::Stomp::MooseHelpers::ReconnectOnFailure::DIST = 'Net-Stomp-MooseHelpers';
5             }
6 2     2   4084 use Moose::Role;
  2         4  
  2         14  
7 2     2   7996 use Net::Stomp::MooseHelpers::Exceptions;
  2         4  
  2         54  
8 2     2   1066 use MooseX::Types::Common::Numeric qw(PositiveNum);
  2         58239  
  2         14  
9 2     2   4063 use Carp;
  2         3  
  2         154  
10 2     2   11 use Try::Tiny;
  2         2  
  2         90  
11 2     2   874 use Time::HiRes 'sleep';
  2         1358  
  2         17  
12 2     2   395 use namespace::autoclean;
  2         4  
  2         23  
13              
14             # ABSTRACT: provide a reconnect-on-failure wrapper method
15              
16              
17             has connect_retry_delay => (
18             is => 'ro',
19             isa => PositiveNum,
20             default => 15,
21             );
22              
23             requires 'connect';
24             requires 'clear_connection';
25             requires '_set_disconnected';
26              
27              
28             sub reconnect_on_failure {
29 2     2 1 2831 my ($self,$method,@args) = @_;
30              
31 2         48 my $wantarray=wantarray;
32 2         4 my @ret;my $done_it=0;
  2         2  
33              
34 2         6 while (!$done_it) {
35             try {
36 10     10   553 $self->connect;
37              
38 2 50       40 if ($wantarray) {
    50          
39 0         0 @ret = $self->$method(@args);
40             }
41             elsif (defined $wantarray) {
42 0         0 @ret = scalar $self->$method(@args);
43             }
44             else {
45 2         7 $self->$method(@args);
46             }
47 2         5 $done_it = 1;
48             }
49             catch {
50 8     8   178 my $err = $_;
51              
52             {
53 8         9 local $"=', ';
  8         18  
54 8         24 local $Carp::CarpInternal{'Try::Tiny'}=1;
55 8         129 carp "connection problems calling ${self}->${method}(@args): $err; reconnecting";
56             }
57 8         804 $self->clear_connection;
58 8         383 $self->_set_disconnected;
59 8         167 sleep($self->connect_retry_delay);
60 10         157 };
61             }
62              
63 2 50       35 if ($wantarray) { return @ret }
  0 50       0  
64 0         0 elsif (defined $wantarray) { return $ret[0] }
65 2         26 else { return };
66             }
67              
68             1;
69              
70             __END__
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             Net::Stomp::MooseHelpers::ReconnectOnFailure - provide a reconnect-on-failure wrapper method
79              
80             =head1 VERSION
81              
82             version 2.9
83              
84             =head1 SYNOPSIS
85              
86             package MyThing;
87             use Moose;
88             with 'Net::Stomp::MooseHelpers::CanConnect';
89             with 'Net::Stomp::MooseHelpers::ReconnectOnFailure';
90              
91             sub foo {
92             my ($self) = @_;
93              
94             $self->reconnect_on_failure('connect');
95              
96             # do something
97             }
98              
99             =head1 DESCRIPTION
100              
101             This role wraps the logic shown in the synopsis for
102             L<Net::Stomp::MooseHelpers::CanConnect> into a simple wrapper method.
103              
104             Just call L</reconnect_on_failure> passing the method name (or a
105             coderef) and all the arguments. See below for details.
106              
107             =head1 ATTRIBUTES
108              
109             =head2 C<connect_retry_delay>
110              
111             How many seconds to wait between connection attempts. Defaults to 15.
112              
113             =head1 METHODS
114              
115             =head2 C<reconnect_on_failure>
116              
117             $self->reconnect_on_failure($method,@args);
118              
119             C<$method> can be a method name or a coderef (anything that you'd
120             write just after C<< $self-> >>). C<@args> are passed untouched.
121              
122             First of all, this calls C<< $self->connect() >>, then it calls C<<
123             $self->$method(@args) >>, returning whatever it returns (preserves
124             context).
125              
126             If an exception is raised, warns about it, sleeps for
127             L</connect_retry_delay> seconds, then tries again.
128              
129             =head1 AUTHOR
130              
131             Gianni Ceccarelli <gianni.ceccarelli@net-a-porter.com>
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             This software is copyright (c) 2014 by Net-a-porter.com.
136              
137             This is free software; you can redistribute it and/or modify it under
138             the same terms as the Perl 5 programming language system itself.
139              
140             =cut