Skip to content

A rust lib for running tasks asynchronously via queues.

License

Notifications You must be signed in to change notification settings

jackieit/queue-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

queue-rs

A simple queue library for rust which execute delay and sync jobs. Now ,it's only support redis ,may be support other queue later such as db,file and so on.

Usage

how to add a job to queue

use queue_rs::queue::Queue;
use serde::{Deserialize, Serialize};
use queue_rs::{QResult,MakeJob};
// define a job struct
#[derive(Serialize, Deserialize)]
pub struct TestJob {
    pub name: String,
    //add some other attributes
}
impl TestJob {
   fn new(name: String) -> Self {
      TestJob { name }
   }
}
// impl JobTrait
#[MakeJob]
impl JobTrait for TestJob {
    fn execute(&self) -> QResult<()> {
       println!("test job [{}] executed", self.name);
       Ok(())
    }
}
let queue = Queue::new("queue-test", redis::Client::open("redis://127.0.0.1/").unwrap());
let _job_id = queue.push(TestJob::new("first job".to_string()));
//!

how add a delay job to queue

let mut queue = Queue::new("queue-test", redis::Client::open("redis://127.0.0.1/").unwrap());
// will execute after 10 seconds
queue.delay(10)
let _job_id = queue.push(TestJob::new("first job".to_string()));
//!

how to listen the queue

let queue = Queue::new("queue-test", redis::Client::open("redis://127.0.0.1/").unwrap());
let task  = QueueTask::new(queue);
task.listen(0);

how to run all jobs in queue, this will exit after all jobs executed

let queue = Queue::new("queue-test", redis::Client::open("redis://127.0.0.1/").unwrap());
let task  = QueueTask::new(queue);
task.run(0);

tracing logs

add tracing-subscriber to cargo.toml

tracing-subscriber="0.3"

add tracing_subscriber::fmt::init();` to your main function, more info about tracing

About

A rust lib for running tasks asynchronously via queues.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages