Quick Start
Taskline is provided as the a library on crates.io. To get started, add taskline as a dependency to your project.
cargo add taskline
Example
The library provides an asynchronous code for interacting with Redis. You can use tokio
or async-std
as the runtime.
First of all, you need to create a RedisBackend
instance. You can do this by using the RedisBackend::new
method or RedisBackendConfig
struct.
After that, you need to create a consumer and a producer. These are simple structs for more comfortable interaction with the library. You can create them using the Consumer::new
and Producer::new
methods.
You can look at the example below.
extern crate redis;
use tokio::time::{sleep, Duration};
use taskline::prelude::*;
#[tokio::main]
async fn main() {
let queue = RedisBackendConfig {
queue_key: "taskline",
read_batch_size: 10,
autodelete: true,
}
.with_client(redis::Client::open("redis://127.0.0.1/").unwrap());
if !queue.is_redis_version_ok().await.unwrap() {
return;
}
queue
.write(&"Hello!".to_string(), &(now() + 1000.))
.await
.unwrap();
loop {
let tasks = queue.read(&now()).await;
match tasks {
Ok(tasks) => {
if tasks.is_empty() {
sleep(Duration::from_millis(100)).await;
continue;
}
for task in tasks {
tokio::task::spawn(async move {
println!("Consumed '{}'", task);
});
}
}
Err(e) => {
sleep(Duration::from_millis(1000)).await;
println!("Error: {:?}", e);
continue;
}
}
}
}
More examples can be found here.
Run the example
git clone git@github.com:daxartio/taskline.git
# git clone https://github.com/daxartio/taskline.git
cd taskline
docker-compose up -d
cargo run --example redis