Lock.pm untested

filter: Statementstmt 0.0% 0 / 24 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 51 CC 3 SCAR 13.9
CC
3
Cov
0%
set
6.0
unset
6.0
new
2.0
CRAP 4.0
1 package Proc::Lock;
2  
3 $Proc::Lock::VERSION = (split " ", '# $Id: Lock.pm,v 1.5 2000/09/21 14:14:20 mkul Exp $ ')[3];
4  
5 =head1 NAME
6  
7 Proc::Lock - lock interface module
8  
9 =head1 SYNOPSIS
10  
11 use Proc::Lock;
12 my $lock = new Proc::Lock ( ... );
13 $lock->set ();
14 die "locked" if $lock->isSet ();
15 $lock->unset ();
16  
17 =head1 DESCRIPTION
18  
19 Generic lock module. You must subclass this class and overwrite set, unset, isSet and new (possible) for do your work
20  
21 =cut
22  
23 =head2 new
24  
25 Construct new lock object. You can setup parameters:
26  
27 ProcessName => $name of process for make lock file name
28 Pid => $ pid of process for make lock file name
29 HostName => $ name of host for make lock file name
30 Wait => wait for lock is clean and continue
31 Timeout => limit of time of wait of lock clean
32 NoUnset => not unset lock at unset operation
33  
34 my $lock = new Proc::Lock... ( ProcessName => $0,
35 Pid => $$,
36 Hostname => gethostname(),
37 Wait => 1,
38 NoUnset => 1,
39 Timeout => 30 );
40  
41 $lock->set || die "already runned";
42  
43 =cut
44  
45 sub new
46 {
47 my ( $class, %params ) = @_;
48 $params{processname} || die "Your must setup ProcessName for lock";
49 foreach my $key ( keys %params ) { $params{ lc $key } = delete $params{$key}; }
50 bless { %params }, $class;
51 }
52  
53 =head2 set
54  
55 setup lock. Return true if success
56  
57 =cut
58  
59 sub set
60 {
61 my $this = shift;
62 eval
63 {
64 local $SIG{ALRM} = sub { die "lock timeout" };
65 alarm ( $this->{timeout} ) if ( $this->{timeout} );
66 $this->_set ();
67 };
68 }
69  
70 =head2 unset
71  
72 unset lock, return true if success
73  
74 =cut
75  
76 sub unset
77 {
78 my $this = shift;
79 return 1 if $this->{nounset};
80 $this->_unset();
81 }
82  
83 =head2 isSet
84  
85 return true if lock is up
86  
87 =cut
88  
89 sub isSet
90 {
91 my $this = shift;
92 $this->_isSet();
93 }
94  
95 =head2 DESTROY
96  
97 destroy lock object
98  
99 =cut
100  
101 sub DESTROY
102 {
103 my $this = shift || return;
104 $this->unset();
105 }
106  
107 =head2 log
108  
109 Return log object
110  
111 =cut
112  
113 sub log
114 {
115 my $this = shift;
116 my $log = ( $this->{log} ||= new Log::Dispatch );
117 $log;
118 }
119  
120 =head2 _set _unset _isSet
121  
122 private methods, You must put your functionality to this methods.
123 The real methods ( set, unset, isSet ) call this method.
124  
125 =cut
126  
127 1;
128  
129 __END__