line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Locker; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
41499
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
79
|
|
4
|
3
|
|
|
3
|
|
1566
|
use Storable; |
|
3
|
|
|
|
|
7179
|
|
|
3
|
|
|
|
|
157
|
|
5
|
3
|
|
|
3
|
|
1179
|
use Convert::Base64; |
|
3
|
|
|
|
|
2888
|
|
|
3
|
|
|
|
|
131
|
|
6
|
3
|
|
|
3
|
|
1371
|
use IO::Handle; |
|
3
|
|
|
|
|
13423
|
|
|
3
|
|
|
|
|
1131
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
App::Locker - lock/unlock you perl script |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use App::Locker; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $locker=App::Locker->create; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $pid=fork(); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
if (!$pid){ |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# child |
23
|
|
|
|
|
|
|
sleep(1); |
24
|
|
|
|
|
|
|
$locker->unlock; |
25
|
|
|
|
|
|
|
sleep(1); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
} else { |
28
|
|
|
|
|
|
|
# parent |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
print "LOCK\n"; |
31
|
|
|
|
|
|
|
$locker->lock(); |
32
|
|
|
|
|
|
|
print "UNLOCK\n"; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
This module provides create lock point in any place scipt and unlock it from another script place (main, fork, thread). |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
$Storable::Deparse = 1; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head2 create |
44
|
|
|
|
|
|
|
my $locker=App::Locker->create; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
create main object thith lock api |
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub create{ |
50
|
2
|
|
|
2
|
1
|
68
|
my ($class, %params)=@_; |
51
|
|
|
|
|
|
|
|
52
|
2
|
|
|
|
|
4
|
my ($reader, $writer); |
53
|
2
|
|
|
|
|
32
|
pipe($reader, $writer); |
54
|
2
|
|
|
|
|
16
|
$writer->autoflush(1); |
55
|
|
|
|
|
|
|
|
56
|
2
|
|
|
|
|
84
|
my $self={ |
57
|
|
|
|
|
|
|
reader=>$reader, |
58
|
|
|
|
|
|
|
writer=>$writer |
59
|
|
|
|
|
|
|
}; |
60
|
|
|
|
|
|
|
|
61
|
2
|
|
|
|
|
6
|
bless($self, $class); |
62
|
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
4
|
return $self; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 destroy |
67
|
|
|
|
|
|
|
$locker->destroy |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
destroy main object |
70
|
|
|
|
|
|
|
=cut |
71
|
|
|
|
|
|
|
sub destroy{ |
72
|
0
|
|
|
0
|
1
|
0
|
my ($self)=@_; |
73
|
0
|
|
|
|
|
0
|
my $reader=$self->{reader}; |
74
|
0
|
|
|
|
|
0
|
my $writer=$self->{writer}; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
0
|
close $reader; |
77
|
0
|
|
|
|
|
0
|
close $writer; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
0
|
delete $self->{reader}; |
80
|
0
|
|
|
|
|
0
|
delete $self->{writer}; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub dpack{ |
85
|
1
|
|
|
1
|
0
|
8
|
my ($data)=@_; |
86
|
|
|
|
|
|
|
|
87
|
1
|
|
|
|
|
8
|
my $ret; |
88
|
1
|
50
|
|
|
|
32
|
if ($data){ |
89
|
1
|
|
|
|
|
38
|
$ret=encode_base64(Storable::freeze($data)); |
90
|
1
|
|
|
|
|
191
|
$ret=~s/[\r\n]//gs; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
3
|
return $ret; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub dunpack{ |
97
|
1
|
|
|
1
|
0
|
4
|
my ($data)=@_; |
98
|
1
|
|
|
|
|
22
|
$data=~s/[\r\n]//gs; |
99
|
1
|
50
|
|
|
|
25
|
my $ret=Storable::thaw(decode_base64($data)) if $data; |
100
|
|
|
|
|
|
|
|
101
|
1
|
|
|
|
|
25
|
return $ret; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 lock |
105
|
|
|
|
|
|
|
$locker->lock # for simple lock |
106
|
|
|
|
|
|
|
OR |
107
|
|
|
|
|
|
|
my $data = $locker->lock # lock thith transfer any data |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
lock execute script |
110
|
|
|
|
|
|
|
=cut |
111
|
|
|
|
|
|
|
sub lock{ |
112
|
1
|
|
|
1
|
1
|
880
|
my ($self)=@_; |
113
|
|
|
|
|
|
|
|
114
|
1
|
|
|
|
|
21
|
my $reader=$self->{reader}; |
115
|
1
|
|
|
|
|
1000303
|
my $ret=<$reader>; |
116
|
|
|
|
|
|
|
|
117
|
1
|
|
|
|
|
12
|
my $data=dunpack($ret); |
118
|
|
|
|
|
|
|
|
119
|
1
|
|
|
|
|
5
|
return $data; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 unlock |
123
|
|
|
|
|
|
|
$locker->unlock # for simple unlock |
124
|
|
|
|
|
|
|
OR |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
$locker->unlock($data) # unlock thith transfer any data ($data - must by reference) |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
lock execute script |
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
sub unlock{ |
131
|
1
|
|
|
1
|
1
|
1000751
|
my ($self, $data)=@_; |
132
|
|
|
|
|
|
|
|
133
|
1
|
50
|
33
|
|
|
37
|
if (!ref($data) && $data){ |
134
|
0
|
|
|
|
|
0
|
print "Error: Sended data may be reference not simple SCALAR\n"; |
135
|
|
|
|
|
|
|
} |
136
|
1
|
|
|
|
|
37
|
my $writer=$self->{writer}; |
137
|
|
|
|
|
|
|
|
138
|
1
|
|
|
|
|
18
|
my $send=dpack($data); |
139
|
1
|
50
|
|
|
|
8
|
if ($send){ |
140
|
1
|
|
|
|
|
43
|
print $writer "$send\n"; |
141
|
|
|
|
|
|
|
} else { |
142
|
0
|
|
|
|
|
|
print $writer "\n"; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 AUTHOR |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Bulichev Evgeniy, F>. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 COPYRIGHT |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Copyright (c) 2017 Bulichev Evgeniy. All rights reserved. |
155
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it |
156
|
|
|
|
|
|
|
under the same terms as Perl itself. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
1; |