Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/bury.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extern crate beanstalkd;

use beanstalkd::Beanstalkd;

fn main() {
let mut beanstalkd = Beanstalkd::localhost().unwrap();
let (id, body) = beanstalkd.reserve().unwrap();
println!("{}", body);
let _ = beanstalkd.bury(id, 1024).unwrap();
}
10 changes: 10 additions & 0 deletions examples/release.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extern crate beanstalkd;

use beanstalkd::Beanstalkd;

fn main() {
let mut beanstalkd = Beanstalkd::localhost().unwrap();
let (id, body) = beanstalkd.reserve().unwrap();
println!("{}", body);
let _ = beanstalkd.release(id, 1024, 10).unwrap();
}
13 changes: 13 additions & 0 deletions examples/reserve_with_timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extern crate beanstalkd;

use beanstalkd::Beanstalkd;

fn main() {
let mut beanstalkd = Beanstalkd::localhost().unwrap();

match beanstalkd.reserve_with_timeout(2) {
Ok(Some((id, body))) => println!("id: {} body: {}", id, body),
Ok(None) => println!("timedout"),
Err(err) => println!("{}", err),
}
}
10 changes: 10 additions & 0 deletions examples/touch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extern crate beanstalkd;

use beanstalkd::Beanstalkd;

fn main() {
let mut beanstalkd = Beanstalkd::localhost().unwrap();
let (id, body) = beanstalkd.reserve().unwrap();
println!("{}", body);
let _ = beanstalkd.touch(id).unwrap();
}
30 changes: 29 additions & 1 deletion src/beanstalkd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use commands;
use error::{BeanstalkdError, BeanstalkdResult};
use parse;
use request::Request;
use response::Response;
use response::{Response, Status};

macro_rules! try {
($e:expr) => (match $e { Ok(e) => e, Err(_) => return Err(BeanstalkdError::ConnectionError) })
Expand Down Expand Up @@ -53,11 +53,39 @@ impl Beanstalkd {
self.cmd(commands::reserve()).map(|r| (parse::id(r.clone()), parse::body(r)))
}

/// Get the next message out of the queue with timeout. If the timeout runs out a None is returned
/// in BeanstalkdResult.
pub fn reserve_with_timeout(&mut self, timeout: u64) -> BeanstalkdResult<Option<(u64, String)>> {
self.cmd(commands::reserve_with_timeout(timeout))
.map(|r| {
if r.status == Status::TIMED_OUT {
None
} else {
Some((parse::id(r.clone()), parse::body(r)))
}
})
}

/// Deletes a message out of the queue
pub fn delete(&mut self, id: u64) -> BeanstalkdResult<()> {
self.cmd(commands::delete(id)).map(|_| ())
}

/// Release a job in the queue
pub fn release(&mut self, id: u64, priority: u32, delay: u32) -> BeanstalkdResult<()> {
self.cmd(commands::release(id, priority, delay)).map(|_| ())
}

/// Bury a job in the queue
pub fn bury(&mut self, id: u64, priority: u32) -> BeanstalkdResult<()> {
self.cmd(commands::bury(id, priority)).map(|_| ())
}

/// Touch a job in the queue
pub fn touch(&mut self, id: u64) -> BeanstalkdResult<()> {
self.cmd(commands::touch(id)).map(|_| ())
}

/// Returns all available stats
pub fn stats(&mut self) -> BeanstalkdResult<HashMap<String, String>> {
self.cmd(commands::stats()).map(parse::hashmap)
Expand Down
36 changes: 36 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@ pub fn reserve() -> String {
build("reserve", vec![], "")
}

pub fn reserve_with_timeout(timeout: u64) -> String {
build("reserve-with-timeout", vec![timeout.to_string()], "")
}

pub fn delete(id: u64) -> String {
build("delete", vec![id.to_string()], "")
}

pub fn release(id: u64, priority: u32, delay: u32) -> String {
build("release", vec![id.to_string(), priority.to_string(), delay.to_string()], "")
}

pub fn bury(id: u64, priority: u32) -> String {
build("bury", vec![id.to_string(), priority.to_string()], "")
}

pub fn touch(id: u64) -> String {
build("touch", vec![id.to_string()], "")
}

pub fn stats() -> String {
build("stats", vec![], "")
}
Expand Down Expand Up @@ -62,11 +78,31 @@ fn reserve_test() {
assert_eq!(reserve(), "reserve\r\n".to_string());
}

#[test]
fn reserve_with_timeout_test() {
assert_eq!(reserve_with_timeout(10), "reserve-with-timeout 10\r\n".to_string())
}

#[test]
fn delete_test() {
assert_eq!(delete(1), "delete 1\r\n".to_string());
}

#[test]
fn release_test() {
assert_eq!(release(1, 1024, 10), "release 1 1024 10\r\n".to_string());
}

#[test]
fn bury_test() {
assert_eq!(bury(1, 1024), "bury 1 1024\r\n".to_string());
}

#[test]
fn touch_test() {
assert_eq!(touch(1), "touch 1\r\n".to_string());
}

#[test]
fn stats_test() {
assert_eq!(stats(), "stats\r\n".to_string());
Expand Down
4 changes: 4 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ impl<'a> Request<'a> {
"DELETED" => Status::DELETED,
"WATCHING" => Status::WATCHING,
"NOT_IGNORED" => Status::NOT_IGNORED,
"TIMED_OUT" => Status::TIMED_OUT,
"RELEASED" => Status::RELEASED,
"BURIED" => Status::BURIED,
"TOUCHED" => Status::TOUCHED,
_ => return Err(BeanstalkdError::RequestError),
};
let mut data = line.clone();
Expand Down
4 changes: 4 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub enum Status {
DELETED,
WATCHING,
NOT_IGNORED,
TIMED_OUT,
RELEASED,
BURIED,
TOUCHED,
}

#[derive(Clone)]
Expand Down