File Coverage

blib/lib/Etcd/Lock.pm
Criterion Covered Total %
statement 8 27 29.6
branch 0 6 0.0
condition 0 3 0.0
subroutine 3 7 42.8
pod 0 3 0.0
total 11 46 23.9


line stmt bran cond sub pod time code
1             package Etcd::Lock;
2             $Etcd::Lock::VERSION = '0.02';
3 2     2   164639 use 5.012;
  2         30  
4              
5 2     2   1124 use Net::Etcd;
  2         555644  
  2         62  
6 2     2   794 use boolean;
  2         1847  
  2         8  
7              
8             sub new {
9 0     0 0   my $c = shift;
10 0           my %a = @_;
11 0           my %b;
12 0           $b{etcd} = Net::Etcd->new( { host => $a{host} } );
13 0           foreach (qw/host key/) {
14 0           $b{$_} = $a{$_};
15             }
16 0           return bless \%b, $c;
17             }
18              
19             sub lock () {
20 0     0 0   my $s = shift;
21 0           return $s->_lock_unlock( true );
22             }
23              
24             sub unlock () {
25 0     0 0   my $s = shift;;
26 0           return $s->_lock_unlock( false );
27             }
28              
29             sub _lock_unlock ( ) {
30 0     0     my $s = shift;
31 0           my $nval = shift;
32 0           my $k = $s->{key};
33              
34 0           my $val = $s->{etcd}->range( { key => $k } )->get_value;
35 0 0         return $val unless defined $nval;
36 0 0 0       return false if defined $val && $val eq $nval;
37             $nval
38             ? $s->{etcd}->put( { key => $k, value => $nval } )
39 0 0         : $s->{etcd}->deleterange( { key => $k } );
40 0           return true;
41             }
42              
43             1;
44              
45             =pod
46              
47             =head1 NAME
48              
49             Etcd::Lock - Lock based on etcd
50              
51             =for html

52            
53             github workflow tests
54            
55             Top language:
56             github last commit
57            

58              
59             =head1 VERSION
60              
61             version 0.02
62              
63             =head1 SYNOPSIS
64              
65             use Etcd::Lock
66              
67             my $etcdLock = Etcd::Lock->new(host => 'host.name.com', key => 'lock_key');
68              
69             =head1 DESCRIPTION
70              
71             Etcd::Lock is a lock based on etcd
72              
73             =encoding UTF-8
74              
75             =head1 BUGS/CONTRIBUTING
76              
77             Please report any bugs through the web interface at L
78              
79             If you want to contribute changes or otherwise involve yourself in development, feel free to fork the Git repository from
80             L.
81              
82             =head1 SUPPORT
83              
84             You can find this documentation with the perldoc command too.
85              
86             perldoc etcd-lock
87              
88             =head1 AUTHOR
89              
90             Emiliano Bruni
91              
92             =head1 COPYRIGHT AND LICENSE
93              
94             This software is copyright (c) 2022 by Emiliano Bruni.
95              
96             This is free software; you can redistribute it and/or modify it under
97             the same terms as the Perl 5 programming language system itself.
98              
99             =cut
100              
101             __END__