| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
=head1 NAME |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Gearman::Spawner::Client::Sync - synchronous client for Gearman::Spawner::Worker workers |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
$client = Gearman::Spawner::Client::Sync->new( |
|
8
|
|
|
|
|
|
|
job_servers => ['localhost:4730'] |
|
9
|
|
|
|
|
|
|
); |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
eval { |
|
12
|
|
|
|
|
|
|
my $result = $client->run_method( |
|
13
|
|
|
|
|
|
|
class => 'MyWorker', |
|
14
|
|
|
|
|
|
|
method => 'sing', |
|
15
|
|
|
|
|
|
|
arg => [qw( do re mi )], |
|
16
|
|
|
|
|
|
|
); |
|
17
|
|
|
|
|
|
|
say "success! result is $result"; |
|
18
|
|
|
|
|
|
|
}; |
|
19
|
|
|
|
|
|
|
if ($@) { |
|
20
|
|
|
|
|
|
|
say "failed because $@"; |
|
21
|
|
|
|
|
|
|
} |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=cut |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
package Gearman::Spawner::Client::Sync; |
|
26
|
|
|
|
|
|
|
|
|
27
|
11
|
|
|
11
|
|
9471
|
use strict; |
|
|
11
|
|
|
|
|
23
|
|
|
|
11
|
|
|
|
|
370
|
|
|
28
|
11
|
|
|
11
|
|
56
|
use warnings; |
|
|
11
|
|
|
|
|
12
|
|
|
|
11
|
|
|
|
|
332
|
|
|
29
|
|
|
|
|
|
|
|
|
30
|
11
|
|
|
11
|
|
8002
|
use Gearman::Client; |
|
|
11
|
|
|
|
|
246290
|
|
|
|
11
|
|
|
|
|
315
|
|
|
31
|
11
|
|
|
11
|
|
229
|
use base 'Gearman::Client'; |
|
|
11
|
|
|
|
|
26
|
|
|
|
11
|
|
|
|
|
1921
|
|
|
32
|
|
|
|
|
|
|
|
|
33
|
11
|
|
|
11
|
|
69
|
use Carp qw( croak ); |
|
|
11
|
|
|
|
|
15
|
|
|
|
11
|
|
|
|
|
695
|
|
|
34
|
11
|
|
|
11
|
|
7118
|
use Gearman::Spawner::Util; |
|
|
11
|
|
|
|
|
43
|
|
|
|
11
|
|
|
|
|
272
|
|
|
35
|
11
|
|
|
11
|
|
78
|
use Storable qw( nfreeze thaw ); |
|
|
11
|
|
|
|
|
16
|
|
|
|
11
|
|
|
|
|
6137
|
|
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 METHODS |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=over 4 |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=item Gearman::Spawner::Client::Sync->new(%options) |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Creates a new client object. Options: |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=over 4 |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=item job_servers |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
(Required) Arrayref of servers to connect to. |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=back |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=cut |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub new { |
|
56
|
2
|
|
|
2
|
1
|
730
|
my $ref = shift; |
|
57
|
2
|
|
33
|
|
|
31
|
my $class = ref $ref || $ref; |
|
58
|
|
|
|
|
|
|
|
|
59
|
2
|
|
|
|
|
78
|
my Gearman::Spawner::Client::Sync $self = fields::new($class)->SUPER::new(@_); |
|
60
|
|
|
|
|
|
|
|
|
61
|
2
|
|
|
|
|
18547
|
return $self; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item $client->run_method(%options) |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Dispatches a foreground job to a worker. |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Returns the deserialized result. If an error occurs, an exception is thrown. |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Options: |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=over 4 |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item class |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
(Required) The name of the worker class. |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item method |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
(Required) The name of the method in I to call. |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item data |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
(Optional) The job-specific data to pass to the worker. Any structure that can |
|
85
|
|
|
|
|
|
|
be serialized with Storable is allowed. If omitted, undef is sent. |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item unique |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
(Optional) The opaque unique tag for coalescing jobs. |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=back |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub run_method { |
|
96
|
21
|
|
|
21
|
1
|
17773
|
my Gearman::Spawner::Client::Sync $self = shift; |
|
97
|
21
|
|
|
|
|
165
|
my %params = @_; |
|
98
|
|
|
|
|
|
|
|
|
99
|
21
|
|
33
|
|
|
438
|
my $class = delete $params{class} || croak "need class"; |
|
100
|
21
|
|
33
|
|
|
302
|
my $method = delete $params{method} || croak "need method"; |
|
101
|
21
|
|
100
|
|
|
159
|
my $data = delete $params{data} || undef; |
|
102
|
21
|
|
50
|
|
|
111
|
my $unique = delete $params{unique} || undef; |
|
103
|
|
|
|
|
|
|
|
|
104
|
21
|
50
|
|
|
|
135
|
croak "unknown parameters to run_method: @{[%params]}" if %params; |
|
|
0
|
|
|
|
|
0
|
|
|
105
|
|
|
|
|
|
|
|
|
106
|
21
|
|
|
|
|
38
|
my %options; |
|
107
|
21
|
50
|
|
|
|
68
|
$options{uniq} = $unique if defined $unique; |
|
108
|
|
|
|
|
|
|
|
|
109
|
21
|
|
|
|
|
95
|
my $function = Gearman::Spawner::Util::method2function($class, $method); |
|
110
|
|
|
|
|
|
|
|
|
111
|
21
|
|
|
|
|
153
|
my $serialized = nfreeze([$data]); |
|
112
|
|
|
|
|
|
|
|
|
113
|
21
|
|
|
|
|
1331
|
my $ref_to_frozen_retval = $self->do_task($function => $serialized, \%options); |
|
114
|
|
|
|
|
|
|
|
|
115
|
21
|
100
|
|
|
|
2104562
|
unless (defined $ref_to_frozen_retval) { |
|
116
|
3
|
|
|
|
|
66
|
die 'no return value from worker'; |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
|
|
119
|
18
|
50
|
33
|
|
|
175
|
if (!ref $ref_to_frozen_retval || ref $ref_to_frozen_retval ne 'SCALAR') { |
|
120
|
0
|
|
|
|
|
0
|
die 'unexpected value type'; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
18
|
|
|
|
|
37
|
my $rets = eval { thaw($$ref_to_frozen_retval) }; |
|
|
18
|
|
|
|
|
81
|
|
|
124
|
18
|
50
|
|
|
|
462
|
if ($@) { |
|
|
|
50
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
0
|
die "deserialization error: $@"; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
elsif (ref $rets ne 'ARRAY') { |
|
128
|
0
|
|
|
|
|
0
|
die "gearman function did not return an array"; |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
18
|
50
|
|
|
|
219
|
return wantarray ? @$rets : $rets->[0]; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item run_method_background |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Dispatches a background job to a worker. |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Options: |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=over 4 |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item class |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
(Required) The name of the worker class. |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item method |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
(Required) The name of the method in I to call. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item data |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
(Optional) The job-specific data to pass to the worker. Any structure that can |
|
153
|
|
|
|
|
|
|
be serialized with Storable is allowed. If omitted, undef is sent. |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item unique |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
(Optional) The opaque unique tag for coalescing jobs. |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=back |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub run_method_background { |
|
164
|
0
|
|
|
0
|
1
|
|
my Gearman::Spawner::Client::Sync $self = shift; |
|
165
|
0
|
|
|
|
|
|
my %params = @_; |
|
166
|
|
|
|
|
|
|
|
|
167
|
0
|
|
0
|
|
|
|
my $class = delete $params{class} || croak "need class"; |
|
168
|
0
|
|
0
|
|
|
|
my $method = delete $params{method} || croak "need method"; |
|
169
|
0
|
|
0
|
|
|
|
my $data = delete $params{data} || undef; |
|
170
|
0
|
|
0
|
|
|
|
my $unique = delete $params{unique} || undef; |
|
171
|
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
croak "unknown parameters to run_method: @{[%params]}" if %params; |
|
|
0
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
my $function = Gearman::Spawner::Util::method2function($class, $method); |
|
175
|
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
my $serialized = nfreeze([$data]); |
|
177
|
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my %options; |
|
179
|
0
|
0
|
|
|
|
|
$options{uniq} = $unique if defined $unique; |
|
180
|
|
|
|
|
|
|
|
|
181
|
0
|
|
|
|
|
|
$self->dispatch_background($function => $serialized, \%options); |
|
182
|
|
|
|
|
|
|
|
|
183
|
0
|
|
|
|
|
|
return; |
|
184
|
|
|
|
|
|
|
} |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1; |