Debian Codegraph @

doc/protocol.txt

raw
1 = Beanstalk Protocol =
2
3 Protocol
4 --------
5
6 The beanstalk protocol runs over TCP using ASCII encoding. Clients connect,
7 send commands and data, wait for responses, and close the connection. For each
8 connection, the server processes commands serially in the order in which they
9 were received and sends responses in the same order. All integers in the
10 protocol are formatted in decimal and (unless otherwise indicated)
11 nonnegative.
12
13 Names, in this protocol, are ASCII strings. They may contain letters (A-Z and
14 a-z), numerals (0-9), hyphen ("-"), plus ("+"), slash ("/"), semicolon (";"),
15 dot ("."), dollar-sign ("$"), underscore ("_"), and parentheses ("(" and ")"),
16 but they may not begin with a hyphen. They are terminated by white space
17 (either a space char or end of line). Each name must be at least one character
18 long.
19
20 The protocol contains two kinds of data: text lines and unstructured chunks of
21 data. Text lines are used for client commands and server responses. Chunks are
22 used to transfer job bodies and stats information. Each job body is an opaque
23 sequence of bytes. The server never inspects or modifies a job body and always
24 sends it back in its original form. It is up to the clients to agree on a
25 meaningful interpretation of job bodies.
26
27 The client may issue the "quit" command, or simply close the TCP connection
28 when it no longer has use for the server. However, beanstalkd performs very
29 well with a large number of open connections, so it is usually better for the
30 client to keep its connection open and reuse it as much as possible. This also
31 avoids the overhead of establishing new TCP connections.
32
33 If a client violates the protocol (such as by sending a request that is not
34 well-formed or a command that does not exist) or if the server has an error,
35 the server will reply with one of the following error messages:
36
37 - "OUT_OF_MEMORY\r\n" The server cannot allocate enough memory for the job.
38 The client should try again later.
39
40 - "INTERNAL_ERROR\r\n" This indicates a bug in the server. It should never
41 happen. If it does happen, please report it at
42 http://groups.google.com/group/beanstalk-talk.
43
44 - "BAD_FORMAT\r\n" The client sent a command line that was not well-formed.
45 This can happen if the line's length exceeds 224 bytes including \r\n,
46 if the name of a tube exceeds 200 bytes, if non-numeric
47 characters occur where an integer is expected, if the wrong number of
48 arguments are present, or if the command line is mal-formed in any other
49 way.
50
51 - "UNKNOWN_COMMAND\r\n" The client sent a command that the server does not
52 know.
53
54 These error responses will not be listed in this document for individual
55 commands in the following sections, but they are implicitly included in the
56 description of all commands. Clients should be prepared to receive an error
57 response after any command.
58
59 As a last resort, if the server has a serious error that prevents it from
60 continuing service to the current client, the server will close the
61 connection.
62
63 Job Lifecycle
64 -------------
65
66 A job in beanstalk gets created by a client with the "put" command. During its
67 life it can be in one of four states: "ready", "reserved", "delayed", or
68 "buried". After the put command, a job typically starts out ready. It waits in
69 the ready queue until a worker comes along and runs the "reserve" command. If
70 this job is next in the queue, it will be reserved for the worker. The worker
71 will execute the job; when it is finished the worker will send a "delete"
72 command to delete the job.
73
74 Here is a picture of the typical job lifecycle:
75
76
77 put reserve delete
78 -----> [READY] ---------> [RESERVED] --------> *poof*
79
80
81
82 Here is a picture with more possibilities:
83
84
85
86 put with delay release with delay
87 ----------------> [DELAYED] <------------.
88 | |
89 | (time passes) |
90 | |
91 put v reserve | delete
92 -----------------> [READY] ---------> [RESERVED] --------> *poof*
93 ^ ^ | |
94 | \ release | |
95 | `-------------' |
96 | |
97 | kick |
98 | |
99 | bury |
100 [BURIED] <---------------'
101 |
102 | delete
103 `--------> *poof*
104
105
106 The system has one or more tubes. Each tube consists of a ready queue and a
107 delay queue. Each job spends its entire life in one tube. Consumers can show
108 interest in tubes by sending the "watch" command; they can show disinterest by
109 sending the "ignore" command. This set of interesting tubes is said to be a
110 consumer's "watch list". When a client reserves a job, it may come from any of
111 the tubes in its watch list.
112
113 When a client connects, its watch list is initially just the tube named
114 "default". If it submits jobs without having sent a "use" command, they will
115 live in the tube named "default".
116
117 Tubes are created on demand whenever they are referenced. If a tube is empty
118 (that is, it contains no ready, delayed, or buried jobs) and no client refers
119 to it, it will be deleted.
120
121 Producer Commands
122 -----------------
123
124 The "put" command is for any process that wants to insert a job into the queue.
125 It comprises a command line followed by the job body:
126
127 put <pri> <delay> <ttr> <bytes>\r\n
128 <data>\r\n
129
130 It inserts a job into the client's currently used tube (see the "use" command
131 below).
132
133 - <pri> is an integer < 2**32. Jobs with smaller priority values will be
134 scheduled before jobs with larger priorities. The most urgent priority is 0;
135 the least urgent priority is 4,294,967,295.
136
137 - <delay> is an integer number of seconds to wait before putting the job in
138 the ready queue. The job will be in the "delayed" state during this time.
139 Maximum delay is 2**32-1.
140
141 - <ttr> -- time to run -- is an integer number of seconds to allow a worker
142 to run this job. This time is counted from the moment a worker reserves
143 this job. If the worker does not delete, release, or bury the job within
144 <ttr> seconds, the job will time out and the server will release the job.
145 The minimum ttr is 1. If the client sends 0, the server will silently
146 increase the ttr to 1. Maximum ttr is 2**32-1.
147
148 - <bytes> is an integer indicating the size of the job body, not including the
149 trailing "\r\n". This value must be less than max-job-size (default: 2**16).
150
151 - <data> is the job body -- a sequence of bytes of length <bytes> from the
152 previous line.
153
154 After sending the command line and body, the client waits for a reply, which
155 may be:
156
157 - "INSERTED <id>\r\n" to indicate success.
158
159 - <id> is the integer id of the new job
160
161 - "BURIED <id>\r\n" if the server ran out of memory trying to grow the
162 priority queue data structure.
163
164 - <id> is the integer id of the new job
165
166 - "EXPECTED_CRLF\r\n" The job body must be followed by a CR-LF pair, that is,
167 "\r\n". These two bytes are not counted in the job size given by the client
168 in the put command line.
169
170 - "JOB_TOO_BIG\r\n" The client has requested to put a job with a body larger
171 than max-job-size bytes.
172
173 - "DRAINING\r\n" This means that the server has been put into "drain mode" and
174 is no longer accepting new jobs. The client should try another server or
175 disconnect and try again later. To put the server in drain mode, send the
176 SIGUSR1 signal to the process.
177
178 The "use" command is for producers. Subsequent put commands will put jobs into
179 the tube specified by this command. If no use command has been issued, jobs
180 will be put into the tube named "default".
181
182 use <tube>\r\n
183
184 - <tube> is a name at most 200 bytes. It specifies the tube to use. If the
185 tube does not exist, it will be created.
186
187 The only reply is:
188
189 USING <tube>\r\n
190
191 - <tube> is the name of the tube now being used.
192
193 Worker Commands
194 ---------------
195
196 A process that wants to consume jobs from the queue uses "reserve", "delete",
197 "release", and "bury". The first worker command, "reserve", looks like this:
198
199 reserve\r\n
200
201 Alternatively, you can specify a timeout as follows:
202
203 reserve-with-timeout <seconds>\r\n
204
205 This will return a newly-reserved job. If no job is available to be reserved,
206 beanstalkd will wait to send a response until one becomes available. Once a
207 job is reserved for the client, the client has limited time to run (TTR) the
208 job before the job times out. When the job times out, the server will put the
209 job back into the ready queue. Both the TTR and the actual time left can be
210 found in response to the stats-job command.
211
212 If more than one job is ready, beanstalkd will choose the one with the
213 smallest priority value. Within each priority, it will choose the one that
214 was received first.
215
216 A timeout value of 0 will cause the server to immediately return either a
217 response or TIMED_OUT. A positive value of timeout will limit the amount of
218 time the client will block on the reserve request until a job becomes
219 available.
220
221 During the TTR of a reserved job, the last second is kept by the server as a
222 safety margin, during which the client will not be made to wait for another
223 job. If the client issues a reserve command during the safety margin, or if
224 the safety margin arrives while the client is waiting on a reserve command,
225 the server will respond with:
226
227 DEADLINE_SOON\r\n
228
229 This gives the client a chance to delete or release its reserved job before
230 the server automatically releases it.
231
232 TIMED_OUT\r\n
233
234 If a non-negative timeout was specified and the timeout exceeded before a job
235 became available, or if the client's connection is half-closed, the server
236 will respond with TIMED_OUT.
237
238 Otherwise, the only other response to this command is a successful reservation
239 in the form of a text line followed by the job body:
240
241 RESERVED <id> <bytes>\r\n
242 <data>\r\n
243
244 - <id> is the job id -- an integer unique to this job in this instance of
245 beanstalkd.
246
247 - <bytes> is an integer indicating the size of the job body, not including
248 the trailing "\r\n".
249
250 - <data> is the job body -- a sequence of bytes of length <bytes> from the
251 previous line. This is a verbatim copy of the bytes that were originally
252 sent to the server in the put command for this job.
253
254 A job can be reserved by its id. Once a job is reserved for the client,
255 the client has limited time to run (TTR) the job before the job times out.
256 When the job times out, the server will put the job back into the ready queue.
257 The command looks like this:
258
259 reserve-job <id>\r\n
260
261 - <id> is the job id to reserve
262
263 This should immediately return one of these responses:
264
265 - "NOT_FOUND\r\n" if the job does not exist or reserved by a client or
266 is not either ready, buried or delayed.
267
268 - "RESERVED <id> <bytes>\r\n<data>\r\n". See the description for
269 the reserve command.
270
271 The delete command removes a job from the server entirely. It is normally used
272 by the client when the job has successfully run to completion. A client can
273 delete jobs that it has reserved, ready jobs, delayed jobs, and jobs that are
274 buried. The delete command looks like this:
275
276 delete <id>\r\n
277
278 - <id> is the job id to delete.
279
280 The client then waits for one line of response, which may be:
281
282 - "DELETED\r\n" to indicate success.
283
284 - "NOT_FOUND\r\n" if the job does not exist or is not either reserved by the
285 client, ready, or buried. This could happen if the job timed out before the
286 client sent the delete command.
287
288 The release command puts a reserved job back into the ready queue (and marks
289 its state as "ready") to be run by any client. It is normally used when the job
290 fails because of a transitory error. It looks like this:
291
292 release <id> <pri> <delay>\r\n
293
294 - <id> is the job id to release.
295
296 - <pri> is a new priority to assign to the job.
297
298 - <delay> is an integer number of seconds to wait before putting the job in
299 the ready queue. The job will be in the "delayed" state during this time.
300
301 The client expects one line of response, which may be:
302
303 - "RELEASED\r\n" to indicate success.
304
305 - "BURIED\r\n" if the server ran out of memory trying to grow the priority
306 queue data structure.
307
308 - "NOT_FOUND\r\n" if the job does not exist or is not reserved by the client.
309
310 The bury command puts a job into the "buried" state. Buried jobs are put into a
311 FIFO linked list and will not be touched by the server again until a client
312 kicks them with the "kick" command.
313
314 The bury command looks like this:
315
316 bury <id> <pri>\r\n
317
318 - <id> is the job id to bury.
319
320 - <pri> is a new priority to assign to the job.
321
322 There are two possible responses:
323
324 - "BURIED\r\n" to indicate success.
325
326 - "NOT_FOUND\r\n" if the job does not exist or is not reserved by the client.
327
328 The "touch" command allows a worker to request more time to work on a job.
329 This is useful for jobs that potentially take a long time, but you still want
330 the benefits of a TTR pulling a job away from an unresponsive worker. A worker
331 may periodically tell the server that it's still alive and processing a job
332 (e.g. it may do this on DEADLINE_SOON). The command postpones the auto
333 release of a reserved job until TTR seconds from when the command is issued.
334
335 The touch command looks like this:
336
337 touch <id>\r\n
338
339 - <id> is the ID of a job reserved by the current connection.
340
341 There are two possible responses:
342
343 - "TOUCHED\r\n" to indicate success.
344
345 - "NOT_FOUND\r\n" if the job does not exist or is not reserved by the client.
346
347 The "watch" command adds the named tube to the watch list for the current
348 connection. A reserve command will take a job from any of the tubes in the
349 watch list. For each new connection, the watch list initially consists of one
350 tube, named "default".
351
352 watch <tube>\r\n
353
354 - <tube> is a name at most 200 bytes. It specifies a tube to add to the watch
355 list. If the tube doesn't exist, it will be created.
356
357 The reply is:
358
359 WATCHING <count>\r\n
360
361 - <count> is the integer number of tubes currently in the watch list.
362
363 The "ignore" command is for consumers. It removes the named tube from the
364 watch list for the current connection.
365
366 ignore <tube>\r\n
367
368 The reply is one of:
369
370 - "WATCHING <count>\r\n" to indicate success.
371
372 - <count> is the integer number of tubes currently in the watch list.
373
374 - "NOT_IGNORED\r\n" if the client attempts to ignore the only tube in its
375 watch list.
376
377 Other Commands
378 --------------
379
380 The peek commands let the client inspect a job in the system. There are four
381 variations. All but the first operate only on the currently used tube.
382
383 - "peek <id>\r\n" - return job <id>.
384
385 - "peek-ready\r\n" - return the next ready job.
386
387 - "peek-delayed\r\n" - return the delayed job with the shortest delay left.
388
389 - "peek-buried\r\n" - return the next job in the list of buried jobs.
390
391 There are two possible responses, either a single line:
392
393 - "NOT_FOUND\r\n" if the requested job doesn't exist or there are no jobs in
394 the requested state.
395
396 Or a line followed by a chunk of data, if the command was successful:
397
398 FOUND <id> <bytes>\r\n
399 <data>\r\n
400
401 - <id> is the job id.
402
403 - <bytes> is an integer indicating the size of the job body, not including
404 the trailing "\r\n".
405
406 - <data> is the job body -- a sequence of bytes of length <bytes> from the
407 previous line.
408
409 The kick command applies only to the currently used tube. It moves jobs into
410 the ready queue. If there are any buried jobs, it will only kick buried jobs.
411 Otherwise it will kick delayed jobs. It looks like:
412
413 kick <bound>\r\n
414
415 - <bound> is an integer upper bound on the number of jobs to kick. The server
416 will kick no more than <bound> jobs.
417
418 The response is of the form:
419
420 KICKED <count>\r\n
421
422 - <count> is an integer indicating the number of jobs actually kicked.
423
424 The kick-job command is a variant of kick that operates with a single job
425 identified by its job id. If the given job id exists and is in a buried or
426 delayed state, it will be moved to the ready queue of the the same tube where it
427 currently belongs. The syntax is:
428
429 kick-job <id>\r\n
430
431 - <id> is the job id to kick.
432
433 The response is one of:
434
435 - "NOT_FOUND\r\n" if the job does not exist or is not in a kickable state. This
436 can also happen upon internal errors.
437
438 - "KICKED\r\n" when the operation succeeded.
439
440 The stats-job command gives statistical information about the specified job if
441 it exists. Its form is:
442
443 stats-job <id>\r\n
444
445 - <id> is a job id.
446
447 The response is one of:
448
449 - "NOT_FOUND\r\n" if the job does not exist.
450
451 - "OK <bytes>\r\n<data>\r\n"
452
453 - <bytes> is the size of the following data section in bytes.
454
455 - <data> is a sequence of bytes of length <bytes> from the previous line. It
456 is a YAML file with statistical information represented by a dictionary.
457
458 The stats-job data is a YAML file representing a single dictionary of string
459 keys to scalar values. It contains these keys:
460
461 - "id" is the job id
462
463 - "tube" is the name of the tube that contains this job
464
465 - "state" is "ready" or "delayed" or "reserved" or "buried"
466
467 - "pri" is the priority value set by the put, release, or bury commands.
468
469 - "age" is the time in seconds since the put command that created this job.
470
471 - "delay" is the integer number of seconds to wait before putting this job in
472 the ready queue.
473
474 - "ttr" -- time to run -- is the integer number of seconds a worker is
475 allowed to run this job.
476
477 - "time-left" is the number of seconds left until the server puts this job
478 into the ready queue. This number is only meaningful if the job is
479 reserved or delayed. If the job is reserved and this amount of time
480 elapses before its state changes, it is considered to have timed out.
481
482 - "file" is the number of the earliest binlog file containing this job.
483 If -b wasn't used, this will be 0.
484
485 - "reserves" is the number of times this job has been reserved.
486
487 - "timeouts" is the number of times this job has timed out during a
488 reservation.
489
490 - "releases" is the number of times a client has released this job from a
491 reservation.
492
493 - "buries" is the number of times this job has been buried.
494
495 - "kicks" is the number of times this job has been kicked.
496
497 The stats-tube command gives statistical information about the specified tube
498 if it exists. Its form is:
499
500 stats-tube <tube>\r\n
501
502 - <tube> is a name at most 200 bytes. Stats will be returned for this tube.
503
504 The response is one of:
505
506 - "NOT_FOUND\r\n" if the tube does not exist.
507
508 - "OK <bytes>\r\n<data>\r\n"
509
510 - <bytes> is the size of the following data section in bytes.
511
512 - <data> is a sequence of bytes of length <bytes> from the previous line. It
513 is a YAML file with statistical information represented by a dictionary.
514
515 The stats-tube data is a YAML file representing a single dictionary of string
516 keys to scalar values. It contains these keys:
517
518 - "name" is the tube's name.
519
520 - "current-jobs-urgent" is the number of ready jobs with priority < 1024 in
521 this tube.
522
523 - "current-jobs-ready" is the number of jobs in the ready queue in this tube.
524
525 - "current-jobs-reserved" is the number of jobs reserved by all clients in
526 this tube.
527
528 - "current-jobs-delayed" is the number of delayed jobs in this tube.
529
530 - "current-jobs-buried" is the number of buried jobs in this tube.
531
532 - "total-jobs" is the cumulative count of jobs created in this tube in
533 the current beanstalkd process.
534
535 - "current-using" is the number of open connections that are currently
536 using this tube.
537
538 - "current-waiting" is the number of open connections that have issued a
539 reserve command while watching this tube but not yet received a response.
540
541 - "current-watching" is the number of open connections that are currently
542 watching this tube.
543
544 - "pause" is the number of seconds the tube has been paused for.
545
546 - "cmd-delete" is the cumulative number of delete commands for this tube
547
548 - "cmd-pause-tube" is the cumulative number of pause-tube commands for this
549 tube.
550
551 - "pause-time-left" is the number of seconds until the tube is un-paused.
552
553 The stats command gives statistical information about the system as a whole.
554 Its form is:
555
556 stats\r\n
557
558 The server will respond:
559
560 OK <bytes>\r\n
561 <data>\r\n
562
563 - <bytes> is the size of the following data section in bytes.
564
565 - <data> is a sequence of bytes of length <bytes> from the previous line. It
566 is a YAML file with statistical information represented by a dictionary.
567
568 The stats data for the system is a YAML file representing a single dictionary
569 of string keys to scalar values. Entries described as "cumulative" are reset
570 when the beanstalkd process starts;
571 they are not stored on disk with the -b flag.
572
573 - "current-jobs-urgent" is the number of ready jobs with priority < 1024.
574
575 - "current-jobs-ready" is the number of jobs in the ready queue.
576
577 - "current-jobs-reserved" is the number of jobs reserved by all clients.
578
579 - "current-jobs-delayed" is the number of delayed jobs.
580
581 - "current-jobs-buried" is the number of buried jobs.
582
583 - "cmd-put" is the cumulative number of put commands.
584
585 - "cmd-peek" is the cumulative number of peek commands.
586
587 - "cmd-peek-ready" is the cumulative number of peek-ready commands.
588
589 - "cmd-peek-delayed" is the cumulative number of peek-delayed commands.
590
591 - "cmd-peek-buried" is the cumulative number of peek-buried commands.
592
593 - "cmd-reserve" is the cumulative number of reserve commands.
594
595 - "cmd-reserve-with-timeout" is the cumulative number of reserve-with-timeout commands.
596
597 - "cmd-touch" is the cumulative number of touch commands.
598
599 - "cmd-use" is the cumulative number of use commands.
600
601 - "cmd-watch" is the cumulative number of watch commands.
602
603 - "cmd-ignore" is the cumulative number of ignore commands.
604
605 - "cmd-delete" is the cumulative number of delete commands.
606
607 - "cmd-release" is the cumulative number of release commands.
608
609 - "cmd-bury" is the cumulative number of bury commands.
610
611 - "cmd-kick" is the cumulative number of kick commands.
612
613 - "cmd-stats" is the cumulative number of stats commands.
614
615 - "cmd-stats-job" is the cumulative number of stats-job commands.
616
617 - "cmd-stats-tube" is the cumulative number of stats-tube commands.
618
619 - "cmd-list-tubes" is the cumulative number of list-tubes commands.
620
621 - "cmd-list-tube-used" is the cumulative number of list-tube-used commands.
622
623 - "cmd-list-tubes-watched" is the cumulative number of list-tubes-watched
624 commands.
625
626 - "cmd-pause-tube" is the cumulative number of pause-tube commands.
627
628 - "job-timeouts" is the cumulative count of times a job has timed out.
629
630 - "total-jobs" is the cumulative count of jobs created.
631
632 - "max-job-size" is the maximum number of bytes in a job.
633
634 - "current-tubes" is the number of currently-existing tubes.
635
636 - "current-connections" is the number of currently open connections.
637
638 - "current-producers" is the number of open connections that have each
639 issued at least one put command.
640
641 - "current-workers" is the number of open connections that have each issued
642 at least one reserve command.
643
644 - "current-waiting" is the number of open connections that have issued a
645 reserve command but not yet received a response.
646
647 - "total-connections" is the cumulative count of connections.
648
649 - "pid" is the process id of the server.
650
651 - "version" is the version string of the server.
652
653 - "rusage-utime" is the cumulative user CPU time of this process in seconds
654 and microseconds.
655
656 - "rusage-stime" is the cumulative system CPU time of this process in
657 seconds and microseconds.
658
659 - "uptime" is the number of seconds since this server process started running.
660
661 - "binlog-oldest-index" is the index of the oldest binlog file needed to
662 store the current jobs.
663
664 - "binlog-current-index" is the index of the current binlog file being
665 written to. If binlog is not active this value will be 0.
666
667 - "binlog-max-size" is the maximum size in bytes a binlog file is allowed
668 to get before a new binlog file is opened.
669
670 - "binlog-records-written" is the cumulative number of records written
671 to the binlog.
672
673 - "binlog-records-migrated" is the cumulative number of records written
674 as part of compaction.
675
676 - "draining" is set to "true" if the server is in drain mode,
677 "false" otherwise.
678
679 - "id" is a random id string for this server process, generated every time
680 beanstalkd process starts.
681
682 - "hostname" is the hostname of the machine as determined by uname.
683
684 - "os" is the OS version as determined by uname
685
686 - "platform" is the machine architecture as determined by uname
687
688 The list-tubes command returns a list of all existing tubes. Its form is:
689
690 list-tubes\r\n
691
692 The response is:
693
694 OK <bytes>\r\n
695 <data>\r\n
696
697 - <bytes> is the size of the following data section in bytes.
698
699 - <data> is a sequence of bytes of length <bytes> from the previous line. It
700 is a YAML file containing all tube names as a list of strings.
701
702 The list-tube-used command returns the tube currently being used by the
703 client. Its form is:
704
705 list-tube-used\r\n
706
707 The response is:
708
709 USING <tube>\r\n
710
711 - <tube> is the name of the tube being used.
712
713 The list-tubes-watched command returns a list tubes currently being watched by
714 the client. Its form is:
715
716 list-tubes-watched\r\n
717
718 The response is:
719
720 OK <bytes>\r\n
721 <data>\r\n
722
723 - <bytes> is the size of the following data section in bytes.
724
725 - <data> is a sequence of bytes of length <bytes> from the previous line. It
726 is a YAML file containing watched tube names as a list of strings.
727
728 The quit command simply closes the connection. Its form is:
729
730 quit\r\n
731
732 The pause-tube command can delay any new job being reserved for a given time. Its form is:
733
734 pause-tube <tube-name> <delay>\r\n
735
736 - <tube-name> is the tube to pause
737
738 - <delay> is an integer number of seconds < 2**32 to wait before reserving any more
739 jobs from the queue
740
741 There are two possible responses:
742
743 - "PAUSED\r\n" to indicate success.
744
745 - "NOT_FOUND\r\n" if the tube does not exist.
746