mirror of
https://git.asonix.dog/asonix/background-jobs.git
synced 2024-11-21 19:40:59 +00:00
Remove server creation free functions
This commit is contained in:
parent
cff996d1f1
commit
db9af6a3b8
11 changed files with 178 additions and 66 deletions
|
@ -14,8 +14,9 @@ members = [
|
||||||
"jobs-actix",
|
"jobs-actix",
|
||||||
"jobs-core",
|
"jobs-core",
|
||||||
"jobs-sled",
|
"jobs-sled",
|
||||||
"examples/actix-example",
|
"examples/basic-example",
|
||||||
"examples/managed-example",
|
"examples/managed-example",
|
||||||
|
"examples/panic-example",
|
||||||
]
|
]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
|
|
@ -136,14 +136,11 @@ async fn main() -> Result<(), Error> {
|
||||||
use background_jobs::memory_storage::Storage;
|
use background_jobs::memory_storage::Storage;
|
||||||
let storage = Storage::new();
|
let storage = Storage::new();
|
||||||
|
|
||||||
// Start the application server. This guards access to to the jobs store
|
|
||||||
let queue_handle = create_server(storage);
|
|
||||||
|
|
||||||
// Configure and start our workers
|
// Configure and start our workers
|
||||||
WorkerConfig::new(move || MyState::new("My App"))
|
let queue_handle = WorkerConfig::new(move || MyState::new("My App"))
|
||||||
.register::<MyJob>()
|
.register::<MyJob>()
|
||||||
.set_worker_count(DEFAULT_QUEUE, 16)
|
.set_worker_count(DEFAULT_QUEUE, 16)
|
||||||
.start(queue_handle.clone());
|
.start(storage);
|
||||||
|
|
||||||
// Queue our jobs
|
// Queue our jobs
|
||||||
queue_handle.queue(MyJob::new(1, 2))?;
|
queue_handle.queue(MyJob::new(1, 2))?;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "actix-example"
|
name = "basic-example"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["asonix <asonix@asonix.dog>"]
|
authors = ["asonix <asonix@asonix.dog>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
105
examples/basic-example/src/main.rs
Normal file
105
examples/basic-example/src/main.rs
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
use actix_rt::Arbiter;
|
||||||
|
use anyhow::Error;
|
||||||
|
use background_jobs::{ActixJob as Job, MaxRetries, WorkerConfig};
|
||||||
|
use background_jobs_sled_storage::Storage;
|
||||||
|
use chrono::{Duration, Utc};
|
||||||
|
use std::future::{ready, Ready};
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::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(db)?;
|
||||||
|
|
||||||
|
let arbiter = Arbiter::new();
|
||||||
|
|
||||||
|
// Configure and start our workers
|
||||||
|
let queue_handle = WorkerConfig::new(move || MyState::new("My App"))
|
||||||
|
.register::<MyJob>()
|
||||||
|
.set_worker_count(DEFAULT_QUEUE, 16)
|
||||||
|
.start_in_arbiter(&arbiter.handle(), storage);
|
||||||
|
|
||||||
|
// 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?;
|
||||||
|
queue_handle
|
||||||
|
.schedule(MyJob::new(7, 8), Utc::now() + Duration::seconds(2))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// Block on Actix
|
||||||
|
actix_rt::signal::ctrl_c().await?;
|
||||||
|
|
||||||
|
arbiter.stop();
|
||||||
|
let _ = arbiter.join();
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
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(()))
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
use actix_rt::Arbiter;
|
use actix_rt::Arbiter;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use background_jobs::{ActixJob as Job, Manager, MaxRetries, WorkerConfig};
|
use background_jobs::{ActixJob as Job, MaxRetries, WorkerConfig};
|
||||||
use background_jobs_sled_storage::Storage;
|
use background_jobs_sled_storage::Storage;
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
use std::future::{ready, Ready};
|
use std::future::{ready, Ready};
|
||||||
|
@ -36,20 +36,17 @@ async fn main() -> Result<(), Error> {
|
||||||
let storage = Storage::new(db)?;
|
let storage = Storage::new(db)?;
|
||||||
|
|
||||||
// Configure and start our workers
|
// Configure and start our workers
|
||||||
let worker_config = WorkerConfig::new(move || MyState::new("My App"))
|
let manager = WorkerConfig::new(move || MyState::new("My App"))
|
||||||
.register::<StopJob>()
|
.register::<StopJob>()
|
||||||
.register::<MyJob>()
|
.register::<MyJob>()
|
||||||
.set_worker_count(DEFAULT_QUEUE, 16);
|
.set_worker_count(DEFAULT_QUEUE, 16)
|
||||||
|
.managed(storage);
|
||||||
// Start the application server. This guards access to to the jobs store
|
|
||||||
let manager = Manager::new(storage, worker_config);
|
|
||||||
|
|
||||||
// Queue our jobs
|
// Queue our jobs
|
||||||
manager.queue_handle().queue(MyJob::new(1, 2)).await?;
|
manager.queue(MyJob::new(1, 2)).await?;
|
||||||
manager.queue_handle().queue(MyJob::new(3, 4)).await?;
|
manager.queue(MyJob::new(3, 4)).await?;
|
||||||
manager.queue_handle().queue(MyJob::new(5, 6)).await?;
|
manager.queue(MyJob::new(5, 6)).await?;
|
||||||
manager
|
manager
|
||||||
.queue_handle()
|
|
||||||
.schedule(MyJob::new(7, 8), Utc::now() + Duration::seconds(2))
|
.schedule(MyJob::new(7, 8), Utc::now() + Duration::seconds(2))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -57,17 +54,16 @@ async fn main() -> Result<(), Error> {
|
||||||
actix_rt::signal::ctrl_c().await?;
|
actix_rt::signal::ctrl_c().await?;
|
||||||
|
|
||||||
// kill the current arbiter
|
// kill the current arbiter
|
||||||
manager.queue_handle().queue(StopJob).await?;
|
manager.queue(StopJob).await?;
|
||||||
|
|
||||||
// Block on Actix
|
// Block on Actix
|
||||||
actix_rt::signal::ctrl_c().await?;
|
actix_rt::signal::ctrl_c().await?;
|
||||||
|
|
||||||
// See that the workers have respawned
|
// See that the workers have respawned
|
||||||
manager.queue_handle().queue(MyJob::new(1, 2)).await?;
|
manager.queue(MyJob::new(1, 2)).await?;
|
||||||
manager.queue_handle().queue(MyJob::new(3, 4)).await?;
|
manager.queue(MyJob::new(3, 4)).await?;
|
||||||
manager.queue_handle().queue(MyJob::new(5, 6)).await?;
|
manager.queue(MyJob::new(5, 6)).await?;
|
||||||
manager
|
manager
|
||||||
.queue_handle()
|
|
||||||
.schedule(MyJob::new(7, 8), Utc::now() + Duration::seconds(2))
|
.schedule(MyJob::new(7, 8), Utc::now() + Duration::seconds(2))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|
1
examples/panic-example/.gitignore
vendored
Normal file
1
examples/panic-example/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/my-sled-db
|
19
examples/panic-example/Cargo.toml
Normal file
19
examples/panic-example/Cargo.toml
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
[package]
|
||||||
|
name = "panic-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]
|
||||||
|
actix-rt = "2.0.0"
|
||||||
|
anyhow = "1.0"
|
||||||
|
async-trait = "0.1.24"
|
||||||
|
background-jobs = { version = "0.11.0", path = "../..", features = ["error-logging"] }
|
||||||
|
background-jobs-sled-storage = { version = "0.10.0", path = "../../jobs-sled" }
|
||||||
|
chrono = "0.4"
|
||||||
|
tracing = "0.1"
|
||||||
|
tracing-subscriber = { version = "0.2", features = ["fmt"] }
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
sled = "0.34"
|
|
@ -1,6 +1,6 @@
|
||||||
use actix_rt::Arbiter;
|
use actix_rt::Arbiter;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use background_jobs::{create_server_in_arbiter, ActixJob as Job, MaxRetries, WorkerConfig};
|
use background_jobs::{ActixJob as Job, MaxRetries, WorkerConfig};
|
||||||
use background_jobs_sled_storage::Storage;
|
use background_jobs_sled_storage::Storage;
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
use std::future::{ready, Ready};
|
use std::future::{ready, Ready};
|
||||||
|
@ -37,15 +37,12 @@ async fn main() -> Result<(), Error> {
|
||||||
|
|
||||||
let arbiter = Arbiter::new();
|
let arbiter = Arbiter::new();
|
||||||
|
|
||||||
// Start the application server. This guards access to to the jobs store
|
|
||||||
let queue_handle = create_server_in_arbiter(arbiter.handle(), storage);
|
|
||||||
|
|
||||||
// Configure and start our workers
|
// Configure and start our workers
|
||||||
WorkerConfig::new(move || MyState::new("My App"))
|
let queue_handle = WorkerConfig::new(move || 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_in_arbiter(&arbiter.handle(), queue_handle.clone());
|
.start_in_arbiter(&arbiter.handle(), storage);
|
||||||
|
|
||||||
// Queue some panicking job
|
// Queue some panicking job
|
||||||
for _ in 0..32 {
|
for _ in 0..32 {
|
|
@ -36,14 +36,11 @@
|
||||||
//! use background_jobs::memory_storage::Storage;
|
//! use background_jobs::memory_storage::Storage;
|
||||||
//! let storage = Storage::new();
|
//! let storage = Storage::new();
|
||||||
//!
|
//!
|
||||||
//! // Start the application server. This guards access to to the jobs store
|
|
||||||
//! let queue_handle = create_server(storage);
|
|
||||||
//!
|
|
||||||
//! // Configure and start our workers
|
//! // Configure and start our workers
|
||||||
//! WorkerConfig::new(move || MyState::new("My App"))
|
//! let queue_handle = WorkerConfig::new(move || MyState::new("My App"))
|
||||||
//! .register::<MyJob>()
|
//! .register::<MyJob>()
|
||||||
//! .set_worker_count(DEFAULT_QUEUE, 16)
|
//! .set_worker_count(DEFAULT_QUEUE, 16)
|
||||||
//! .start(queue_handle.clone());
|
//! .start(storage);
|
||||||
//!
|
//!
|
||||||
//! // Queue our jobs
|
//! // Queue our jobs
|
||||||
//! queue_handle.queue(MyJob::new(1, 2))?;
|
//! queue_handle.queue(MyJob::new(1, 2))?;
|
||||||
|
@ -151,7 +148,7 @@ impl Manager {
|
||||||
///
|
///
|
||||||
/// Manager works by startinng a new Arbiter to run jobs, and if that arbiter ever dies, it
|
/// Manager works by startinng a new Arbiter to run jobs, and if that arbiter ever dies, it
|
||||||
/// spins up another one and spawns the workers again
|
/// spins up another one and spawns the workers again
|
||||||
pub fn new<S, State>(storage: S, worker_config: WorkerConfig<State>) -> Self
|
fn new<S, State>(storage: S, worker_config: WorkerConfig<State>) -> Self
|
||||||
where
|
where
|
||||||
S: Storage + Sync + 'static,
|
S: Storage + Sync + 'static,
|
||||||
State: Clone,
|
State: Clone,
|
||||||
|
@ -188,7 +185,8 @@ impl Manager {
|
||||||
drop(drop_notifier);
|
drop(drop_notifier);
|
||||||
|
|
||||||
// Assume arbiter is dead if we were notified
|
// Assume arbiter is dead if we were notified
|
||||||
if arbiter.spawn(async {}) {
|
let online = arbiter.spawn(async {});
|
||||||
|
if online {
|
||||||
panic!("Arbiter should be dead by now");
|
panic!("Arbiter should be dead by now");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,6 +212,14 @@ impl Manager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Deref for Manager {
|
||||||
|
type Target = QueueHandle;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.queue_handle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Deref for ArbiterDropper {
|
impl Deref for ArbiterDropper {
|
||||||
type Target = Arbiter;
|
type Target = Arbiter;
|
||||||
|
|
||||||
|
@ -255,23 +261,7 @@ impl Drop for DropNotifier {
|
||||||
/// In previous versions of this library, the server itself was run on it's own dedicated threads
|
/// In previous versions of this library, the server itself was run on it's own dedicated threads
|
||||||
/// and guarded access to jobs via messages. Since we now have futures-aware synchronization
|
/// and guarded access to jobs via messages. Since we now have futures-aware synchronization
|
||||||
/// primitives, the Server has become an object that gets shared between client threads.
|
/// primitives, the Server has become an object that gets shared between client threads.
|
||||||
///
|
fn create_server_in_arbiter<S>(arbiter: ArbiterHandle, storage: S) -> QueueHandle
|
||||||
/// This method will panic if not called from an actix runtime
|
|
||||||
pub fn create_server<S>(storage: S) -> QueueHandle
|
|
||||||
where
|
|
||||||
S: Storage + Sync + 'static,
|
|
||||||
{
|
|
||||||
create_server_in_arbiter(Arbiter::current(), storage)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new Server
|
|
||||||
///
|
|
||||||
/// In previous versions of this library, the server itself was run on it's own dedicated threads
|
|
||||||
/// and guarded access to jobs via messages. Since we now have futures-aware synchronization
|
|
||||||
/// primitives, the Server has become an object that gets shared between client threads.
|
|
||||||
///
|
|
||||||
/// This method will panic if not called from an actix runtime
|
|
||||||
pub fn create_server_in_arbiter<S>(arbiter: ArbiterHandle, storage: S) -> QueueHandle
|
|
||||||
where
|
where
|
||||||
S: Storage + Sync + 'static,
|
S: Storage + Sync + 'static,
|
||||||
{
|
{
|
||||||
|
@ -285,7 +275,7 @@ where
|
||||||
/// In previous versions of this library, the server itself was run on it's own dedicated threads
|
/// In previous versions of this library, the server itself was run on it's own dedicated threads
|
||||||
/// and guarded access to jobs via messages. Since we now have futures-aware synchronization
|
/// and guarded access to jobs via messages. Since we now have futures-aware synchronization
|
||||||
/// primitives, the Server has become an object that gets shared between client threads.
|
/// primitives, the Server has become an object that gets shared between client threads.
|
||||||
pub fn create_server_managed<S>(storage: S) -> QueueHandle
|
fn create_server_managed<S>(storage: S) -> QueueHandle
|
||||||
where
|
where
|
||||||
S: Storage + Sync + 'static,
|
S: Storage + Sync + 'static,
|
||||||
{
|
{
|
||||||
|
@ -352,17 +342,28 @@ where
|
||||||
/// Start the workers in the current arbiter
|
/// Start the workers in the current arbiter
|
||||||
///
|
///
|
||||||
/// This method will panic if not called from an actix runtime
|
/// This method will panic if not called from an actix runtime
|
||||||
pub fn start(self, queue_handle: QueueHandle) {
|
pub fn start<S: Storage + Send + Sync + 'static>(self, storage: S) -> QueueHandle {
|
||||||
self.start_in_arbiter(&Arbiter::current(), queue_handle)
|
self.start_in_arbiter(&Arbiter::current(), storage)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start the workers in the provided arbiter
|
/// Start the workers in the provided arbiter
|
||||||
pub fn start_in_arbiter(self, arbiter: &ArbiterHandle, queue_handle: QueueHandle) {
|
pub fn start_in_arbiter<S: Storage + Send + Sync + 'static>(
|
||||||
self.start_managed(arbiter, queue_handle, &())
|
self,
|
||||||
|
arbiter: &ArbiterHandle,
|
||||||
|
storage: S,
|
||||||
|
) -> QueueHandle {
|
||||||
|
let queue_handle = create_server_in_arbiter(arbiter.clone(), storage);
|
||||||
|
self.start_managed(arbiter, queue_handle.clone(), &());
|
||||||
|
queue_handle
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start the workers on a managed arbiter, and return the manager struct
|
||||||
|
pub fn managed<S: Storage + Send + Sync + 'static>(self, storage: S) -> Manager {
|
||||||
|
Manager::new(storage, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start a workers in a managed way
|
/// Start a workers in a managed way
|
||||||
pub fn start_managed<Extras: Clone + Send + 'static>(
|
fn start_managed<Extras: Clone + Send + 'static>(
|
||||||
&self,
|
&self,
|
||||||
arbiter: &ArbiterHandle,
|
arbiter: &ArbiterHandle,
|
||||||
queue_handle: QueueHandle,
|
queue_handle: QueueHandle,
|
||||||
|
|
11
src/lib.rs
11
src/lib.rs
|
@ -132,14 +132,11 @@
|
||||||
//! // Set up our Storage
|
//! // Set up our Storage
|
||||||
//! let storage = Storage::new();
|
//! let storage = Storage::new();
|
||||||
//!
|
//!
|
||||||
//! // Start the application server. This guards access to to the jobs store
|
|
||||||
//! let queue_handle = ServerConfig::new(storage).start();
|
|
||||||
//!
|
|
||||||
//! // Configure and start our workers
|
//! // Configure and start our workers
|
||||||
//! WorkerConfig::new(move || MyState::new("My App"))
|
//! let queue_handle = WorkerConfig::new(move || MyState::new("My App"))
|
||||||
//! .register::<MyJob>()
|
//! .register::<MyJob>()
|
||||||
//! .set_processor_count(DEFAULT_QUEUE, 16)
|
//! .set_processor_count(DEFAULT_QUEUE, 16)
|
||||||
//! .start(queue_handle.clone());
|
//! .start(storage);
|
||||||
//!
|
//!
|
||||||
//! // Queue our jobs
|
//! // Queue our jobs
|
||||||
//! queue_handle.queue(MyJob::new(1, 2))?;
|
//! queue_handle.queue(MyJob::new(1, 2))?;
|
||||||
|
@ -172,6 +169,4 @@ pub mod dev {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "background-jobs-actix")]
|
#[cfg(feature = "background-jobs-actix")]
|
||||||
pub use background_jobs_actix::{
|
pub use background_jobs_actix::{ActixJob, Manager, QueueHandle, WorkerConfig};
|
||||||
create_server, create_server_in_arbiter, ActixJob, Manager, QueueHandle, WorkerConfig,
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in a new issue