File Coverage

blib/lib/PawsX/Waiter.pm
Criterion Covered Total %
statement 26 27 96.3
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 35 37 94.5


line stmt bran cond sub pod time code
1             package PawsX::Waiter;
2              
3 2     2   279555 use strict;
  2         11  
  2         59  
4 2     2   11 use warnings;
  2         6  
  2         47  
5              
6 2     2   1024 use Moose::Role;
  2         825456  
  2         11  
7 2     2   13352 use JSON;
  2         21701  
  2         12  
8 2     2   2043 use Path::Tiny;
  2         22278  
  2         121  
9 2     2   1056 use PawsX::Waiter::Client;
  2         8  
  2         532  
10              
11             our $VERSION = "0.03";
12              
13             sub GetWaiter {
14 3     3 1 12792 my ( $self, $waiter ) = @_;
15              
16 3         21 my $version = $self->version;
17 3         29 my $waiter_file = path(__FILE__)->parent->child('Waiter/waiters.json');
18              
19 3         586 my $service = lc $self->service;
20 3         31 my $definition = $waiter_file->slurp();
21 3         5382 my $waiter_struct = JSON->new->utf8(1)->decode($definition);
22              
23 3 50       48 if ( my $config = $waiter_struct->{$service}->{$version}->{$waiter} ) {
24             return PawsX::Waiter::Client->new(
25             client => $self,
26             delay => $config->{'delay'},
27             maxAttempts => $config->{'maxAttempts'},
28             operation => $config->{'operation'},
29 3         61 acceptors => $config->{'acceptors'},
30             );
31             }
32              
33 0           die "Invalid waiter: " . $waiter;
34             }
35              
36             1;
37             __END__
38              
39             =encoding utf-8
40              
41             =head1 NAME
42            
43             PawsX::Waiter - A Waiter library for Paws
44              
45             =head1 SYNOPSIS
46              
47             use PawsX::Waiter;
48              
49             my $client = Paws->new(
50             config => {
51             region => 'ap-south-1'
52             }
53             );
54              
55             my $service = $client->service('ELB');
56              
57             # Apply waiter role to Paws class
58             PawsX::Waiter->meta->apply($service);
59             my $response = $service->RegisterInstancesWithLoadBalancer(
60             LoadBalancerName => 'test-elb',
61             Instances => [ { InstanceId => 'i-0xxxxx' } ]
62             );
63              
64             my $waiter = $service->GetWaiter('InstanceInService');
65             $waiter->wait({
66             LoadBalancerName => 'test-elb',
67             Instances => [ { InstanceId => 'i-0xxxxx' } ],
68             });
69            
70             =head1 DESCRIPTION
71              
72             Waiters are utility methods that poll for a particular state to occur on a client. Waiters can fail after a number of attempts at a polling interval defined for the service client.
73              
74             =head1 METHODS
75              
76             =head2 GetWaiter
77              
78             my $waiter = $service->GetWaiter('InstanceInService');
79            
80             This method returns a new PawsX::Waiter object and It has the following attributes. You can configure the waiter behaviour with this.
81              
82             =head3 delay(Int)
83            
84             $waiter->delay(10);
85            
86             Number of seconds to delay between polling attempts. Each waiter has a default delay configuration value, but you may need to modify this setting for specific use cases.
87              
88             =head3 maxAttempts(Int)
89            
90             $waiter->maxAttempts(100);
91            
92             Maximum number of polling attempts to issue before failing the waiter. Each waiter has a default maxAttempts configuration value,
93             but you may need to modify this setting for specific use cases.
94              
95             =head4 beforeWait(CodeRef)
96              
97             $waiter->beforeWait(sub {
98             my ($w, $attempts, $response) = @_;
99             say STDERR "Waiter attempts left:" . ( $w->maxAttempts - $attempts );
100             });
101              
102             Register a callback that is invoked after an attempt but before sleeping. provides the number of attempts made and the previous response.
103            
104             =head3 wait(HashRef)
105            
106             $waiter->wait({
107             LoadBalancerName => 'test-elb',
108             Instances => [ { InstanceId => 'i-0xxxxx' } ],
109             });
110              
111             Block until the waiter completes or fails.Note that this might throw a PawsX::Exception::* if the waiter fails.
112              
113             =head1 SEE ALSO
114              
115             =over 4
116              
117             =item L<Paws>
118              
119             =back
120              
121             =head1 AUTHOR
122              
123             Prajith Ndz E<lt>prajithpalakkuda@gmail.comE<gt>
124              
125             =head1 COPYRIGHT
126              
127             Copyright (C) Prajith Ndz.
128              
129             =head1 LICENSE
130              
131             This library is free software; you can redistribute it and/or modify
132             it under the same terms as Perl itself.
133              
134             =cut