1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! This crate provides a client for working with [Beanstalkd](https://beanstalkd.github.io/), a simple
//! fast work queue.
//
//! # About Beanstalkd
//!
//! Beanstalkd is a simple fast work queue. It works at the TCP connection level, considering each TCP
//! connection individually. A worker may have multiple connections to the Beanstalkd server and each
//! connection will be considered separate.
//!
//! The protocol is ASCII text based but the data itself is just a bytestream. This means that the
//! application is responsible for interpreting the data.
//!
//! ## Operation
//! This library can serve as a client for both the application and the worker. The application would
//! `put` jobs on the queue and the workers can `reserve` them. Once they are done with the job, they
//! have to `delete` job. This is required for every job, or else beanstlkd will not remove it from
//! its internal datastructres.
//!
//! If a worker cannot finish the job in it's TTR (Time To Run), then it can `release` the job. The
//! application can use the `using` method to put jobs in a specific tube, and workers can use `watch`
//! to only reserve jobs from the specified tubes.
//!
//! ## Interaction with Tokio
//!
//! The futures in this crate expect to be running under a `tokio::Runtime`. In the common case,
//! you cannot resolve them solely using `.wait()`, but should instead use `tokio::run` or
//! explicitly create a `tokio::Runtime` and then use `Runtime::block_on`.
//!
//! A contrived example
//!
//! ```no_run
//! extern crate tokio;
//! extern crate futures;
//! extern crate tokio_beanstalkd;
//!
//! use tokio::prelude::*;
//! use tokio_beanstalkd::*;
//!
//! # fn consumer_commands() {
//!      let mut rt = tokio::runtime::Runtime::new().unwrap();
//!      let bean = rt.block_on(
//!          Beanstalkd::connect(&"127.0.0.1:11300".parse().unwrap()).and_then(|bean| {
//!              bean.put(0, 1, 1, &b"data"[..])
//!                  .inspect(|(_, response)| assert!(response.is_ok()))
//!                  .and_then(|(bean, _)| bean.reserve())
//!                  .inspect(|(_, response)| match response {
//!                      Ok(Response::Reserved(j)) => {
//!                          assert_eq!(j.data, b"data");
//!                      }
//!                      _ => panic!("Wrong response received"),
//!                  })
//!                  .and_then(|(bean, response)| match response {
//!                      Ok(Response::Reserved(j)) => bean.touch(j.id),
//!                      Ok(_) => panic!("Wrong response returned"),
//!                      Err(e) => panic!("Got error: {}", e),
//!                  })
//!                  .inspect(|(_, response)| match response {
//!                      Ok(v) => assert_eq!(*v, Response::Touched),
//!                      Err(e) => panic!("Got error: {}", e),
//!                  })
//!                  .and_then(|(bean, _)| {
//!                      // how about another one?
//!                      bean.put(0, 1, 1, &b"more data"[..])
//!                  })
//!                  .and_then(|(bean, _)| bean.reserve())
//!                  .and_then(|(bean, response)| match response {
//!                      Ok(Response::Reserved(job)) => bean.release(job.id, 10, 10),
//!                      Ok(_) => panic!("Wrong response returned"),
//!                      Err(e) => panic!("Got error: {}", e),
//!                  })
//!                  .inspect(|(_, response)| match response {
//!                      Ok(v) => assert_eq!(*v, Response::Released),
//!                      Err(e) => panic!("Got error: {}", e),
//!                  })
//!                  .and_then(|(bean, _)| bean.reserve())
//!                  .and_then(|(bean, response)| match response {
//!                      Ok(Response::Reserved(job)) => bean.bury(job.id, 10),
//!                      Ok(_) => panic!("Wrong response returned"),
//!                      Err(e) => panic!("Got error: {}", e),
//!                  })
//!                  .inspect(|(_, response)| match response {
//!                      Ok(v) => assert_eq!(*v, Response::Buried),
//!                      Err(e) => panic!("Got error: {}", e),
//!                  })
//!                  .and_then(|(bean, _)| {
//!                      // how about another one?
//!                      bean.put(0, 1, 1, &b"more data"[..])
//!                  })
//!                  .inspect(|(_, response)| assert!(response.is_ok()))
//!                  .and_then(|(bean, response)| match response {
//!                      Ok(Response::Inserted(id)) => bean.delete(id),
//!                      Ok(_) => panic!("Wrong response returned"),
//!                      Err(e) => panic!("Got error: {}", e),
//!                  })
//!                  .inspect(|(_, response)| match response {
//!                      Ok(v) => assert_eq!(*v, Response::Deleted),
//!                      Err(e) => {
//!                          // assert_eq!(*e, error::Consumer::NotFound);
//!                          panic!("Got error: {}", e)
//!                      }
//!                  })
//!                  .and_then(|(bean, _)| bean.watch("test"))
//!                  .inspect(|(_, response)| match response {
//!                      Ok(v) => assert_eq!(*v, Response::Watching(2)),
//!                      Err(e) => panic!("Got error: {}", e),
//!                  })
//!                  .and_then(|(bean, _)| bean.ignore("test"))
//!                  .inspect(|(_, response)| match response {
//!                      Ok(v) => assert_eq!(*v, Response::Watching(1)),
//!                      Err(e) => panic!("Got error: {}", e),
//!                  })
//!          }),
//!      );
//!      assert!(!bean.is_err());
//!      drop(bean);
//!      rt.shutdown_on_idle();
//! # }
//! ```

extern crate bytes;
extern crate futures;
#[macro_use]
extern crate failure;
extern crate tokio;

mod proto;

use tokio::codec::Framed;
use tokio::prelude::*;

use std::borrow::Cow;
use std::net::SocketAddr;

pub use proto::error;
pub use proto::Response;
// Request doesn't have to be a public type
use proto::Request;

macro_rules! handle_response {
    ($input:ident) => {
        $input.into_future().then(|val| match val {
            Ok((Some(val), conn)) => Ok((Beanstalkd { connection: conn }, Ok(val))),
            // None is only returned when the stream is closed
            Ok((None, _)) => bail!("Stream closed"),
            Err((e, conn)) => Ok((Beanstalkd { connection: conn }, Err(e))),
        })
    };
}

/// Connection to the Beanstalkd server.
///
/// All interactions with Beanstalkd happen by calling methods on a `Beanstalkd` instance.
///
/// Even though there is a `quit` command, Beanstalkd consideres a closed connection as the
/// end of communication, so just dropping this struct will close the connection.
#[derive(Debug)]
pub struct Beanstalkd {
    connection: Framed<tokio::net::TcpStream, proto::CommandCodec>,
}

impl Beanstalkd {
    /// Connect to a Beanstalkd instance.
    ///
    /// A successful TCP connect is considered the start of communication.
    pub fn connect(addr: &SocketAddr) -> impl Future<Item = Self, Error = failure::Error> {
        tokio::net::TcpStream::connect(addr)
            .map_err(failure::Error::from)
            .map(|stream| Beanstalkd::setup(stream))
    }

    fn setup(stream: tokio::net::TcpStream) -> Self {
        let bean = Framed::new(stream, proto::CommandCodec::new());
        Beanstalkd { connection: bean }
    }

    /// The "put" command is for any process that wants to insert a job into the queue.
    ///
    /// It inserts a job into the client's currently used tube (see the `use` command
    /// below).
    ///
    /// - `priority` is an integer < 2**32. Jobs with smaller priority values will be
    ///   scheduled before jobs with larger priorities. The most urgent priority is 0;
    ///   the least urgent priority is 4,294,967,295.

    /// - `delay` is an integer number of seconds to wait before putting the job in
    ///   the ready queue. The job will be in the "delayed" state during this time.

    /// - `ttr` -- time to run -- is an integer number of seconds to allow a worker
    ///   to run this job. This time is counted from the moment a worker reserves
    ///   this job. If the worker does not delete, release, or bury the job within
    ///   `ttr` seconds, the job will time out and the server will release the job.
    ///   The minimum ttr is 1. If the client sends 0, the server will silently
    ///   increase the ttr to 1.

    /// - `data` is the job body -- a sequence of bytes of length <bytes> from the
    ///   previous line.
    ///
    /// After sending the command line and body, the client waits for a reply, which
    /// may be:
    ///
    ///  - [Inserted(Id)](enum.Response.html#variant.Inserted) to indicate success.
    ///
    ///    - <id> is the integer id of the new job
    pub fn put<D>(
        self,
        priority: u32,
        delay: u32,
        ttr: u32,
        data: D,
    ) -> impl Future<Item = (Self, Result<Response, failure::Error>), Error = failure::Error>
    where
        D: Into<Cow<'static, [u8]>>,
    {
        let data = data.into();
        self.connection
            .send(proto::Request::Put {
                priority,
                delay,
                ttr,
                data,
            })
            .and_then(|conn| handle_response!(conn))
    }

    /// Reserve a job to process.
    ///
    /// A process that wants to consume jobs from the queue uses `reserve`,
    /// [delete](struct.Beanstalkd.html#method.delete),
    /// [release](struct.Beanstalkd.html#method.release), and
    /// [bury](struct.Beanstalkd.html#method.bury).
    ///
    /// The only successful response to this command is a successful reservation:
    ///
    /// - [Reserved(Job)](enum.Response.html#variant.Reserved)
    pub fn reserve(
        self,
    ) -> impl Future<Item = (Self, Result<Response, failure::Error>), Error = failure::Error> {
        self.connection
            .send(proto::Request::Reserve)
            .and_then(|conn| handle_response!(conn))
    }

    /// The "use" command is for producers. Subsequent put commands will put jobs into
    /// the tube specified by this command. If no use command has been issued, jobs
    /// will be put into the tube named "default".
    ///
    /// - `tube` is a name at most 200 bytes. It specifies the tube to use. If the
    ///   tube does not exist, it will be created.
    ///
    /// The only reply is:
    ///
    /// - [Using(Tube)](enum.Response.html#variant.Using)
    pub fn using(
        self,
        tube: &'static str,
    ) -> impl Future<Item = (Self, Result<Response, failure::Error>), Error = failure::Error> {
        self.connection
            .send(Request::Use { tube })
            .and_then(|conn| handle_response!(conn))
    }

    /// The delete command removes a job from the server entirely. It is normally used
    /// by the client when the job has successfully run to completion. A client can
    /// delete jobs that it has reserved, ready jobs, delayed jobs, and jobs that are
    /// buried.
    ///
    ///  - `id` is the job id to delete.
    ///
    /// A successful response is:
    ///
    /// - [Deleted](enum.Response.html#variant.Deleted)
    pub fn delete(
        self,
        id: u32,
    ) -> impl Future<Item = (Self, Result<Response, failure::Error>), Error = failure::Error> {
        self.connection
            .send(Request::Delete { id })
            .and_then(|conn| handle_response!(conn))
    }

    /// The release command puts a reserved job back into the ready queue (and marks
    /// its state as "ready") to be run by any client. It is normally used when the job
    /// fails because of a transitory error.
    ///
    ///  - `id` is the job id to release.
    ///
    /// - `pri` is a new priority to assign to the job.
    ///
    /// - `delay` is an integer number of seconds to wait before putting the job in
    ///   the ready queue. The job will be in the "delayed" state during this time.
    ///
    /// The successful response is:
    ///
    /// - [Released](enum.Response.html#variant.Released)
    pub fn release(
        self,
        id: u32,
        priority: u32,
        delay: u32,
    ) -> impl Future<Item = (Self, Result<Response, failure::Error>), Error = failure::Error> {
        self.connection
            .send(Request::Release {
                id,
                priority,
                delay,
            })
            .and_then(|conn| {
                conn.into_future().then(|val| match val {
                    // Since both release and bury can get BURIED in the response from the server, but
                    // in the case of release, it is an error, handle it appropriately.
                    Ok((Some(Response::Released), conn)) => {
                        Ok((Beanstalkd { connection: conn }, Ok(Response::Released)))
                    }
                    Ok((Some(Response::Buried), conn)) => Ok((
                        Beanstalkd { connection: conn },
                        Err(failure::Error::from(error::Consumer::Buried)),
                    )),
                    // This should never happen
                    Ok((Some(_), _)) => bail!("Wrong response from server"),
                    // None is only returned when the stream is closed
                    Ok((None, _)) => bail!("Stream closed"),
                    Err((e, conn)) => Ok((Beanstalkd { connection: conn }, Err(e))),
                })
            })
    }

    /// The "touch" command allows a worker to request more time to work on a job.
    /// This is useful for jobs that potentially take a long time, but you still want
    /// the benefits of a TTR pulling a job away from an unresponsive worker.  A worker
    /// may periodically tell the server that it's still alive and processing a job
    /// (e.g. it may do this on DEADLINE_SOON). The command postpones the auto
    /// release of a reserved job until TTR seconds from when the command is issued.
    ///
    /// - `id` is the ID of a job reserved by the current connection.
    ///
    /// A successful response is:
    ///
    /// - [Touched](enum.Response.html#variant.Touched)
    pub fn touch(
        self,
        id: u32,
    ) -> impl Future<Item = (Self, Result<Response, failure::Error>), Error = failure::Error> {
        self.connection
            .send(Request::Touch { id })
            .and_then(|conn| handle_response!(conn))
    }

    /// The bury command puts a job into the "buried" state. Buried jobs are put into a
    /// FIFO linked list and will not be touched by the server again until a client
    /// kicks them with the "kick" command.
    ///
    ///  - `id` is the job id to release.
    ///
    /// - `prioritiy` is a new priority to assign to the job.
    ///
    /// The successful response is:
    ///
    /// - [Buried(Id)](enum.Response.html#variant.Buried)
    pub fn bury(
        self,
        id: u32,
        priority: u32,
    ) -> impl Future<Item = (Self, Result<Response, failure::Error>), Error = failure::Error> {
        self.connection
            .send(Request::Bury { id, priority })
            .and_then(|conn| handle_response!(conn))
    }

    /// The "watch" command adds the named tube to the watch list for the current
    /// connection. A reserve command will take a job from any of the tubes in the
    /// watch list. For each new connection, the watch list initially consists of one
    /// tube, named "default".
    ///
    ///  - <tube> is a name at most 200 bytes. It specifies a tube to add to the watch
    ///     list. If the tube doesn't exist, it will be created.
    ///
    /// A successful response is:
    ///
    /// - [Watching(u32)](enum.Response.html#variant.Watching) The value returned is the
    ///     count of the tubes being watched by the current connection.
    pub fn watch(
        self,
        tube: &'static str,
    ) -> impl Future<Item = (Self, Result<Response, failure::Error>), Error = failure::Error> {
        self.connection
            .send(Request::Watch { tube })
            .and_then(|conn| handle_response!(conn))
    }

    /// The "ignore" command is for consumers. It removes the named tube from the
    /// watch list for the current connection.
    ///
    ///  - <tube> is a name at most 200 bytes. It specifies a tube to add to the watch
    ///     list. If the tube doesn't exist, it will be created.
    ///
    /// A successful response is:
    ///
    /// - [Watching(u32)](enum.Response.html#variant.Watching) The value returned is the
    ///     count of the tubes being watched by the current connection.
    pub fn ignore(
        self,
        tube: &'static str,
    ) -> impl Future<Item = (Self, Result<Response, failure::Error>), Error = failure::Error> {
        self.connection
            .send(Request::Ignore { tube })
            .and_then(|conn| handle_response!(conn))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::process::Command;

    static mut SPAWNED: bool = false;

    // Simple function to make sure only one instance of beanstalkd is spawned.
    // Really sketchy..
    unsafe fn spawn_beanstalkd() {
        if !SPAWNED {
            Command::new("beanstalkd")
                .spawn()
                .expect("Unable to spawn server");
            SPAWNED = true;
        }
    }

    #[test]
    fn it_works() {
        unsafe {
            spawn_beanstalkd();
        }
        let mut rt = tokio::runtime::Runtime::new().unwrap();
        let bean = rt.block_on(
            Beanstalkd::connect(&"127.0.0.1:11300".parse().unwrap()).and_then(|bean| {
                // Let put a job in
                bean.put(0, 1, 100, &b"data"[..])
                    .inspect(|(_, response)| assert!(response.is_ok()))
                    .and_then(|(bean, _)| {
                        // how about another one?
                        bean.put(0, 1, 100, &b"more data"[..])
                    })
                    .inspect(|(_, response)| assert!(response.is_ok()))
                    .and_then(|(bean, _)| {
                        // Let's watch a particular tube
                        bean.using("test")
                    })
                    .inspect(|(_, response)| match response {
                        Ok(v) => assert_eq!(*v, Response::Using("test".to_string())),
                        Err(e) => panic!("Unexpected error: {}", e),
                    })
            }),
        );
        assert!(!bean.is_err());
        drop(bean);
        rt.shutdown_on_idle();
    }

    #[test]
    fn consumer_commands() {
        unsafe {
            spawn_beanstalkd();
        }
        let mut rt = tokio::runtime::Runtime::new().unwrap();
        let bean = rt.block_on(
            Beanstalkd::connect(&"127.0.0.1:11300".parse().unwrap()).and_then(|bean| {
                bean.put(0, 1, 100, &b"data"[..])
                    .inspect(|(_, response)| assert!(response.is_ok()))
                    .and_then(|(bean, _)| bean.reserve())
                    .inspect(|(_, response)| match response {
                        Ok(Response::Reserved(j)) => {
                            assert_eq!(j.data, b"data");
                        }
                        _ => panic!("Wrong response received"),
                    })
                    .and_then(|(bean, response)| match response {
                        Ok(Response::Reserved(j)) => bean.touch(j.id),
                        Ok(_) => panic!("Wrong response returned"),
                        Err(e) => panic!("Got error: {}", e),
                    })
                    .inspect(|(_, response)| match response {
                        Ok(v) => assert_eq!(*v, Response::Touched),
                        Err(e) => panic!("Got error: {}", e),
                    })
                    .and_then(|(bean, _)| {
                        // how about another one?
                        bean.put(0, 1, 100, &b"more data"[..])
                    })
                    .and_then(|(bean, _)| bean.reserve())
                    .and_then(|(bean, response)| match response {
                        Ok(Response::Reserved(job)) => bean.release(job.id, 10, 10),
                        Ok(_) => panic!("Wrong response returned"),
                        Err(e) => panic!("Got error: {}", e),
                    })
                    .inspect(|(_, response)| match response {
                        Ok(v) => assert_eq!(*v, Response::Released),
                        Err(e) => panic!("Got error: {}", e),
                    })
                    .and_then(|(bean, _)| bean.reserve())
                    .and_then(|(bean, response)| match response {
                        Ok(Response::Reserved(job)) => bean.bury(job.id, 10),
                        Ok(_) => panic!("Wrong response returned"),
                        Err(e) => panic!("Got error: {}", e),
                    })
                    .inspect(|(_, response)| match response {
                        Ok(v) => assert_eq!(*v, Response::Buried),
                        Err(e) => panic!("Got error: {}", e),
                    })
                    .and_then(|(bean, _)| {
                        // how about another one?
                        bean.put(0, 1, 100, &b"more data"[..])
                    })
                    .inspect(|(_, response)| assert!(response.is_ok()))
                    .and_then(|(bean, response)| match response {
                        Ok(Response::Inserted(id)) => bean.delete(id),
                        Ok(_) => panic!("Wrong response returned"),
                        Err(e) => panic!("Got error: {}", e),
                    })
                    .inspect(|(_, response)| match response {
                        Ok(v) => assert_eq!(*v, Response::Deleted),
                        Err(e) => {
                            // assert_eq!(*e, error::Consumer::NotFound);
                            panic!("Got error: {}", e)
                        }
                    })
                    .and_then(|(bean, _)| bean.watch("test"))
                    .inspect(|(_, response)| match response {
                        Ok(v) => assert_eq!(*v, Response::Watching(2)),
                        Err(e) => panic!("Got error: {}", e),
                    })
                    .and_then(|(bean, _)| bean.ignore("test"))
                    .inspect(|(_, response)| match response {
                        Ok(v) => assert_eq!(*v, Response::Watching(1)),
                        Err(e) => panic!("Got error: {}", e),
                    })
            }),
        );
        assert!(!bean.is_err());
        drop(bean);
        rt.shutdown_on_idle();
    }
}