backie/fang_examples/asynk/simple_async_worker/src/lib.rs

75 lines
1.7 KiB
Rust
Raw Normal View History

use fang::async_trait;
2023-03-04 18:07:17 +00:00
use fang::queue::AsyncQueueable;
use fang::serde::{Deserialize, Serialize};
use fang::typetag;
2023-03-04 18:07:17 +00:00
use fang::runnable::AsyncRunnable;
2022-09-18 13:05:26 +00:00
use fang::FangError;
use std::time::Duration;
#[derive(Serialize, Deserialize)]
#[serde(crate = "fang::serde")]
pub struct MyTask {
pub number: u16,
}
impl MyTask {
pub fn new(number: u16) -> Self {
Self { number }
}
}
#[derive(Serialize, Deserialize)]
#[serde(crate = "fang::serde")]
pub struct MyFailingTask {
pub number: u16,
}
impl MyFailingTask {
pub fn new(number: u16) -> Self {
Self { number }
}
}
#[async_trait]
#[typetag::serde]
impl AsyncRunnable for MyTask {
2022-09-18 13:05:26 +00:00
async fn run(&self, queue: &mut dyn AsyncQueueable) -> Result<(), FangError> {
2023-03-04 18:07:17 +00:00
// let new_task = MyTask::new(self.number + 1);
// queue
// .insert_task(&new_task as &dyn AsyncRunnable)
// .await
// .unwrap();
log::info!("the current number is {}", self.number);
tokio::time::sleep(Duration::from_secs(3)).await;
Ok(())
}
}
#[async_trait]
#[typetag::serde]
impl AsyncRunnable for MyFailingTask {
2022-09-18 13:05:26 +00:00
async fn run(&self, queue: &mut dyn AsyncQueueable) -> Result<(), FangError> {
2023-03-04 18:07:17 +00:00
// let new_task = MyFailingTask::new(self.number + 1);
// queue
// .insert_task(&new_task as &dyn AsyncRunnable)
// .await
// .unwrap();
log::info!("the current number is {}", self.number);
tokio::time::sleep(Duration::from_secs(3)).await;
2023-03-04 18:07:17 +00:00
log::info!("done..");
//
// let b = true;
//
// if b {
// panic!("Hello!");
// } else {
// Ok(())
// }
Ok(())
}
}