Convert panic example to tokio

This commit is contained in:
asonix 2024-01-14 12:49:07 -05:00
parent eff0956740
commit 5a95a71b7c
2 changed files with 25 additions and 29 deletions

View file

@ -7,10 +7,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
actix-rt = "2.0.0"
anyhow = "1.0" anyhow = "1.0"
background-jobs = { version = "0.17.0", path = "../..", features = [ "error-logging", "sled" ] } background-jobs = { version = "0.17.0", path = "../..", default-features = false, features = [ "error-logging", "sled", "tokio" ] }
time = "0.3" time = "0.3"
tokio = { version = "1", features = ["full"] }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View file

@ -1,6 +1,5 @@
use actix_rt::Arbiter;
use anyhow::Error; use anyhow::Error;
use background_jobs::{actix::WorkerConfig, sled::Storage, Job, MaxRetries}; use background_jobs::{sled::Storage, tokio::WorkerConfig, Job, MaxRetries};
use std::{ use std::{
future::{ready, Ready}, future::{ready, Ready},
time::{Duration, SystemTime}, time::{Duration, SystemTime},
@ -17,14 +16,14 @@ pub struct MyState {
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct MyJob { pub struct MyJob {
some_usize: usize, some_u64: u64,
other_usize: usize, other_u64: u64,
} }
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct PanickingJob; pub struct PanickingJob;
#[actix_rt::main] #[tokio::main]
async fn main() -> Result<(), Error> { async fn main() -> Result<(), Error> {
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")); let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
@ -36,15 +35,12 @@ async fn main() -> Result<(), Error> {
let db = sled::Config::new().temporary(true).open()?; let db = sled::Config::new().temporary(true).open()?;
let storage = Storage::new(db)?; let storage = Storage::new(db)?;
let arbiter = Arbiter::new();
// Configure and start our workers // Configure and start our workers
let queue_handle = let queue_handle = WorkerConfig::new(storage, |_| MyState::new("My App"))
WorkerConfig::new_in_arbiter(arbiter.handle(), storage, |_| MyState::new("My App")) .register::<PanickingJob>()
.register::<PanickingJob>() .register::<MyJob>()
.register::<MyJob>() .set_worker_count(DEFAULT_QUEUE, 16)
.set_worker_count(DEFAULT_QUEUE, 16) .start()?;
.start();
// Queue some panicking job // Queue some panicking job
for _ in 0..32 { for _ in 0..32 {
@ -52,18 +48,18 @@ async fn main() -> Result<(), Error> {
} }
// Queue our jobs // Queue our jobs
queue_handle.queue(MyJob::new(1, 2)).await?; for i in 0..10 {
queue_handle.queue(MyJob::new(3, 4)).await?; queue_handle.queue(MyJob::new(i, i + 1)).await?;
queue_handle.queue(MyJob::new(5, 6)).await?; queue_handle
queue_handle .schedule(
.schedule(MyJob::new(7, 8), SystemTime::now() + Duration::from_secs(2)) MyJob::new(i + 10, i + 11),
.await?; SystemTime::now() + Duration::from_secs(i),
)
.await?;
}
// Block on Actix // Block on tokio
actix_rt::signal::ctrl_c().await?; tokio::signal::ctrl_c().await?;
arbiter.stop();
let _ = arbiter.join();
Ok(()) Ok(())
} }
@ -77,10 +73,10 @@ impl MyState {
} }
impl MyJob { impl MyJob {
pub fn new(some_usize: usize, other_usize: usize) -> Self { pub fn new(some_u64: u64, other_u64: u64) -> Self {
MyJob { MyJob {
some_usize, some_u64,
other_usize, other_u64,
} }
} }
} }