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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
use std::collections::HashMap;
use std::net::TcpStream;
use std::net::ToSocketAddrs;
use std::time::Duration;

use bufstream::BufStream;

use crate::command;
use crate::config::*;
use crate::error::{BeanstalkcError, BeanstalkcResult};
use crate::job::Job;
use crate::request::Request;
use crate::response::Response;

/// `Beanstalkc` provides beanstalkd client operations.
#[derive(Debug)]
pub struct Beanstalkc {
    host: String,
    port: u16,
    connection_timeout: Option<Duration>,
    stream: Option<BufStream<TcpStream>>,
}

impl Beanstalkc {
    /// Create a new `Beanstalkc` instance with default configs.
    /// Default connection address is `localhost:11300`
    pub fn new() -> Beanstalkc {
        Beanstalkc {
            host: DEFAULT_HOST.to_string(),
            port: DEFAULT_PORT,
            connection_timeout: DEFAULT_CONNECTION_TIMEOUT,
            stream: None,
        }
    }

    /// Change host to beanstalkd server.
    ///
    /// # Example:
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().host("localhost").connect().unwrap();
    /// ```
    pub fn host(mut self, host: &str) -> Self {
        self.host = host.to_string();
        self
    }

    /// Change port to beanstalkd server.
    ///
    /// # Example:
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().port(12345).connect().unwrap();
    /// ```
    pub fn port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }

    /// Set timeout for TCP connection to beanstalkd server.
    /// Default connection timeout is `120s`.
    ///
    /// # Example:
    ///
    /// ```no_run
    /// use std::time::Duration;
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new()
    ///        .connection_timeout(Some(Duration::from_secs(10)))
    ///        .connect()
    ///        .unwrap();
    /// ```
    pub fn connection_timeout(mut self, timeout: Option<Duration>) -> Self {
        self.connection_timeout = timeout;
        self
    }

    /// Connect to a running beanstalkd server.
    ///
    /// # Examples
    ///
    /// Basic usage
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    /// ```
    ///
    /// With custom configurations
    ///
    /// ```no_run
    /// use std::time::Duration;
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new()
    ///        .host("127.0.0.1")
    ///        .port(11300)
    ///        .connection_timeout(Some(Duration::from_secs(5)))
    ///        .connect()
    ///        .unwrap();
    /// ```
    pub fn connect(mut self) -> BeanstalkcResult<Self> {
        let addr = format!("{}:{}", self.host, self.port);
        let tcp_stream = match self.connection_timeout {
            Some(timeout) => {
                let addresses: Vec<_> = addr
                    .to_socket_addrs()
                    .unwrap_or_else(|_| panic!("failed to parse address: {}", addr))
                    .filter(|x| x.is_ipv4())
                    .collect();
                // FIXME: maybe we should try every possible addresses?
                TcpStream::connect_timeout(&addresses.first().unwrap(), timeout)?
            }
            None => TcpStream::connect(&addr)?,
        };
        self.stream = Some(BufStream::new(tcp_stream));
        Ok(self)
    }

    /// Close connection to remote server.
    #[allow(unused_must_use)]
    fn close(&mut self) {
        self.send(command::quit());
    }

    /// Re-connect to the beanstalkd server.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    /// let mut conn = conn.reconnect().unwrap();
    /// ```
    pub fn reconnect(mut self) -> BeanstalkcResult<Self> {
        self.close();
        self.connect()
    }

    /// Put a job into the current tube with default configs. Return job id.
    ///
    /// # Example:
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let job_id = conn.put_default(b"Rust").unwrap();
    /// ```
    pub fn put_default(&mut self, body: &[u8]) -> BeanstalkcResult<u64> {
        self.put(
            body,
            DEFAULT_JOB_PRIORITY,
            DEFAULT_JOB_DELAY,
            DEFAULT_JOB_TTR,
        )
    }

    /// Put a job into the current tube and return the job id.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use std::time::Duration;
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let job_id = conn.put(
    ///        b"Rust",
    ///        0,
    ///        Duration::from_secs(1),
    ///        Duration::from_secs(10),
    ///    );
    /// ```
    pub fn put(
        &mut self,
        body: &[u8],
        priority: u32,
        delay: Duration,
        ttr: Duration,
    ) -> BeanstalkcResult<u64> {
        self.send(command::put(body, priority, delay, ttr))
            .and_then(|r| r.job_id())
    }

    /// Reserve a job from one of those watched tubes. Return a `Job` object if it succeeds.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let mut job = conn.reserve().unwrap();
    /// // Execute job...
    /// dbg!(job.id());
    /// dbg!(job.body());
    ///
    /// job.delete().unwrap();
    /// ```
    pub fn reserve(&mut self) -> BeanstalkcResult<Job> {
        let resp = self.send(command::reserve(None))?;
        Ok(Job::new(
            self,
            resp.job_id()?,
            resp.body.unwrap_or_default(),
            true,
        ))
    }

    /// Reserve a job with given timeout from one of those watched tubes.
    /// Return a `Job` object if it succeeds.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    /// use std::time::Duration;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let mut job = conn.reserve_with_timeout(Duration::from_secs(10)).unwrap();
    /// // Execute job...
    /// dbg!(job.id());
    /// dbg!(job.body());
    ///
    /// job.delete().unwrap();
    /// ```
    pub fn reserve_with_timeout(&mut self, timeout: Duration) -> BeanstalkcResult<Job> {
        let resp = self.send(command::reserve(Some(timeout)))?;
        Ok(Job::new(
            self,
            resp.job_id()?,
            resp.body.unwrap_or_default(),
            true,
        ))
    }

    /// Kick at most `bound` jobs into the ready queue.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// assert_eq!(10, conn.kick(10).unwrap());
    /// ```
    pub fn kick(&mut self, bound: u32) -> BeanstalkcResult<u64> {
        self.send(command::kick(bound))
            .and_then(|r| r.get_int_param(0))
    }

    /// Kick a specific job into the ready queue.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// conn.kick(123).unwrap();
    /// ```
    pub fn kick_job(&mut self, job_id: u64) -> BeanstalkcResult<()> {
        self.send(command::kick_job(job_id)).map(|_| ())
    }

    /// Return a specific job.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let mut job = conn.peek(1).unwrap();
    /// assert_eq!(1, job.id());
    /// ```
    pub fn peek(&mut self, job_id: u64) -> BeanstalkcResult<Job> {
        self.do_peek(command::peek_job(job_id))
    }

    /// Return the next ready job.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let mut job = conn.peek_ready().unwrap();
    /// dbg!(job.id());
    /// dbg!(job.body());
    /// ```
    pub fn peek_ready(&mut self) -> BeanstalkcResult<Job> {
        self.do_peek(command::peek_ready())
    }

    /// Return the delayed job with the shortest delay left.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let mut job = conn.peek_delayed().unwrap();
    /// dbg!(job.id());
    /// dbg!(job.body());
    /// ```
    pub fn peek_delayed(&mut self) -> BeanstalkcResult<Job> {
        self.do_peek(command::peek_delayed())
    }

    /// Return the next job in the list of buried jobs.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let mut job = conn.peek_buried().unwrap();
    /// dbg!(job.id());
    /// dbg!(job.body());
    /// ```
    pub fn peek_buried(&mut self) -> BeanstalkcResult<Job> {
        self.do_peek(command::peek_buried())
    }

    pub fn do_peek(&mut self, cmd: command::Command) -> BeanstalkcResult<Job> {
        let resp = self.send(cmd)?;
        Ok(Job::new(
            self,
            resp.job_id()?,
            resp.body.unwrap_or_default(),
            false,
        ))
    }

    /// Return a list of all existing tubes.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let tubes = conn.tubes().unwrap();
    /// assert!(tubes.contains(&String::from("default")));
    /// ```
    pub fn tubes(&mut self) -> BeanstalkcResult<Vec<String>> {
        self.send(command::tubes()).map(|r| r.body_as_vec())
    }

    /// Return the tube currently being used.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let tube = conn.using().unwrap();
    /// assert_eq!("default".to_string(), tube);
    /// ```
    pub fn using(&mut self) -> BeanstalkcResult<String> {
        self.send(command::using()).and_then(|r| r.get_param(0))
    }

    /// Use a given tube.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let tube = conn.use_tube("jobs").unwrap();
    /// assert_eq!("jobs".to_string(), tube);
    /// ```
    pub fn use_tube(&mut self, name: &str) -> BeanstalkcResult<String> {
        self.send(command::use_tube(name))
            .and_then(|r| r.get_param(0))
    }

    /// Return a list of tubes currently being watched.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let tubes = conn.watching().unwrap();
    /// assert_eq!(vec!["default".to_string()], tubes);
    /// ```
    pub fn watching(&mut self) -> BeanstalkcResult<Vec<String>> {
        self.send(command::watching()).map(|r| r.body_as_vec())
    }

    /// Watch a specific tube.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let watched_count = conn.watch("foo").unwrap();
    /// assert_eq!(2, watched_count);
    /// ```
    pub fn watch(&mut self, name: &str) -> BeanstalkcResult<u64> {
        self.send(command::watch(name))
            .and_then(|r| r.get_int_param(0))
    }

    /// Stop watching a specific tube.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    /// conn.ignore("foo").unwrap();
    /// ```
    pub fn ignore(&mut self, name: &str) -> BeanstalkcResult<u64> {
        self.send(command::ignore(name))
            .and_then(|r| r.get_int_param(0))
    }

    /// Return a dict of statistical information about the beanstalkd server.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// dbg!(conn.stats().unwrap());
    /// ```
    pub fn stats(&mut self) -> BeanstalkcResult<HashMap<String, String>> {
        self.send(command::stats()).map(|r| r.body_as_map())
    }

    /// Return a dict of statistical information about the specified tube.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// dbg!(conn.stats_tube("default").unwrap());
    /// ```
    pub fn stats_tube(&mut self, name: &str) -> BeanstalkcResult<HashMap<String, String>> {
        self.send(command::stats_tube(name))
            .map(|r| r.body_as_map())
    }

    /// Pause the specific tube for `delay` time.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use std::time::Duration;
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    /// conn.pause_tube("default", Duration::from_secs(100));
    /// ```
    pub fn pause_tube(&mut self, name: &str, delay: Duration) -> BeanstalkcResult<()> {
        self.send(command::pause_tube(name, delay)).map(|_| ())
    }

    /// Delete job by job id.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// conn.delete(123).unwrap();
    ///
    /// let mut job = conn.reserve().unwrap();
    /// // Recommended way to delete a job
    /// job.delete().unwrap();
    /// ```
    pub fn delete(&mut self, job_id: u64) -> BeanstalkcResult<()> {
        self.send(command::delete(job_id)).map(|_| ())
    }

    /// Release a reserved job back into the ready queue with default priority and delay.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// conn.release_default(1).unwrap();
    /// ```
    pub fn release_default(&mut self, job_id: u64) -> BeanstalkcResult<()> {
        self.release(job_id, DEFAULT_JOB_PRIORITY, DEFAULT_JOB_DELAY)
    }

    /// Release a reserved job back into the ready queue.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use std::time::Duration;
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// conn.release(1, 0, Duration::from_secs(10)).unwrap();
    /// ```
    pub fn release(&mut self, job_id: u64, priority: u32, delay: Duration) -> BeanstalkcResult<()> {
        self.send(command::release(job_id, priority, delay))
            .map(|_| ())
    }

    /// Bury a specific job with default priority.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// conn.bury_default(1).unwrap();
    /// ```
    pub fn bury_default(&mut self, job_id: u64) -> BeanstalkcResult<()> {
        self.bury(job_id, DEFAULT_JOB_PRIORITY)
    }

    /// Bury a specific job.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// conn.bury(1, 0).unwrap();
    /// ```
    pub fn bury(&mut self, job_id: u64, priority: u32) -> BeanstalkcResult<()> {
        self.send(command::bury(job_id, priority)).map(|_| ())
    }

    /// Touch a job by `job_id`. Allowing the worker to request more time on a reserved
    /// job before it expires.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// conn.touch(1).unwrap();
    /// ```
    pub fn touch(&mut self, job_id: u64) -> BeanstalkcResult<()> {
        self.send(command::touch(job_id)).map(|_| ())
    }

    /// Return a dict of statistical information about a job.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use beanstalkc::Beanstalkc;
    ///
    /// let mut conn = Beanstalkc::new().connect().unwrap();
    ///
    /// let stats = conn.stats_job(1).unwrap();
    /// dbg!(stats);
    /// ```
    pub fn stats_job(&mut self, job_id: u64) -> BeanstalkcResult<HashMap<String, String>> {
        self.send(command::stats_job(job_id))
            .map(|r| r.body_as_map())
    }

    fn send(&mut self, cmd: command::Command) -> BeanstalkcResult<Response> {
        if self.stream.is_none() {
            return Err(BeanstalkcError::ConnectionError(
                "invalid connection".to_string(),
            ));
        }

        let mut request = Request::new(self.stream.as_mut().unwrap());
        let resp = request.send(cmd.build().as_bytes())?;

        if cmd.expected_ok_status.contains(&resp.status) {
            Ok(resp)
        } else if cmd.expected_error_status.contains(&resp.status) {
            Err(BeanstalkcError::CommandFailed(format!("{:?}", resp.status)))
        } else {
            Err(BeanstalkcError::UnexpectedResponse(format!(
                "{:?}",
                resp.status
            )))
        }
    }
}

impl Drop for Beanstalkc {
    fn drop(&mut self) {
        self.close();
    }
}

impl Default for Beanstalkc {
    fn default() -> Self {
        Beanstalkc::new()
    }
}