Autodelete

Deleting from a storage after handling

If you want to delete a task from storage after handling, you can use RedisJsonBackend or RedisBackend with autodelete=false parameter. It's safe to use it only with one consumer. If you have more than one consumer, you can use distributed lock by redis. It's also named as redlock. See Distributed Locks with Redis.

Don't forget to delete a task explicitly from storage after handling. See Committer::commit.

It's experimental implementation. In the future, it will be implemented more comfortable way.

Recommendation

I recommend to use autodelete=True, if it fits to you. This way is simple to understanding and it do not require extra configurations. But you need to know that your tasks will not be handling again if your application has an error.

Example

extern crate redis;
use serde::{Deserialize, Serialize};

use taskline::prelude::*;

#[derive(Deserialize, Serialize, Debug, Clone)]
struct Data {
    id: u64,
    name: String,
}

#[tokio::main]
async fn main() {
    let backend = JsonRedisBackend::<Data>::new(RedisBackend::new(
        redis::Client::open("redis://127.0.0.1/").unwrap(),
        String::from("taskline"),
        10,
        false,
    ));
    let producer = Producer::new(backend.clone());
    let consumer = Consumer::new(backend.clone());
    let committer = Committer::new(backend.clone());

    producer
        .schedule(
            &Data {
                id: 1,
                name: "Task".to_string(),
            },
            &(now() + 1000.),
        )
        .await
        .unwrap();

    poll_tasks(100, consumer, |tasks| async {
        for task in tasks.unwrap() {
            let task = task.unwrap();
            println!("Consumed {:?}", task);
            committer.commit(&task).await.unwrap();
        }
        true
    })
    .await;
}