line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Gearman::Server::Job; |
2
|
3
|
|
|
3
|
|
16
|
use version (); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
152
|
|
3
|
|
|
|
|
|
|
$Gearman::Server::Job::VERSION = version->declare("1.140_001"); |
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
15
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
78
|
|
6
|
3
|
|
|
3
|
|
14
|
use warnings; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
104
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Gearman::Server::Job - job representation of L |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 DESCRIPTION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 METHODS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=cut |
17
|
|
|
|
|
|
|
|
18
|
3
|
|
|
3
|
|
16
|
use Gearman::Server::Client; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
102
|
|
19
|
3
|
|
|
3
|
|
15
|
use Scalar::Util; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
179
|
|
20
|
3
|
|
|
3
|
|
1368
|
use Sys::Hostname; |
|
3
|
|
|
|
|
2878
|
|
|
3
|
|
|
|
|
270
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use fields ( |
23
|
3
|
|
|
|
|
21
|
'func', |
24
|
|
|
|
|
|
|
'uniq', |
25
|
|
|
|
|
|
|
'argref', |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# arrayref of interested Clients |
28
|
|
|
|
|
|
|
'listeners', |
29
|
|
|
|
|
|
|
'worker', |
30
|
|
|
|
|
|
|
'handle', |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# [1, 100] |
33
|
|
|
|
|
|
|
'status', |
34
|
|
|
|
|
|
|
'require_listener', |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Gearman::Server that owns us |
37
|
|
|
|
|
|
|
'server', |
38
|
3
|
|
|
3
|
|
22
|
); |
|
3
|
|
|
|
|
6
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub new { |
41
|
0
|
|
|
0
|
0
|
|
my Gearman::Server::Job $self = shift; |
42
|
0
|
|
|
|
|
|
my ($server, $func, $uniq, $argref, $highpri) = @_; |
43
|
|
|
|
|
|
|
|
44
|
0
|
0
|
|
|
|
|
$self = fields::new($self) unless ref $self; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# if they specified a uniq, see if we have a dup job running already |
47
|
|
|
|
|
|
|
# to merge with |
48
|
0
|
0
|
|
|
|
|
if (length($uniq)) { |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# a unique value of "-" means "use my args as my unique key" |
51
|
0
|
0
|
|
|
|
|
$uniq = $$argref if $uniq eq "-"; |
52
|
0
|
0
|
|
|
|
|
if (my $job = $server->job_of_unique($func, $uniq)) { |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# found a match |
55
|
0
|
|
|
|
|
|
return $job; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# create a new key |
59
|
0
|
|
|
|
|
|
$server->set_unique_job($func, $uniq => $self); |
60
|
|
|
|
|
|
|
} ## end if (length($uniq)) |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
$self->{'server'} = $server; |
63
|
0
|
|
|
|
|
|
$self->{'func'} = $func; |
64
|
0
|
|
|
|
|
|
$self->{'uniq'} = $uniq; |
65
|
0
|
|
|
|
|
|
$self->{'argref'} = $argref; |
66
|
0
|
|
|
|
|
|
$self->{'require_listener'} = 1; |
67
|
0
|
|
|
|
|
|
$self->{'listeners'} = []; |
68
|
0
|
|
|
|
|
|
$self->{'handle'} = $server->new_job_handle; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
$server->enqueue_job($self, $highpri); |
71
|
0
|
|
|
|
|
|
return $self; |
72
|
|
|
|
|
|
|
} ## end sub new |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 add_listener($client) |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub add_listener { |
79
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
80
|
0
|
|
|
|
|
|
my Gearman::Server::Client $li = shift; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
push @{ $self->{listeners} }, $li; |
|
0
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
Scalar::Util::weaken($self->{listeners}->[-1]); |
84
|
|
|
|
|
|
|
} ## end sub add_listener |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 relay_to_listeners($msg) |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub relay_to_listeners { |
91
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
92
|
0
|
|
|
|
|
|
foreach my Gearman::Server::Client $c (@{ $self->{listeners} }) { |
|
0
|
|
|
|
|
|
|
93
|
0
|
0
|
0
|
|
|
|
next if !$c || $c->{closed}; |
94
|
0
|
|
|
|
|
|
$c->write($_[0]); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} ## end sub relay_to_listeners |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 relay_to_option_listeners($msg, [$option]) |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub relay_to_option_listeners { |
103
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
104
|
0
|
|
|
|
|
|
my $option = $_[1]; |
105
|
0
|
|
|
|
|
|
foreach my Gearman::Server::Client $c (@{ $self->{listeners} }) { |
|
0
|
|
|
|
|
|
|
106
|
0
|
0
|
0
|
|
|
|
next if !$c || $c->{closed}; |
107
|
0
|
0
|
|
|
|
|
next unless $c->option($option); |
108
|
0
|
|
|
|
|
|
$c->write($_[0]); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
} ## end sub relay_to_option_listeners |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 clear_listeners() |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=cut |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub clear_listeners { |
118
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
119
|
0
|
|
|
|
|
|
$self->{listeners} = []; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 listeners() |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub listeners { |
127
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
128
|
0
|
|
|
|
|
|
return @{ $self->{listeners} }; |
|
0
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 uniq() |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub uniq { |
136
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
137
|
0
|
|
|
|
|
|
return $self->{uniq}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 note_finished($success) |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=cut |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub note_finished { |
145
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
146
|
0
|
|
|
|
|
|
my $success = shift; |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
$self->{server}->note_job_finished($self); |
149
|
|
|
|
|
|
|
} ## end sub note_finished |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 worker() |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# accessors: |
156
|
|
|
|
|
|
|
sub worker { |
157
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
158
|
0
|
0
|
|
|
|
|
return $self->{'worker'} unless @_; |
159
|
0
|
|
|
|
|
|
return $self->{'worker'} = shift; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 require_listener([$require]) |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub require_listener { |
167
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
168
|
0
|
0
|
|
|
|
|
return $self->{'require_listener'} unless @_; |
169
|
0
|
|
|
|
|
|
return $self->{'require_listener'} = shift; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 status([numerator,denominator]) |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# takes arrayref of [numerator,denominator] |
177
|
|
|
|
|
|
|
sub status { |
178
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
179
|
0
|
0
|
|
|
|
|
return $self->{'status'} unless @_; |
180
|
0
|
|
|
|
|
|
return $self->{'status'} = shift; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head2 handle() |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=cut |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub handle { |
188
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
189
|
0
|
|
|
|
|
|
return $self->{'handle'}; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 func() |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=cut |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub func { |
197
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
198
|
0
|
|
|
|
|
|
return $self->{'func'}; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 argref() |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=cut |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub argref { |
206
|
0
|
|
|
0
|
1
|
|
my Gearman::Server::Job $self = shift; |
207
|
0
|
|
|
|
|
|
return $self->{'argref'}; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
1; |