mirror of
https://git.asonix.dog/asonix/background-jobs.git
synced 2024-11-24 21:11:03 +00:00
Add tokio example, update example imports
This commit is contained in:
parent
56291a91f8
commit
6665ced671
15 changed files with 154 additions and 43 deletions
|
@ -9,10 +9,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "2.0.0"
|
actix-rt = "2.0.0"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
background-jobs = { version = "0.17.0", path = "../..", features = [
|
background-jobs = { version = "0.17.0", path = "../..", features = [ "error-logging", "sled" ] }
|
||||||
"error-logging",
|
|
||||||
] }
|
|
||||||
background-jobs-sled-storage = { version = "0.10.0", path = "../../jobs-sled" }
|
|
||||||
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"] }
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use actix_rt::Arbiter;
|
use actix_rt::Arbiter;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use background_jobs::{
|
use background_jobs::{
|
||||||
|
actix::{Spawner, WorkerConfig},
|
||||||
memory_storage::{ActixTimer, Storage},
|
memory_storage::{ActixTimer, Storage},
|
||||||
ActixSpawner, MaxRetries, UnsendJob as Job, WorkerConfig,
|
MaxRetries, UnsendJob as Job,
|
||||||
};
|
};
|
||||||
// use background_jobs_sled_storage::Storage;
|
|
||||||
use std::{
|
use std::{
|
||||||
future::{ready, Ready},
|
future::{ready, Ready},
|
||||||
time::{Duration, SystemTime},
|
time::{Duration, SystemTime},
|
||||||
|
@ -85,7 +85,7 @@ impl MyJob {
|
||||||
impl Job for MyJob {
|
impl Job for MyJob {
|
||||||
type State = MyState;
|
type State = MyState;
|
||||||
type Future = Ready<Result<(), Error>>;
|
type Future = Ready<Result<(), Error>>;
|
||||||
type Spawner = ActixSpawner;
|
type Spawner = Spawner;
|
||||||
|
|
||||||
// The name of the job. It is super important that each job has a unique name,
|
// The name of the job. It is super important that each job has a unique name,
|
||||||
// because otherwise one job will overwrite another job when they're being
|
// because otherwise one job will overwrite another job when they're being
|
||||||
|
|
|
@ -9,10 +9,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "2.0.0"
|
actix-rt = "2.0.0"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
background-jobs = { version = "0.17.0", path = "../..", features = [
|
background-jobs = { version = "0.17.0", path = "../..", features = [ "error-logging", "sled" ] }
|
||||||
"error-logging",
|
|
||||||
] }
|
|
||||||
background-jobs-sled-storage = { version = "0.10.0", path = "../../jobs-sled" }
|
|
||||||
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"] }
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
use actix_rt::Arbiter;
|
use actix_rt::Arbiter;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use background_jobs::{ActixSpawner, MaxRetries, UnsendJob as Job, WorkerConfig};
|
use background_jobs::{
|
||||||
use background_jobs_sled_storage::Storage;
|
actix::{Spawner, WorkerConfig},
|
||||||
|
sled::Storage,
|
||||||
|
MaxRetries, UnsendJob as Job,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
future::{ready, Future, Ready},
|
future::{ready, Future, Ready},
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
|
@ -88,7 +91,7 @@ impl MyJob {
|
||||||
impl Job for MyJob {
|
impl Job for MyJob {
|
||||||
type State = MyState;
|
type State = MyState;
|
||||||
type Future = Ready<Result<(), Error>>;
|
type Future = Ready<Result<(), Error>>;
|
||||||
type Spawner = ActixSpawner;
|
type Spawner = Spawner;
|
||||||
|
|
||||||
// The name of the job. It is super important that each job has a unique name,
|
// The name of the job. It is super important that each job has a unique name,
|
||||||
// because otherwise one job will overwrite another job when they're being
|
// because otherwise one job will overwrite another job when they're being
|
||||||
|
@ -118,7 +121,7 @@ impl Job for MyJob {
|
||||||
impl Job for LongJob {
|
impl Job for LongJob {
|
||||||
type State = MyState;
|
type State = MyState;
|
||||||
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
|
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
|
||||||
type Spawner = ActixSpawner;
|
type Spawner = Spawner;
|
||||||
|
|
||||||
const NAME: &'static str = "LongJob";
|
const NAME: &'static str = "LongJob";
|
||||||
|
|
||||||
|
|
|
@ -9,10 +9,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "2.0.0"
|
actix-rt = "2.0.0"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
background-jobs = { version = "0.17.0", path = "../..", features = [
|
background-jobs = { version = "0.17.0", path = "../..", features = [ "error-logging", "sled"] }
|
||||||
"error-logging",
|
|
||||||
] }
|
|
||||||
background-jobs-sled-storage = { version = "0.10.0", path = "../../jobs-sled" }
|
|
||||||
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"] }
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
use actix_rt::Arbiter;
|
use actix_rt::Arbiter;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use background_jobs::{ActixSpawner, MaxRetries, UnsendJob as Job, WorkerConfig};
|
use background_jobs::{
|
||||||
use background_jobs_sled_storage::Storage;
|
actix::{Spawner, WorkerConfig},
|
||||||
|
sled::Storage,
|
||||||
|
MaxRetries, UnsendJob as Job,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
future::{ready, Ready},
|
future::{ready, Ready},
|
||||||
time::{Duration, SystemTime},
|
time::{Duration, SystemTime},
|
||||||
|
@ -100,7 +103,7 @@ impl MyJob {
|
||||||
impl Job for MyJob {
|
impl Job for MyJob {
|
||||||
type State = MyState;
|
type State = MyState;
|
||||||
type Future = Ready<Result<(), Error>>;
|
type Future = Ready<Result<(), Error>>;
|
||||||
type Spawner = ActixSpawner;
|
type Spawner = Spawner;
|
||||||
|
|
||||||
// The name of the job. It is super important that each job has a unique name,
|
// The name of the job. It is super important that each job has a unique name,
|
||||||
// because otherwise one job will overwrite another job when they're being
|
// because otherwise one job will overwrite another job when they're being
|
||||||
|
@ -130,7 +133,7 @@ impl Job for MyJob {
|
||||||
impl Job for StopJob {
|
impl Job for StopJob {
|
||||||
type State = MyState;
|
type State = MyState;
|
||||||
type Future = Ready<Result<(), Error>>;
|
type Future = Ready<Result<(), Error>>;
|
||||||
type Spawner = ActixSpawner;
|
type Spawner = Spawner;
|
||||||
|
|
||||||
const NAME: &'static str = "StopJob";
|
const NAME: &'static str = "StopJob";
|
||||||
const QUEUE: &'static str = DEFAULT_QUEUE;
|
const QUEUE: &'static str = DEFAULT_QUEUE;
|
||||||
|
|
|
@ -9,10 +9,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "2.0.0"
|
actix-rt = "2.0.0"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
background-jobs = { version = "0.17.0", path = "../..", features = [
|
background-jobs = { version = "0.17.0", path = "../..", features = [ "error-logging", "sled" ] }
|
||||||
"error-logging",
|
|
||||||
] }
|
|
||||||
background-jobs-sled-storage = { version = "0.10.0", path = "../../jobs-sled" }
|
|
||||||
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"] }
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
use actix_rt::Arbiter;
|
use actix_rt::Arbiter;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use background_jobs::{
|
use background_jobs::{
|
||||||
|
actix::{Spawner, WorkerConfig},
|
||||||
metrics::MetricsStorage,
|
metrics::MetricsStorage,
|
||||||
ActixSpawner,
|
sled::Storage,
|
||||||
MaxRetries,
|
MaxRetries, UnsendJob as Job,
|
||||||
// memory_storage::{ActixTimer, Storage},
|
|
||||||
UnsendJob as Job,
|
|
||||||
WorkerConfig,
|
|
||||||
};
|
};
|
||||||
use background_jobs_sled_storage::Storage;
|
|
||||||
use std::{future::Future, pin::Pin, time::Duration};
|
use std::{future::Future, pin::Pin, time::Duration};
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
@ -93,7 +90,7 @@ impl MyJob {
|
||||||
impl Job for MyJob {
|
impl Job for MyJob {
|
||||||
type State = MyState;
|
type State = MyState;
|
||||||
type Future = Pin<Box<dyn Future<Output = Result<(), Error>> + 'static>>;
|
type Future = Pin<Box<dyn Future<Output = Result<(), Error>> + 'static>>;
|
||||||
type Spawner = ActixSpawner;
|
type Spawner = Spawner;
|
||||||
|
|
||||||
// The name of the job. It is super important that each job has a unique name,
|
// The name of the job. It is super important that each job has a unique name,
|
||||||
// because otherwise one job will overwrite another job when they're being
|
// because otherwise one job will overwrite another job when they're being
|
||||||
|
|
|
@ -9,10 +9,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "2.0.0"
|
actix-rt = "2.0.0"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
background-jobs = { version = "0.17.0", path = "../..", features = [
|
background-jobs = { version = "0.17.0", path = "../..", features = [ "error-logging", "sled" ] }
|
||||||
"error-logging",
|
|
||||||
] }
|
|
||||||
background-jobs-sled-storage = { version = "0.10.0", path = "../../jobs-sled" }
|
|
||||||
time = "0.3"
|
time = "0.3"
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use actix_rt::Arbiter;
|
use actix_rt::Arbiter;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use background_jobs::{Job, MaxRetries, WorkerConfig};
|
use background_jobs::{actix::WorkerConfig, sled::Storage, Job, MaxRetries};
|
||||||
use background_jobs_sled_storage::Storage;
|
|
||||||
use std::{
|
use std::{
|
||||||
future::{ready, Ready},
|
future::{ready, Ready},
|
||||||
time::{Duration, SystemTime},
|
time::{Duration, SystemTime},
|
||||||
|
|
|
@ -8,7 +8,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "2.9.0"
|
actix-rt = "2.9.0"
|
||||||
anyhow = "1.0.79"
|
anyhow = "1.0.79"
|
||||||
background-jobs = { version = "0.17.0", features = ["background-jobs-postgres"], path = "../.." }
|
background-jobs = { version = "0.17.0", features = ["postgres"], path = "../.." }
|
||||||
serde = { version = "1.0.195", features = ["derive"] }
|
serde = { version = "1.0.195", features = ["derive"] }
|
||||||
tokio = { version = "1.35.1", features = ["full"] }
|
tokio = { version = "1.35.1", features = ["full"] }
|
||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
use actix_rt::Arbiter;
|
use actix_rt::Arbiter;
|
||||||
use background_jobs::{
|
use background_jobs::{
|
||||||
postgres::Storage, ActixSpawner, MaxRetries, UnsendJob as Job, WorkerConfig,
|
actix::{Spawner, WorkerConfig},
|
||||||
|
postgres::Storage,
|
||||||
|
MaxRetries, UnsendJob as Job,
|
||||||
};
|
};
|
||||||
// use background_jobs_sled_storage::Storage;
|
// use background_jobs_sled_storage::Storage;
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -89,7 +91,7 @@ impl MyJob {
|
||||||
impl Job for MyJob {
|
impl Job for MyJob {
|
||||||
type State = MyState;
|
type State = MyState;
|
||||||
type Future = Ready<anyhow::Result<()>>;
|
type Future = Ready<anyhow::Result<()>>;
|
||||||
type Spawner = ActixSpawner;
|
type Spawner = Spawner;
|
||||||
|
|
||||||
// The name of the job. It is super important that each job has a unique name,
|
// The name of the job. It is super important that each job has a unique name,
|
||||||
// because otherwise one job will overwrite another job when they're being
|
// because otherwise one job will overwrite another job when they're being
|
||||||
|
|
1
examples/tokio-example/.gitignore
vendored
Normal file
1
examples/tokio-example/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/my-sled-db
|
16
examples/tokio-example/Cargo.toml
Normal file
16
examples/tokio-example/Cargo.toml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
[package]
|
||||||
|
name = "tokio-example"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["asonix <asonix@asonix.dog>"]
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "1.0"
|
||||||
|
background-jobs = { version = "0.17.0", path = "../..", default-features = false, features = [ "error-logging", "sled", "tokio"] }
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
tracing = "0.1"
|
||||||
|
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
sled = "0.34"
|
105
examples/tokio-example/src/main.rs
Normal file
105
examples/tokio-example/src/main.rs
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
use anyhow::Error;
|
||||||
|
use background_jobs::{
|
||||||
|
memory_storage::{Storage, TokioTimer},
|
||||||
|
tokio::WorkerConfig,
|
||||||
|
Job, MaxRetries,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
future::{ready, Ready},
|
||||||
|
time::{Duration, SystemTime},
|
||||||
|
};
|
||||||
|
use tracing::info;
|
||||||
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
|
const DEFAULT_QUEUE: &str = "default";
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct MyState {
|
||||||
|
pub app_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||||
|
pub struct MyJob {
|
||||||
|
some_usize: usize,
|
||||||
|
other_usize: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Error> {
|
||||||
|
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
|
||||||
|
|
||||||
|
tracing_subscriber::fmt::fmt()
|
||||||
|
.with_env_filter(env_filter)
|
||||||
|
.init();
|
||||||
|
|
||||||
|
// Set up our Storage
|
||||||
|
// let db = sled::Config::new().temporary(true).open()?;
|
||||||
|
let storage = Storage::new(TokioTimer);
|
||||||
|
|
||||||
|
// Configure and start our workers
|
||||||
|
let queue_handle = WorkerConfig::new(storage, |_| MyState::new("My App"))
|
||||||
|
.register::<MyJob>()
|
||||||
|
.set_worker_count(DEFAULT_QUEUE, 16)
|
||||||
|
.start();
|
||||||
|
|
||||||
|
// Queue our jobs
|
||||||
|
queue_handle.queue(MyJob::new(1, 2)).await?;
|
||||||
|
queue_handle.queue(MyJob::new(3, 4)).await?;
|
||||||
|
queue_handle.queue(MyJob::new(5, 6)).await?;
|
||||||
|
for i in 0..20 {
|
||||||
|
queue_handle
|
||||||
|
.schedule(MyJob::new(7, 8), SystemTime::now() + Duration::from_secs(i))
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Block on Tokio
|
||||||
|
tokio::signal::ctrl_c().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MyState {
|
||||||
|
pub fn new(app_name: &str) -> Self {
|
||||||
|
MyState {
|
||||||
|
app_name: app_name.to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MyJob {
|
||||||
|
pub fn new(some_usize: usize, other_usize: usize) -> Self {
|
||||||
|
MyJob {
|
||||||
|
some_usize,
|
||||||
|
other_usize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Job for MyJob {
|
||||||
|
type State = MyState;
|
||||||
|
type Future = Ready<Result<(), Error>>;
|
||||||
|
|
||||||
|
// The name of the job. It is super important that each job has a unique name,
|
||||||
|
// because otherwise one job will overwrite another job when they're being
|
||||||
|
// registered.
|
||||||
|
const NAME: &'static str = "MyJob";
|
||||||
|
|
||||||
|
// The queue that this processor belongs to
|
||||||
|
//
|
||||||
|
// Workers have the option to subscribe to specific queues, so this is important to
|
||||||
|
// determine which worker will call the processor
|
||||||
|
//
|
||||||
|
// Jobs can optionally override the queue they're spawned on
|
||||||
|
const QUEUE: &'static str = DEFAULT_QUEUE;
|
||||||
|
|
||||||
|
// The number of times background-jobs should try to retry a job before giving up
|
||||||
|
//
|
||||||
|
// Jobs can optionally override this value
|
||||||
|
const MAX_RETRIES: MaxRetries = MaxRetries::Count(1);
|
||||||
|
|
||||||
|
fn run(self, state: MyState) -> Self::Future {
|
||||||
|
info!("{}: args, {:?}", state.app_name, self);
|
||||||
|
|
||||||
|
ready(Ok(()))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue