2021-10-29 20:20:04 +00:00
|
|
|
use actix_rt::Arbiter;
|
2022-07-02 19:03:26 +00:00
|
|
|
use background_jobs::{
|
2024-01-13 21:58:48 +00:00
|
|
|
actix::{Spawner, WorkerConfig},
|
2024-01-08 04:48:38 +00:00
|
|
|
memory_storage::{ActixTimer, Storage},
|
2024-02-05 05:35:47 +00:00
|
|
|
BoxError, MaxRetries, UnsendJob as Job,
|
2022-07-02 19:03:26 +00:00
|
|
|
};
|
2022-01-17 23:45:24 +00:00
|
|
|
use std::{
|
|
|
|
future::{ready, Ready},
|
|
|
|
time::{Duration, SystemTime},
|
|
|
|
};
|
2021-09-16 22:50:32 +00:00
|
|
|
use tracing::info;
|
|
|
|
use tracing_subscriber::EnvFilter;
|
2019-05-25 21:15:09 +00:00
|
|
|
|
2020-04-25 22:12:43 +00:00
|
|
|
const DEFAULT_QUEUE: &str = "default";
|
2019-05-25 21:15:09 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct MyState {
|
|
|
|
pub app_name: String,
|
|
|
|
}
|
|
|
|
|
2020-03-21 02:31:03 +00:00
|
|
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
2019-05-25 21:15:09 +00:00
|
|
|
pub struct MyJob {
|
|
|
|
some_usize: usize,
|
|
|
|
other_usize: usize,
|
|
|
|
}
|
|
|
|
|
2020-03-21 02:31:03 +00:00
|
|
|
#[actix_rt::main]
|
2024-02-05 05:35:47 +00:00
|
|
|
async fn main() -> Result<(), BoxError> {
|
2021-09-16 22:50:32 +00:00
|
|
|
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
|
|
|
|
|
|
|
|
tracing_subscriber::fmt::fmt()
|
|
|
|
.with_env_filter(env_filter)
|
|
|
|
.init();
|
|
|
|
|
2019-05-28 00:01:21 +00:00
|
|
|
// Set up our Storage
|
2024-01-08 04:48:38 +00:00
|
|
|
// let db = sled::Config::new().temporary(true).open()?;
|
|
|
|
let storage = Storage::new(ActixTimer);
|
2019-05-25 21:15:09 +00:00
|
|
|
|
2021-10-29 20:20:04 +00:00
|
|
|
let arbiter = Arbiter::new();
|
|
|
|
|
2019-05-28 00:01:21 +00:00
|
|
|
// Configure and start our workers
|
2021-10-30 00:19:04 +00:00
|
|
|
let queue_handle =
|
|
|
|
WorkerConfig::new_in_arbiter(arbiter.handle(), storage, |_| MyState::new("My App"))
|
|
|
|
.register::<MyJob>()
|
|
|
|
.set_worker_count(DEFAULT_QUEUE, 16)
|
|
|
|
.start();
|
2021-09-13 22:40:49 +00:00
|
|
|
|
2019-05-28 00:01:21 +00:00
|
|
|
// Queue our jobs
|
2021-10-07 01:31:27 +00:00
|
|
|
queue_handle.queue(MyJob::new(1, 2)).await?;
|
|
|
|
queue_handle.queue(MyJob::new(3, 4)).await?;
|
|
|
|
queue_handle.queue(MyJob::new(5, 6)).await?;
|
2024-01-08 04:48:38 +00:00
|
|
|
for i in 0..20 {
|
|
|
|
queue_handle
|
|
|
|
.schedule(MyJob::new(7, 8), SystemTime::now() + Duration::from_secs(i))
|
|
|
|
.await?;
|
|
|
|
}
|
2019-05-25 21:15:09 +00:00
|
|
|
|
2019-05-28 00:01:21 +00:00
|
|
|
// Block on Actix
|
2020-03-21 02:31:03 +00:00
|
|
|
actix_rt::signal::ctrl_c().await?;
|
2021-10-29 20:20:04 +00:00
|
|
|
|
|
|
|
arbiter.stop();
|
|
|
|
let _ = arbiter.join();
|
|
|
|
|
2019-05-25 21:15:09 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 00:01:21 +00:00
|
|
|
impl Job for MyJob {
|
|
|
|
type State = MyState;
|
2024-02-05 05:35:47 +00:00
|
|
|
type Error = BoxError;
|
|
|
|
type Future = Ready<Result<(), BoxError>>;
|
2024-01-13 21:58:48 +00:00
|
|
|
type Spawner = Spawner;
|
2019-05-28 00:01:21 +00:00
|
|
|
|
2020-04-21 00:30:56 +00:00
|
|
|
// 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
|
2019-05-25 21:15:09 +00:00
|
|
|
// registered.
|
2020-04-21 00:30:56 +00:00
|
|
|
const NAME: &'static str = "MyJob";
|
2019-05-25 21:15:09 +00:00
|
|
|
|
|
|
|
// 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);
|
2020-04-21 00:30:56 +00:00
|
|
|
|
|
|
|
fn run(self, state: MyState) -> Self::Future {
|
2021-09-16 22:50:32 +00:00
|
|
|
info!("{}: args, {:?}", state.app_name, self);
|
2020-04-21 00:30:56 +00:00
|
|
|
|
2021-02-04 18:40:39 +00:00
|
|
|
ready(Ok(()))
|
2020-04-21 00:30:56 +00:00
|
|
|
}
|
2019-05-25 21:15:09 +00:00
|
|
|
}
|