line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package IPC::Concurrency::DBI::Application::Instance; |
2
|
|
|
|
|
|
|
|
3
|
21
|
|
|
21
|
|
28679
|
use warnings; |
|
21
|
|
|
|
|
30
|
|
|
21
|
|
|
|
|
791
|
|
4
|
21
|
|
|
21
|
|
99
|
use strict; |
|
21
|
|
|
|
|
35
|
|
|
21
|
|
|
|
|
544
|
|
5
|
|
|
|
|
|
|
|
6
|
21
|
|
|
21
|
|
1104
|
use Data::Dumper; |
|
21
|
|
|
|
|
13279
|
|
|
21
|
|
|
|
|
1104
|
|
7
|
21
|
|
|
21
|
|
103
|
use Carp; |
|
21
|
|
|
|
|
31
|
|
|
21
|
|
|
|
|
9582
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
IPC::Concurrency::DBI::Application::Instance - Application instance that represents consumption of the limited resource. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 VERSION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Version 1.2.0 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '1.2.0'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
This module represents one instance of an application managed by |
27
|
|
|
|
|
|
|
IPC::Concurrency::DBI. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
See the documentation of IPC::Concurrency::DBI for more information. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $instance = $concurrent_program->start_instance(); |
32
|
|
|
|
|
|
|
unless ( defined( $instance ) ) |
33
|
|
|
|
|
|
|
{ |
34
|
|
|
|
|
|
|
print "Too many instances of $0 are already running.\n"; |
35
|
|
|
|
|
|
|
exit; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# [...] Do some work. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Now that the application is about to exit, flag the instance as completed. |
41
|
|
|
|
|
|
|
# (note: this is implicit when $instance is destroyed). |
42
|
|
|
|
|
|
|
$instance->finish(); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 METHODS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 new() |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Create a new IPC::Concurrency::DBI::Application::Instance object. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
This function should not be called directly and its API could change, instead |
52
|
|
|
|
|
|
|
use IPC::Concurrency::DBI::Application::start_instance(). |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Retrieve the application by name. |
55
|
|
|
|
|
|
|
my $instance = IPC::Concurrency::DBI::Application::Instance->new( |
56
|
|
|
|
|
|
|
application => $application, |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
'application': mandatory, an IPC::Concurrency::DBI::Application object. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub new |
64
|
|
|
|
|
|
|
{ |
65
|
8
|
|
|
8
|
1
|
3944
|
my ( $class, %args ) = @_; |
66
|
8
|
|
|
|
|
25
|
my $application = delete( $args{'application'} ); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Check parameters. |
69
|
8
|
100
|
|
|
|
38
|
croak "Argument 'application' is required to create a new IPC::Concurrency::DBI::Application::Instance object" |
70
|
|
|
|
|
|
|
unless defined( $application ); |
71
|
7
|
100
|
|
|
|
37
|
croak "Argument 'application' is not an IPC::Concurrency::DBI::Application" |
72
|
|
|
|
|
|
|
if !Data::Validate::Type::is_instance( $application, class => 'IPC::Concurrency::DBI::Application' ); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Create the object. |
75
|
6
|
|
|
|
|
192
|
my $self = bless( |
76
|
|
|
|
|
|
|
{ |
77
|
|
|
|
|
|
|
application => $application, |
78
|
|
|
|
|
|
|
finished => 0, |
79
|
|
|
|
|
|
|
}, |
80
|
|
|
|
|
|
|
$class, |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
|
83
|
6
|
|
|
|
|
33
|
return $self; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 finish() |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Declare that the current instance has finished running and free the slot for |
90
|
|
|
|
|
|
|
a new instance. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub finish |
95
|
|
|
|
|
|
|
{ |
96
|
6
|
|
|
6
|
1
|
33
|
my ( $self ) = @_; |
97
|
6
|
|
|
|
|
18
|
my $application = $self->get_application(); |
98
|
6
|
|
|
|
|
33
|
my $database_handle = $application->get_database_handle(); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# If the object has already been destroyed, we have a problem. |
101
|
|
|
|
|
|
|
croak 'The instance has already been marked as finished' |
102
|
6
|
50
|
|
|
|
25
|
if $self->{'finished'}; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Decrement the count of running instances, provided that it's > 0. |
105
|
|
|
|
|
|
|
# We should never encounter the case that would make it go negative, but |
106
|
|
|
|
|
|
|
# being careful never hurts. |
107
|
6
|
|
|
|
|
35
|
my $rows_affected = $database_handle->do( |
108
|
|
|
|
|
|
|
q| |
109
|
|
|
|
|
|
|
UPDATE ipc_concurrency_applications |
110
|
|
|
|
|
|
|
SET current_instances = current_instances - 1, modified = ? |
111
|
|
|
|
|
|
|
WHERE ipc_concurrency_application_id = ? |
112
|
|
|
|
|
|
|
AND current_instances > 0 |
113
|
|
|
|
|
|
|
|, |
114
|
|
|
|
|
|
|
{}, |
115
|
|
|
|
|
|
|
time(), |
116
|
|
|
|
|
|
|
$application->get_id(), |
117
|
|
|
|
|
|
|
); |
118
|
6
|
50
|
|
|
|
61542
|
croak 'Cannot execute SQL: ' . $database_handle->errstr() |
119
|
|
|
|
|
|
|
if defined( $database_handle->errstr() ); |
120
|
|
|
|
|
|
|
|
121
|
6
|
|
|
|
|
21
|
$self->{'finished'} = 1; |
122
|
|
|
|
|
|
|
|
123
|
6
|
|
|
|
|
19
|
return 1; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 INTERNAL METHODS |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 get_application() |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Returns the parent IPC::Concurrency::DBI::Application object. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
my $application = $instance->get_application(); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub get_application |
138
|
|
|
|
|
|
|
{ |
139
|
7
|
|
|
7
|
1
|
44
|
my ( $self ) = @_; |
140
|
|
|
|
|
|
|
|
141
|
7
|
|
|
|
|
27
|
return $self->{'application'}; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 DESTROY() |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Automatically clear the slot used by the current instance when the object |
148
|
|
|
|
|
|
|
is destroyed, if finish() has not been called already. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub DESTROY |
153
|
|
|
|
|
|
|
{ |
154
|
6
|
|
|
6
|
|
2706
|
my( $self ) = @_; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
$self->finish() |
157
|
6
|
100
|
|
|
|
51
|
unless $self->{'finished'}; |
158
|
|
|
|
|
|
|
|
159
|
6
|
50
|
|
|
|
70
|
$self->SUPER::DESTROY() |
160
|
|
|
|
|
|
|
if $self->can( 'SUPER::DESTROY' ); |
161
|
|
|
|
|
|
|
|
162
|
6
|
|
|
|
|
667
|
return; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 BUGS |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Please report any bugs or feature requests through the web interface at |
169
|
|
|
|
|
|
|
L. |
170
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
171
|
|
|
|
|
|
|
your bug as I make changes. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 SUPPORT |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
perldoc IPC::Concurrency::DBI |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
You can also look for information at: |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=over 4 |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item * GitHub's request tracker |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
L |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
L |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=item * CPAN Ratings |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
L |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item * MetaCPAN |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
L |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=back |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 AUTHOR |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
L, |
207
|
|
|
|
|
|
|
C<< >>. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Copyright 2011-2017 Guillaume Aubert. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
This code is free software; you can redistribute it and/or modify it under the |
215
|
|
|
|
|
|
|
same terms as Perl 5 itself. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY |
218
|
|
|
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
219
|
|
|
|
|
|
|
PARTICULAR PURPOSE. See the LICENSE file for more details. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=cut |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
1; |