| 1 | package Net::Async::Beanstalk; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | our $VERSION = '0.002'; | |||||||||
| 4 | 3 | $VERSION = eval $VERSION; | |||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Net::Async::Beanstalk - Non-blocking beanstalk client | ||||||||||
| 9 | |||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||
| 11 | |||||||||||
| 12 | use IO::Async::Loop; | ||||||||||
| 13 | use Net::Async::Beanstalk; | ||||||||||
| 14 | |||||||||||
| 15 | my $loop = IO::Async::Loop->new(); | ||||||||||
| 16 | |||||||||||
| 17 | my $client = Net::Async::Beanstalk->new(); | ||||||||||
| 18 | $loop->add($client); | ||||||||||
| 19 | |||||||||||
| 20 | $client->connect(host => 'localhost', service => '11300')->get(); | ||||||||||
| 21 | $client->put("anything")->get(); | ||||||||||
| 22 | |||||||||||
| 23 | $loop->run(); | ||||||||||
| 24 | |||||||||||
| 25 | =head1 BUGS | ||||||||||
| 26 | |||||||||||
| 27 | =over | ||||||||||
| 28 | |||||||||||
| 29 | =item * Receiving on_disconnect after sending quit might not work. | ||||||||||
| 30 | |||||||||||
| 31 | In fact disconnecting hasn't been tested at all, even ad-hoc. | ||||||||||
| 32 | |||||||||||
| 33 | =item * This document is even longer. | ||||||||||
| 34 | |||||||||||
| 35 | =item * There are no tests | ||||||||||
| 36 | |||||||||||
| 37 | See if it's appropriate to steal the tests out of L<Beanstalk::Client>. | ||||||||||
| 38 | |||||||||||
| 39 | =back | ||||||||||
| 40 | |||||||||||
| 41 | =cut | ||||||||||
| 42 | |||||||||||
| 43 | 3 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 3 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 3 | ▶ | use IO::Async::Stream; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 3 | ▶ | use List::Util qw(any); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 3 | ▶ | use MooX::EventHandler; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 3 | ▶ | use Net::Async::Beanstalk::Constant qw(:state @GENERAL); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 3 | ▶ | use YAML::Any qw(Dump Load); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 3 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 53 | |||||||||||
| 54 | =head1 DESCRIPTION | ||||||||||
| 55 | |||||||||||
| 56 | Implements the client-side of the beanstalk 1.10 protocol described in | ||||||||||
| 57 | L<https://raw.githubusercontent.com/kr/beanstalkd/v1.10/doc/protocol.txt> | ||||||||||
| 58 | using L<IO::Async> to provide an asynchronous non-blocking API. | ||||||||||
| 59 | |||||||||||
| 60 | Net::Async::Beanstalk is based on L<Moo> and | ||||||||||
| 61 | L<IO::Async::Stream>. Refer to those modules' documentation for basic | ||||||||||
| 62 | usage. In particular L<IO::Async::Loop/connect>. | ||||||||||
| 63 | |||||||||||
| 64 | =head1 ATTRIBUTES | ||||||||||
| 65 | |||||||||||
| 66 | Includes the command stack from L<Net::Async::Beanstalk::Send>. | ||||||||||
| 67 | |||||||||||
| 68 | =cut | ||||||||||
| 69 | |||||||||||
| 70 | 3 | with 'Net::Async::Beanstalk::Stack'; | |||||||||
| 71 | |||||||||||
| 72 | =over | ||||||||||
| 73 | |||||||||||
| 74 | =item default_priority (10,000) | ||||||||||
| 75 | |||||||||||
| 76 | =item defauly_delay (0) | ||||||||||
| 77 | |||||||||||
| 78 | =item default_ttr (120) | ||||||||||
| 79 | |||||||||||
| 80 | Default values to associate with a job when it is L</put> on the | ||||||||||
| 81 | beanstalk server. The defaults here are arbitrary; they have been | ||||||||||
| 82 | chosen to match the default values from L<AnyEvent::Beanstalk>. | ||||||||||
| 83 | |||||||||||
| 84 | =cut | ||||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | has default_priority => is => lazy => builder => sub { 10_000 }; # Totally arbitrary | ||||||||
|
Subroutine
_build_default_priority: not called CC=1 SCAR=1.2
| |||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | has default_delay => is => lazy => builder => sub { 0 }; | ||||||||
|
Subroutine
_build_default_delay: not called CC=1 SCAR=2.6
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||
| 89 | |||||||||||
| 90 | 0 | ▶ | has default_ttr => is => lazy => builder => sub { 120 }; # 2 minutes | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
Subroutine
_build_default_ttr: not called CC=1 SCAR=2.6
| |||||||||||
| 91 | |||||||||||
| 92 | =item decoder (&YAML::Load) | ||||||||||
| 93 | |||||||||||
| 94 | =item encoder (&YAML::Dump) | ||||||||||
| 95 | |||||||||||
| 96 | A coderef which will be used to deserialise or serialise jobs as they | ||||||||||
| 97 | are retreived from or sent to a beanstalk server. | ||||||||||
| 98 | |||||||||||
| 99 | This is not related to how the result of C<list> or C<stats> commands | ||||||||||
| 100 | are deserialised. This is always done using L<YAML/Load>. | ||||||||||
| 101 | |||||||||||
| 102 | =cut | ||||||||||
| 103 | |||||||||||
| 104 | # TODO: has codec => ...; ? | ||||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | has decoder => is => lazy => builder => sub { \&Load }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
Subroutine
_build_decoder: not called CC=1 SCAR=2.6
| |||||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | has encoder => is => lazy => builder => sub { \&Dump }; | ||||||||
|
Subroutine
_build_encoder: not called CC=1 SCAR=2.6
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||
| 109 | |||||||||||
| 110 | =item using | ||||||||||
| 111 | |||||||||||
| 112 | The name of the tube which was recently L<use>d. | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||
| 115 | |||||||||||
| 116 | 3 | has using => is => rwp => init_arg => undef, default => 'default'; | |||||||||
| 117 | |||||||||||
| 118 | =item _watching | ||||||||||
| 119 | |||||||||||
| 120 | A hashref who's keys are the tubes which are being C<watch>ed. The | ||||||||||
| 121 | values are ignored. | ||||||||||
| 122 | |||||||||||
| 123 | Use the accessor C<watching> to get the list of C<watch>ed tubes | ||||||||||
| 124 | instead of using the attribute directly. | ||||||||||
| 125 | |||||||||||
| 126 | =cut | ||||||||||
| 127 | |||||||||||
| 128 | 0 | ▶ | has _watching => is => rwp => init_arg => undef, default => sub {+{ default => 1 }}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 129 | |||||||||||
| 130 | 0 | ▶ | sub watching { keys %{ $_[0]->_watching } } | ||||||||
|
Subroutine
watching: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 131 | |||||||||||
| 132 | =back | ||||||||||
| 133 | |||||||||||
| 134 | =cut | ||||||||||
| 135 | |||||||||||
| 136 | =head1 CLASS | ||||||||||
| 137 | |||||||||||
| 138 | A Net::Async::Beanstalk object represents a single connection to a | ||||||||||
| 139 | beanstalkd server. Once a connection has been established (see | ||||||||||
| 140 | L</CONNECTING>) commands may be submitted by calling the objects | ||||||||||
| 141 | methods (see L</COMMAND METHODS>). | ||||||||||
| 142 | |||||||||||
| 143 | The command methods all return a L<Future> which will either be | ||||||||||
| 144 | completed (marked C<done>) with the result if the command was a | ||||||||||
| 145 | success or failed with the error. | ||||||||||
| 146 | |||||||||||
| 147 | The command methods are named after the L<beanstalk | ||||||||||
| 148 | API|https://raw.githubusercontent.com/kr/beanstalkd/v1.10/doc/protocol.txt> | ||||||||||
| 149 | command that they implement, with the hyphens changed to an underscore | ||||||||||
| 150 | (C<s/-/_/g>). All command methods but L</put> take the same options in | ||||||||||
| 151 | the same order as the respective API command. L</reserve> also has an | ||||||||||
| 152 | option added to it so that it can be used to make reservations with or | ||||||||||
| 153 | without a timeout. | ||||||||||
| 154 | |||||||||||
| 155 | Some events may happen without a command having been sent or in spite | ||||||||||
| 156 | of a command being actively executed. These invoke the events | ||||||||||
| 157 | documented below and do I<not> complete or fail the associated | ||||||||||
| 158 | L<Future>. See the documentation of each error (there aren't many) for | ||||||||||
| 159 | the details in L</ERRORS>. | ||||||||||
| 160 | |||||||||||
| 161 | Although this class implements a non-blocking beanstalk client, the | ||||||||||
| 162 | protocol itself is not asynchronous. Each command sent will receive a | ||||||||||
| 163 | response before the next command will be processed although in | ||||||||||
| 164 | practice network buffering makes it appear that commands can be sent | ||||||||||
| 165 | while waiting for a previous command's response. | ||||||||||
| 166 | |||||||||||
| 167 | Ordinarily this is irrelevant as all the commands except C<reserve> | ||||||||||
| 168 | and C<reserve-with-timeout> respond quickly enough that any delay will | ||||||||||
| 169 | be negligible and this class' internal L<command | ||||||||||
| 170 | stack|Net::Async::Beanstalk::Stack> smooths over the times where | ||||||||||
| 171 | that's not the case. | ||||||||||
| 172 | |||||||||||
| 173 | When any command which blocks the server has been sent, other commands | ||||||||||
| 174 | will be stacked up waiting to be sent to the server but will not be | ||||||||||
| 175 | handled (that is, not even put on the wire) until the running command | ||||||||||
| 176 | command has completed (perhaps with a timeout). | ||||||||||
| 177 | |||||||||||
| 178 | If this is a concern, the beanstalkd server is explicitly written to | ||||||||||
| 179 | support multiple connections with low overhead, so there is no need to | ||||||||||
| 180 | perform all operations using the same client object. Just be aware | ||||||||||
| 181 | that the list of active tubes is not copied (by default) from one | ||||||||||
| 182 | client to another. Each client starts off using and watching the | ||||||||||
| 183 | C<default> tube. | ||||||||||
| 184 | |||||||||||
| 185 | When a job has been reserved by a client (which remains connected) | ||||||||||
| 186 | that job is invisible to any other clients. It cannot, for example, be | ||||||||||
| 187 | deleted except over the same connected in which it was reserved and if | ||||||||||
| 188 | that connection is closed the job will return to the ready queue and | ||||||||||
| 189 | may be reserved by another client. | ||||||||||
| 190 | |||||||||||
| 191 | =head1 CONNECTING | ||||||||||
| 192 | |||||||||||
| 193 | Voodoo. | ||||||||||
| 194 | |||||||||||
| 195 | =head1 ERRORS | ||||||||||
| 196 | |||||||||||
| 197 | =for comment Move this to ::Receive | ||||||||||
| 198 | |||||||||||
| 199 | There are not many error conditions described by the beanstalk | ||||||||||
| 200 | protocol. L<Net::Async::Beanstalk::Receive> also defines errors in the | ||||||||||
| 201 | event of bugs revealing mistakes in this code or communication | ||||||||||
| 202 | failures. Each will cause either the current L<Future> to fail, raise | ||||||||||
| 203 | an event, or both. | ||||||||||
| 204 | |||||||||||
| 205 | See each error's description but by and large the errors that happen | ||||||||||
| 206 | because a command failed (which is not the same thing thing as "while | ||||||||||
| 207 | a command was active") fail the L<Future> while the error conditions | ||||||||||
| 208 | that arise spontaneously invoke an C<on_error> event (defined in | ||||||||||
| 209 | L<IO::Async::Notifier>). If the Net::Async::Beanstalk object (or its | ||||||||||
| 210 | parent) is created without an C<on_error> handler then the default | ||||||||||
| 211 | C<on_error> handler will be used which calls L<die|perlfunc/die>. | ||||||||||
| 212 | |||||||||||
| 213 | Except where noted each error or failure includes the arguments which | ||||||||||
| 214 | were sent with the command to the server (not including the command | ||||||||||
| 215 | itself). If you call a command such as this | ||||||||||
| 216 | |||||||||||
| 217 | my $give_it_a_rest = $beanstalk->bury(0x6642, 9_001); | ||||||||||
| 218 | |||||||||||
| 219 | then $give_it_a_rest will hold a L<Future> which will eventually fail | ||||||||||
| 220 | and call its handler with: | ||||||||||
| 221 | |||||||||||
| 222 | $h->("...buried: 26180 not found", "beanstalk-peek", 0x6632, 9_001); | ||||||||||
| 223 | |||||||||||
| 224 | The exceptional errors are: | ||||||||||
| 225 | |||||||||||
| 226 | |||||||||||
| 227 | =for comment = find all three errors. | ||||||||||
| 228 | |||||||||||
| 229 | =over | ||||||||||
| 230 | |||||||||||
| 231 | =item C<Protocol error: Bad format> | ||||||||||
| 232 | |||||||||||
| 233 | Category: C<beanstalk-internal> | ||||||||||
| 234 | |||||||||||
| 235 | Arguments: The buffer which was written into the communication stream. | ||||||||||
| 236 | |||||||||||
| 237 | This error invokes an C<on_error> event and fails the active L<Future>. | ||||||||||
| 238 | |||||||||||
| 239 | The server received a command line that was not well-formed. This | ||||||||||
| 240 | should never happen and when it does it indicates an error in this | ||||||||||
| 241 | module. Please report it so that it can be repaired. | ||||||||||
| 242 | |||||||||||
| 243 | =item C<Protocol error: Internal error> | ||||||||||
| 244 | |||||||||||
| 245 | Category: C<beanstalk-server> | ||||||||||
| 246 | |||||||||||
| 247 | Arguments: Everything. | ||||||||||
| 248 | |||||||||||
| 249 | This error invokes C<on_error> only. | ||||||||||
| 250 | |||||||||||
| 251 | The server suffered from an internal error. This should never happen | ||||||||||
| 252 | and when it does it indicates an error in the server. Please report it | ||||||||||
| 253 | to the L<beanstalk maintainer|http://kr.github.com/beanstalkd/> so | ||||||||||
| 254 | that it can be repaired. | ||||||||||
| 255 | |||||||||||
| 256 | This error does not attempt to fail the current or any pending | ||||||||||
| 257 | L<Future>s, however the server's probably about to crash so your code | ||||||||||
| 258 | should deal with that and the pending L<Future>s gracefully. | ||||||||||
| 259 | |||||||||||
| 260 | =item C<Protocol error: Out of memory> | ||||||||||
| 261 | |||||||||||
| 262 | Category: C<beanstalk-server> | ||||||||||
| 263 | |||||||||||
| 264 | Arguments: The command name and then the arguments as usual. | ||||||||||
| 265 | |||||||||||
| 266 | This error only fails the active L<Future>. | ||||||||||
| 267 | |||||||||||
| 268 | The server ran out of memory. This happens sometimes but generally it | ||||||||||
| 269 | should not. Please report it to your system administrator so that he | ||||||||||
| 270 | can be repaired. | ||||||||||
| 271 | |||||||||||
| 272 | =item C<Protocol error: Unknown command> | ||||||||||
| 273 | |||||||||||
| 274 | Category: C<beanstalk-internal> | ||||||||||
| 275 | |||||||||||
| 276 | Arguments: The command name and then the arguments as usual. | ||||||||||
| 277 | |||||||||||
| 278 | This error invokes an C<on_error> event and fails the active L<Future>. | ||||||||||
| 279 | |||||||||||
| 280 | This module sent a command the server did not understand. This should | ||||||||||
| 281 | never happen and when it does it indicates an error in the server or a | ||||||||||
| 282 | protocol mismatch between the server and client. | ||||||||||
| 283 | |||||||||||
| 284 | =item C<Protocol error: Unknown response> | ||||||||||
| 285 | |||||||||||
| 286 | Category: C<beanstalk-server> | ||||||||||
| 287 | |||||||||||
| 288 | Arguments: The buffer which was received from the communication stream | ||||||||||
| 289 | as an arrayref of each received chunk. | ||||||||||
| 290 | |||||||||||
| 291 | This error invokes C<on_error> only. | ||||||||||
| 292 | |||||||||||
| 293 | The server sent a message this client did not understand. This should | ||||||||||
| 294 | never happen and when it does it indicates an error in the server or a | ||||||||||
| 295 | protocol mismatch between the server and client. | ||||||||||
| 296 | |||||||||||
| 297 | This error does not attempt to fail the current or any pending | ||||||||||
| 298 | L<Future>s, however the server's speaking gibberish so nobody knows | ||||||||||
| 299 | what's going to happen next. Your code should deal with that and the | ||||||||||
| 300 | pending L<Future>s gracefully. | ||||||||||
| 301 | |||||||||||
| 302 | =back | ||||||||||
| 303 | |||||||||||
| 304 | In order to make the command stack work, each L<Future> is created | ||||||||||
| 305 | with an C<on_ready> handler which sends the next pending command. In | ||||||||||
| 306 | the event of an error the pending commands may become invalid. This | ||||||||||
| 307 | class makes no attempt to deal with that. | ||||||||||
| 308 | |||||||||||
| 309 | One other protocol error (C<Expected cr+lf>) can be received only in | ||||||||||
| 310 | response to a L</put> command (it does not invoke an L<on_error> | ||||||||||
| 311 | event). | ||||||||||
| 312 | |||||||||||
| 313 | =head1 COMMAND METHODS | ||||||||||
| 314 | |||||||||||
| 315 | =for comment Move this to ::Send | ||||||||||
| 316 | |||||||||||
| 317 | Methods which initiate a command on the server are implemented in | ||||||||||
| 318 | L<Net::Async::Beanstalk::Send>. The server response is processed by | ||||||||||
| 319 | the event handlers in L<Net::Async::Beanstalk::Receive>. Every command | ||||||||||
| 320 | method returns a L<Future> which will complete with the server's | ||||||||||
| 321 | response to that command, whether success or failure. | ||||||||||
| 322 | |||||||||||
| 323 | With few exceptions, documented below, each method expects exactly the | ||||||||||
| 324 | arguments that the respective command requires. The commands which | ||||||||||
| 325 | expect to receive a YAML structure as the response (primarily the | ||||||||||
| 326 | C<list-*> commands) deserialise the response before returning it as a | ||||||||||
| 327 | (non-reference) list or hash. | ||||||||||
| 328 | |||||||||||
| 329 | The methods are named with a C<_> where the API command has a C<->. | ||||||||||
| 330 | |||||||||||
| 331 | See | ||||||||||
| 332 | L<the protocol documentation|https://raw.githubusercontent.com/kr/beanstalkd/v1.10/doc/protocol.txt> | ||||||||||
| 333 | for further details on each command. They are: | ||||||||||
| 334 | |||||||||||
| 335 | =over | ||||||||||
| 336 | |||||||||||
| 337 | =item put ($job, %options) or put (%options) | ||||||||||
| 338 | |||||||||||
| 339 | Put a job onto the currently C<use>d tube. The job data can be passed | ||||||||||
| 340 | as the method's first argument or in C<%options>. | ||||||||||
| 341 | |||||||||||
| 342 | The job's C<priority>, C<delay> or C<ttr> can be set by including | ||||||||||
| 343 | those values in C<%options>. If they are not then the object's default | ||||||||||
| 344 | value is used (see above). | ||||||||||
| 345 | |||||||||||
| 346 | The job may be passed as the method's first or only argument or as | ||||||||||
| 347 | C<data> in C<%options>. It will be serialised using L</encoder> if | ||||||||||
| 348 | it's a reference and does not overload the stringify (C<"">) operator. | ||||||||||
| 349 | |||||||||||
| 350 | The job may instead be passed as C<raw_data> in C<%options> if it has | ||||||||||
| 351 | already been serialised. | ||||||||||
| 352 | |||||||||||
| 353 | Regardless of whether C</encoder> is used to serialise the job it is | ||||||||||
| 354 | changed to a string of bytes using L<utf8::encode|utf8/encode>. | ||||||||||
| 355 | |||||||||||
| 356 | It is an error to pass the job data in more than one form or to | ||||||||||
| 357 | included unknown options and C<put> will L<croak|Carp/croak>. | ||||||||||
| 358 | |||||||||||
| 359 | Possible failures: | ||||||||||
| 360 | |||||||||||
| 361 | =over | ||||||||||
| 362 | |||||||||||
| 363 | =item C<Protocol error: Expected cr+lf> | ||||||||||
| 364 | |||||||||||
| 365 | Category: C<beanstalk-put> | ||||||||||
| 366 | |||||||||||
| 367 | Arguments: As with C<bad format>, this should but does not include the | ||||||||||
| 368 | buffer which was sent. | ||||||||||
| 369 | |||||||||||
| 370 | This error only fails the active L<Future>. | ||||||||||
| 371 | |||||||||||
| 372 | The client sent badly-formed job data which was not terminated by a | ||||||||||
| 373 | CR+LF pair. This should never happen and when it does it indicates an | ||||||||||
| 374 | error in this module. Please report it so that it can be repaired. | ||||||||||
| 375 | |||||||||||
| 376 | =item C<Invalid job: too big> | ||||||||||
| 377 | |||||||||||
| 378 | Category: C<beanstalk-put> | ||||||||||
| 379 | |||||||||||
| 380 | The client sent job data which was rejected by the server for being | ||||||||||
| 381 | too large. The job has not beed stored in any queue. | ||||||||||
| 382 | |||||||||||
| 383 | =item C<Job was inserted but buried (out of memory): ID $id> | ||||||||||
| 384 | |||||||||||
| 385 | Category: C<beanstalk-put> | ||||||||||
| 386 | |||||||||||
| 387 | The job was successfully received by the server but it was unable to | ||||||||||
| 388 | allocate memory to put it into the ready queue and so the job has been | ||||||||||
| 389 | buried. | ||||||||||
| 390 | |||||||||||
| 391 | =item C<Job was not inserted: Server is draining> | ||||||||||
| 392 | |||||||||||
| 393 | Category: C<beanstalk-put> | ||||||||||
| 394 | |||||||||||
| 395 | The server is currently being drained and is not accepting new | ||||||||||
| 396 | jobs. The job has not been stored in any queue. | ||||||||||
| 397 | |||||||||||
| 398 | =back | ||||||||||
| 399 | |||||||||||
| 400 | =item reserve (%options) | ||||||||||
| 401 | |||||||||||
| 402 | Reserve the next available job. C<timeout> may be passed in | ||||||||||
| 403 | C<%options> in which case the C<reserve-with-timout> command is sent | ||||||||||
| 404 | instead. C<timeout> may be C<0>. | ||||||||||
| 405 | |||||||||||
| 406 | The data returned by the server is transformed into a string of | ||||||||||
| 407 | characters with L<utf8/decode> then deserialised using C</decoder>. | ||||||||||
| 408 | |||||||||||
| 409 | If the C<asis> option is set to a true value then the data returned by | ||||||||||
| 410 | the server is transformed into characters but is not deserialised. | ||||||||||
| 411 | |||||||||||
| 412 | If the C<raw> option is set to a true value then the data is left | ||||||||||
| 413 | completely untouched. | ||||||||||
| 414 | |||||||||||
| 415 | Possible failures: | ||||||||||
| 416 | |||||||||||
| 417 | =over | ||||||||||
| 418 | |||||||||||
| 419 | =item C<No job was reserved: Deadline soon> | ||||||||||
| 420 | |||||||||||
| 421 | Category: C<beanstalk-reserve> | ||||||||||
| 422 | |||||||||||
| 423 | A job which was previously reserved by this client and has not been | ||||||||||
| 424 | handled is nearing the time when its reservation will expire and the | ||||||||||
| 425 | server will restore it to the ready queue. | ||||||||||
| 426 | |||||||||||
| 427 | =back | ||||||||||
| 428 | |||||||||||
| 429 | =item reserve_with_timeout ($time, %options) | ||||||||||
| 430 | |||||||||||
| 431 | Implemented by calling L</reserve> with a C<timeout> option. C<$time> | ||||||||||
| 432 | may be 0 which will cause the L<Future> to fail immediately with a | ||||||||||
| 433 | C<Timed out> error if there are no jobs available. | ||||||||||
| 434 | |||||||||||
| 435 | Possible failures: | ||||||||||
| 436 | |||||||||||
| 437 | =over | ||||||||||
| 438 | |||||||||||
| 439 | =item C<No job was reserved: Timed out> | ||||||||||
| 440 | |||||||||||
| 441 | Category: C<beanstalk-reserve> | ||||||||||
| 442 | |||||||||||
| 443 | The number of seconds specified in the timeout value to | ||||||||||
| 444 | C<reserve-with-timeout> has expired without a job becoming ready to | ||||||||||
| 445 | reserve. | ||||||||||
| 446 | |||||||||||
| 447 | =back | ||||||||||
| 448 | |||||||||||
| 449 | In addition all the failures possible in response to the L</reserve> | ||||||||||
| 450 | command can be received in response to C<reserve_with_timeout>. | ||||||||||
| 451 | |||||||||||
| 452 | =item bury ($job_id) | ||||||||||
| 453 | |||||||||||
| 454 | Possible failures: | ||||||||||
| 455 | |||||||||||
| 456 | =over | ||||||||||
| 457 | |||||||||||
| 458 | =item C<The job could not be buried: $id not found> | ||||||||||
| 459 | |||||||||||
| 460 | Category: C<beanstalk-job> | ||||||||||
| 461 | |||||||||||
| 462 | The job with ID C<$id> could not be buried because it does not exist | ||||||||||
| 463 | or has not been previously reserved by this client. | ||||||||||
| 464 | |||||||||||
| 465 | =for comment TODO: Can a job be buried if it is reserved by _no_ client? | ||||||||||
| 466 | |||||||||||
| 467 | =back | ||||||||||
| 468 | |||||||||||
| 469 | =item delete ($job_id) | ||||||||||
| 470 | |||||||||||
| 471 | Possible failures: | ||||||||||
| 472 | |||||||||||
| 473 | =over | ||||||||||
| 474 | |||||||||||
| 475 | =item C<The job could not be deleted: $id not found> | ||||||||||
| 476 | |||||||||||
| 477 | Category: C<beanstalk-job> | ||||||||||
| 478 | |||||||||||
| 479 | The job with ID C<$id> could not be deleted because it does not exist, | ||||||||||
| 480 | has not been previously reserved by this client or is not in a | ||||||||||
| 481 | C<ready> or C<buried> state. | ||||||||||
| 482 | |||||||||||
| 483 | =back | ||||||||||
| 484 | |||||||||||
| 485 | =item ignore ($tube_name) | ||||||||||
| 486 | |||||||||||
| 487 | Possible failures: | ||||||||||
| 488 | |||||||||||
| 489 | =over | ||||||||||
| 490 | |||||||||||
| 491 | =item C<The last tube cannot be ignored: $tube> | ||||||||||
| 492 | |||||||||||
| 493 | Category: C<beanstalk-tube> | ||||||||||
| 494 | |||||||||||
| 495 | The client attempted to ignore the only tube remaining in its watch | ||||||||||
| 496 | list. | ||||||||||
| 497 | |||||||||||
| 498 | =for comment Is it an error to ignore a tube which is not being watched? | ||||||||||
| 499 | |||||||||||
| 500 | =back | ||||||||||
| 501 | |||||||||||
| 502 | =item kick_job ($job_id) | ||||||||||
| 503 | |||||||||||
| 504 | Possible failures: | ||||||||||
| 505 | |||||||||||
| 506 | =over | ||||||||||
| 507 | |||||||||||
| 508 | =item C<The job could not be kicked: $id not found> | ||||||||||
| 509 | |||||||||||
| 510 | Category: C<beanstalk-job> | ||||||||||
| 511 | |||||||||||
| 512 | The job with ID C<$id> could not be kicked because it "is not in a | ||||||||||
| 513 | kickable state". | ||||||||||
| 514 | |||||||||||
| 515 | =for comment The documentation is vague and includes the possibility | ||||||||||
| 516 | of "internal errors". | ||||||||||
| 517 | |||||||||||
| 518 | =back | ||||||||||
| 519 | |||||||||||
| 520 | =item kick ($max) | ||||||||||
| 521 | |||||||||||
| 522 | This command should not fail. | ||||||||||
| 523 | |||||||||||
| 524 | =item list_tubes () | ||||||||||
| 525 | |||||||||||
| 526 | This command should not fail. | ||||||||||
| 527 | |||||||||||
| 528 | =item list_tubes_watched () | ||||||||||
| 529 | |||||||||||
| 530 | This command should not fail. | ||||||||||
| 531 | |||||||||||
| 532 | =item list_tube_used () | ||||||||||
| 533 | |||||||||||
| 534 | This command should not fail. | ||||||||||
| 535 | |||||||||||
| 536 | =item pause_tube ($tube_name, $delay) | ||||||||||
| 537 | |||||||||||
| 538 | Possible failures: | ||||||||||
| 539 | |||||||||||
| 540 | =over | ||||||||||
| 541 | |||||||||||
| 542 | =item C<The tube could not be paused: $tube not found> | ||||||||||
| 543 | |||||||||||
| 544 | Category: C<beanstalk-tube> | ||||||||||
| 545 | |||||||||||
| 546 | The tube could not be paused because it doesn't exist. | ||||||||||
| 547 | |||||||||||
| 548 | =back | ||||||||||
| 549 | |||||||||||
| 550 | =item peek ($job_id) | ||||||||||
| 551 | |||||||||||
| 552 | Possible failures: | ||||||||||
| 553 | |||||||||||
| 554 | =over | ||||||||||
| 555 | |||||||||||
| 556 | =item C<The job could not be peeked at: $id not found> | ||||||||||
| 557 | |||||||||||
| 558 | Category: C<beanstalk-peek> | ||||||||||
| 559 | |||||||||||
| 560 | The specified job could not be retrieved because it does not exist. | ||||||||||
| 561 | |||||||||||
| 562 | =back | ||||||||||
| 563 | |||||||||||
| 564 | =item peek_buried () | ||||||||||
| 565 | |||||||||||
| 566 | Possible failures: | ||||||||||
| 567 | |||||||||||
| 568 | =over | ||||||||||
| 569 | |||||||||||
| 570 | =item C<The next buried job could not be peeked at: None found> | ||||||||||
| 571 | |||||||||||
| 572 | Category: C<beanstalk-peek> | ||||||||||
| 573 | |||||||||||
| 574 | The next job in a buried state could not be retrieved because one does | ||||||||||
| 575 | not exist. | ||||||||||
| 576 | |||||||||||
| 577 | =back | ||||||||||
| 578 | |||||||||||
| 579 | =item peek_delayed () | ||||||||||
| 580 | |||||||||||
| 581 | Possible failures: | ||||||||||
| 582 | |||||||||||
| 583 | =over | ||||||||||
| 584 | |||||||||||
| 585 | =item C<The next delayed job could not be peeked at: None found> | ||||||||||
| 586 | |||||||||||
| 587 | Category: C<beanstalk-peek> | ||||||||||
| 588 | |||||||||||
| 589 | The next job in a delayed state could not be retrieved because one | ||||||||||
| 590 | does not exist. | ||||||||||
| 591 | |||||||||||
| 592 | =back | ||||||||||
| 593 | |||||||||||
| 594 | =item peek_ready () | ||||||||||
| 595 | |||||||||||
| 596 | Possible failures: | ||||||||||
| 597 | |||||||||||
| 598 | =over | ||||||||||
| 599 | |||||||||||
| 600 | =item C<The next ready job could not be peeked at: None found> | ||||||||||
| 601 | |||||||||||
| 602 | Category: C<beanstalk-peek> | ||||||||||
| 603 | |||||||||||
| 604 | The next job in a ready state could not be retrieved because one does | ||||||||||
| 605 | not exist. | ||||||||||
| 606 | |||||||||||
| 607 | =back | ||||||||||
| 608 | |||||||||||
| 609 | =item quit () | ||||||||||
| 610 | |||||||||||
| 611 | In theory this will raise an C<on_disconnect> in addition to | ||||||||||
| 612 | completing the L<Future> it returns. In practice I haven't written it | ||||||||||
| 613 | yet. | ||||||||||
| 614 | |||||||||||
| 615 | =item release ($job_id, $priority, $delay) | ||||||||||
| 616 | |||||||||||
| 617 | Possible failures: | ||||||||||
| 618 | |||||||||||
| 619 | =over | ||||||||||
| 620 | |||||||||||
| 621 | =item C<The job could not be released: $id not found> | ||||||||||
| 622 | |||||||||||
| 623 | Category: C<beanstalk-job> | ||||||||||
| 624 | |||||||||||
| 625 | The job with ID C<$id> could not be released because it does not exist | ||||||||||
| 626 | or has not been previously reserved by this client. | ||||||||||
| 627 | |||||||||||
| 628 | =for comment TODO: What if an attempt is made to release a released job? | ||||||||||
| 629 | |||||||||||
| 630 | =item C<The job could not be released (out of memory): ID $id> | ||||||||||
| 631 | |||||||||||
| 632 | Category: C<beanstalk-job> | ||||||||||
| 633 | |||||||||||
| 634 | The job with ID C<$id> could not be released because the server ran | ||||||||||
| 635 | out of memory. | ||||||||||
| 636 | |||||||||||
| 637 | =back | ||||||||||
| 638 | |||||||||||
| 639 | =item stats () | ||||||||||
| 640 | |||||||||||
| 641 | This command should not fail. | ||||||||||
| 642 | |||||||||||
| 643 | =item stats_job ($job_id) | ||||||||||
| 644 | |||||||||||
| 645 | Possible failures: | ||||||||||
| 646 | |||||||||||
| 647 | =over | ||||||||||
| 648 | |||||||||||
| 649 | =item C<Statistics were not found for the job: $id not found> | ||||||||||
| 650 | |||||||||||
| 651 | Category: C<beanstalk-job> | ||||||||||
| 652 | |||||||||||
| 653 | No statistics are available for the job with ID C<$id> because it does | ||||||||||
| 654 | not exist. | ||||||||||
| 655 | |||||||||||
| 656 | =back | ||||||||||
| 657 | |||||||||||
| 658 | =item stats_tube ($tube_name) | ||||||||||
| 659 | |||||||||||
| 660 | Possible failures: | ||||||||||
| 661 | |||||||||||
| 662 | =over | ||||||||||
| 663 | |||||||||||
| 664 | =item C<Statistics were not found for the tube: $tube not found> | ||||||||||
| 665 | |||||||||||
| 666 | Category: C<beanstalk-tube> | ||||||||||
| 667 | |||||||||||
| 668 | No statistics are available for the tube named C<$tube> because it | ||||||||||
| 669 | does not exist. | ||||||||||
| 670 | |||||||||||
| 671 | =back | ||||||||||
| 672 | |||||||||||
| 673 | =item touch ($job_id) | ||||||||||
| 674 | |||||||||||
| 675 | Possible failures: | ||||||||||
| 676 | |||||||||||
| 677 | =over | ||||||||||
| 678 | |||||||||||
| 679 | =item C<The job could not be touched: $id not found> | ||||||||||
| 680 | |||||||||||
| 681 | Category: C<beanstalk-job> | ||||||||||
| 682 | |||||||||||
| 683 | The job with ID C<$id> could not be touched because it does not exist | ||||||||||
| 684 | or has not been previously reserved by this client. | ||||||||||
| 685 | |||||||||||
| 686 | =back | ||||||||||
| 687 | |||||||||||
| 688 | =item use ($tube_name) | ||||||||||
| 689 | |||||||||||
| 690 | This command should not fail. | ||||||||||
| 691 | |||||||||||
| 692 | =item watch ($tube_name) | ||||||||||
| 693 | |||||||||||
| 694 | This command should not fail. | ||||||||||
| 695 | |||||||||||
| 696 | =back | ||||||||||
| 697 | |||||||||||
| 698 | =cut | ||||||||||
| 699 | |||||||||||
| 700 | 3 | with 'Net::Async::Beanstalk::Receive'; | |||||||||
| 701 | |||||||||||
| 702 | 3 | with 'Net::Async::Beanstalk::Send'; | |||||||||
| 703 | |||||||||||
| 704 | =head1 OTHER METHODS | ||||||||||
| 705 | |||||||||||
| 706 | =over | ||||||||||
| 707 | |||||||||||
| 708 | =item reserve_pending () => @commands | ||||||||||
| 709 | |||||||||||
| 710 | Returns a all the entries in L<the command stack|/_command_stack> | ||||||||||
| 711 | which refer to a C<reserve> or C<reserve-with-timeout> command. | ||||||||||
| 712 | |||||||||||
| 713 | =cut | ||||||||||
| 714 | |||||||||||
| 715 | 0 | ▶ | sub reserve_pending { | ||||||||
|
Subroutine
reserve_pending: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 716 | 0 | scalar grep { $_->[STATE_COMMAND] =~ /^reserve/ } $_[0]->_pending_commands; | |||||||||
| 717 | } | ||||||||||
| 718 | |||||||||||
| 719 | =item disconnect () => $future | ||||||||||
| 720 | |||||||||||
| 721 | An alias for L<quit>. | ||||||||||
| 722 | |||||||||||
| 723 | =cut | ||||||||||
| 724 | |||||||||||
| 725 | 3 | *disconnect = \&quit; | |||||||||
| 726 | |||||||||||
| 727 | =item sync () => $future | ||||||||||
| 728 | |||||||||||
| 729 | Returns a L<Future> which completes when all pending commands have | ||||||||||
| 730 | been responded to. | ||||||||||
| 731 | |||||||||||
| 732 | =cut | ||||||||||
| 733 | |||||||||||
| 734 | 0 | ▶ | sub sync { Future->wait_all(map { $_->[STATE_FUTURE] } $_[0]->_pending_commands) } | ||||||||
|
Subroutine
sync: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 735 | |||||||||||
| 736 | =item watch_only (@tubes) => $future | ||||||||||
| 737 | |||||||||||
| 738 | Send a C<list-tubes-watched> command and based on its result send a | ||||||||||
| 739 | series of C<watch> and then C<ignore> commands so that the tubes being | ||||||||||
| 740 | watched for this client exactly matches C<@tubes>. | ||||||||||
| 741 | |||||||||||
| 742 | =cut | ||||||||||
| 743 | |||||||||||
| 744 | 0 | ▶ | sub watch_only { | ||||||||
|
Subroutine
watch_only: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 745 | 0 | my $self = shift; | |||||||||
| 746 | 0 | my %want = map{+($_=>1)} @_; | |||||||||
| 747 | 0 | ▶ | $self->list_tubes_watched->then(sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 748 | 0 | my %current = map {+($_=>1)} @_; | |||||||||
| 749 | 0 | my @watch = map { $self->watch($_) } grep { not $current{$_} } keys %want; | |||||||||
| 750 | 0 | my @ignore = map { $self->ignore($_) } grep { not $want{$_} } keys %current; | |||||||||
| 751 | 0 | Future->wait_all(@watch, @ignore); | |||||||||
| 752 | 0 | }); | |||||||||
| 753 | } | ||||||||||
| 754 | |||||||||||
| 755 | =back | ||||||||||
| 756 | |||||||||||
| 757 | =head1 INTERNAL METHODS | ||||||||||
| 758 | |||||||||||
| 759 | =over | ||||||||||
| 760 | |||||||||||
| 761 | =item _assert_state($response_word) => VOID | ||||||||||
| 762 | |||||||||||
| 763 | Raises an exception of the word received from the server is not | ||||||||||
| 764 | something expected in response to the command which has most recently | ||||||||||
| 765 | been sent. | ||||||||||
| 766 | |||||||||||
| 767 | =cut | ||||||||||
| 768 | |||||||||||
| 769 | 0 | ▶ | sub _assert_state { | ||||||||
|
Subroutine
_assert_state: not called CC=4 SCAR=30.0
| |||||||||||
| 770 | 0 | my $self = shift; | |||||||||
| 771 | 0 | my ($response) = @_; | |||||||||
| 772 | 0 | ▶ | return if any { $response eq $_ } @GENERAL; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 773 | 0 | ▶ | croak "Internal error: $response in null state" unless $self->count_commands; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 774 | 0 | my $state = $self->current_command; | |||||||||
| 775 | croak "Internal error: $response in invalid state " . $state->[STATE_COMMAND] | ||||||||||
| 776 | 0 | ▶ | unless exists $STATE_CAN{$state->[STATE_COMMAND]}{$response}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 777 | } | ||||||||||
| 778 | |||||||||||
| 779 | =item fail_command($message, $exception, @args) => $future | ||||||||||
| 780 | |||||||||||
| 781 | Remove the current command from the command stack and fail its | ||||||||||
| 782 | L<Future> with this method's arguments. | ||||||||||
| 783 | |||||||||||
| 784 | The L<Future> returned is the one which returned when initiating a | ||||||||||
| 785 | command and can be safely ignored. | ||||||||||
| 786 | |||||||||||
| 787 | This is used by L<Net::Async::Beanstalk::Receive> when the client | ||||||||||
| 788 | received an expected response which nevertheless indicates an error of | ||||||||||
| 789 | some kind, such as C<DEADLINE_SOON> received in response to a | ||||||||||
| 790 | C<reserve> command. | ||||||||||
| 791 | |||||||||||
| 792 | =cut | ||||||||||
| 793 | |||||||||||
| 794 | 0 | ▶ | sub fail_command { $_[0]->_shift_command->[0]->fail(@_[1..$#_]) } | ||||||||
|
Subroutine
fail_command: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 795 | |||||||||||
| 796 | =item finish_command($event, @args) => $future | ||||||||||
| 797 | |||||||||||
| 798 | Remove the current command from the command stack and complete its | ||||||||||
| 799 | L<Future> with this method's arguments. | ||||||||||
| 800 | |||||||||||
| 801 | The L<Future> returned is the one which returned when initiating a | ||||||||||
| 802 | command and can be safely ignored. | ||||||||||
| 803 | |||||||||||
| 804 | This is used by L<Net::Async::Beanstalk::Receive> when the server sent | ||||||||||
| 805 | a response to a command which indicates successful completion. | ||||||||||
| 806 | |||||||||||
| 807 | =cut | ||||||||||
| 808 | |||||||||||
| 809 | 0 | ▶ | sub finish_command { $_[0]->_shift_command->[0]->done(@_[1..$#_]) } | ||||||||
|
Subroutine
finish_command: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 810 | |||||||||||
| 811 | # Messy Moo/IO::Async stuff | ||||||||||
| 812 | |||||||||||
| 813 | # TODO: Don't use IaProtocol. Apparently it's bad. | ||||||||||
| 814 | 3 | ▶ | BEGIN { our @ISA; unshift @ISA, 'IO::Async::Stream' } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 815 | |||||||||||
| 816 | 3 | my @events = qw( | |||||||||
| 817 | on_disconnect | ||||||||||
| 818 | ); | ||||||||||
| 819 | |||||||||||
| 820 | # TODO: MooX::EventHandler is looking less and less useful. | ||||||||||
| 821 | 3 | has_event $_ for @events; | |||||||||
| 822 | |||||||||||
| 823 | 3 | my @attributes = qw( | |||||||||
| 824 | default_delay | ||||||||||
| 825 | default_priority | ||||||||||
| 826 | default_ttr | ||||||||||
| 827 | encoder | ||||||||||
| 828 | decoder | ||||||||||
| 829 | ); | ||||||||||
| 830 | |||||||||||
| 831 | 0 | ▶ | sub FOREIGNBUILDARGS { | ||||||||
|
Subroutine
FOREIGNBUILDARGS: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 832 | 0 | my $class = shift; | |||||||||
| 833 | 0 | my %args = @_; | |||||||||
| 834 | 0 | delete @args{@attributes, @events}; | |||||||||
| 835 | 0 | return %args; | |||||||||
| 836 | } | ||||||||||
| 837 | |||||||||||
| 838 | 3 | 1; | |||||||||
| 839 | |||||||||||
| 840 | =back | ||||||||||
| 841 | |||||||||||
| 842 | =head1 ALTERNATIVE IMPLEMENTATIONS | ||||||||||
| 843 | |||||||||||
| 844 | =over | ||||||||||
| 845 | |||||||||||
| 846 | =item L<AnyEvent::Beanstalk> | ||||||||||
| 847 | |||||||||||
| 848 | A good module and asynchronous but it uses L<AnyEvent> which ... the | ||||||||||
| 849 | less said the better. The core of the protocol is implemented but it | ||||||||||
| 850 | does not handle all error conditions. I have attempted to make | ||||||||||
| 851 | L<Net::Async::Beanstalk>'s API superficially similar to this one. | ||||||||||
| 852 | |||||||||||
| 853 | =item L<Beanstalk::Client> | ||||||||||
| 854 | |||||||||||
| 855 | Also written by Graham Barr, this module seems to be slightly more | ||||||||||
| 856 | functionally complete than its L<AnyEvent> counterpart and has proven | ||||||||||
| 857 | itself stable and fast but unfortunately does not operate | ||||||||||
| 858 | asynchronously. | ||||||||||
| 859 | |||||||||||
| 860 | =item L<AnyEvent::Beanstalk::Worker> | ||||||||||
| 861 | |||||||||||
| 862 | Unfortunately also based on L<AnyEvent> which is a shame because it | ||||||||||
| 863 | implements what appears to be an interesting FSA using beanstalk | ||||||||||
| 864 | queues. | ||||||||||
| 865 | |||||||||||
| 866 | =item L<Queue::Beanstalk> | ||||||||||
| 867 | |||||||||||
| 868 | Ancient, presumably unsupported and based on an out-dated version of | ||||||||||
| 869 | the beanstalk protocol. | ||||||||||
| 870 | |||||||||||
| 871 | =back | ||||||||||
| 872 | |||||||||||
| 873 | =head1 SEE ALSO | ||||||||||
| 874 | |||||||||||
| 875 | L<IO::Async> | ||||||||||
| 876 | |||||||||||
| 877 | L<Future> | ||||||||||
| 878 | |||||||||||
| 879 | L<http://kr.github.com/beanstalkd/> | ||||||||||
| 880 | |||||||||||
| 881 | L<https://raw.githubusercontent.com/kr/beanstalkd/v1.10/doc/protocol.txt> | ||||||||||
| 882 | |||||||||||
| 883 | =head1 AUTHOR | ||||||||||
| 884 | |||||||||||
| 885 | Matthew King <chohag@jtan.com> | ||||||||||
| 886 | |||||||||||
| 887 | =cut | ||||||||||