Update examples to use new api

This commit is contained in:
asonix 2024-01-07 22:48:38 -06:00
parent c00f739cdc
commit 6cec89361c
5 changed files with 22 additions and 16 deletions

View file

@ -1,12 +1,10 @@
use actix_rt::Arbiter; use actix_rt::Arbiter;
use anyhow::Error; use anyhow::Error;
use background_jobs::{ use background_jobs::{
// memory_storage::{ActixTimer, Storage}, memory_storage::{ActixTimer, Storage},
ActixJob as Job, ActixSpawner, MaxRetries, UnsendJob as Job, WorkerConfig,
MaxRetries,
WorkerConfig,
}; };
use background_jobs_sled_storage::Storage; // use background_jobs_sled_storage::Storage;
use std::{ use std::{
future::{ready, Ready}, future::{ready, Ready},
time::{Duration, SystemTime}, time::{Duration, SystemTime},
@ -36,9 +34,8 @@ async fn main() -> Result<(), Error> {
.init(); .init();
// Set up our Storage // Set up our Storage
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(ActixTimer);
// let storage = Storage::new(ActixTimer);
let arbiter = Arbiter::new(); let arbiter = Arbiter::new();
@ -53,9 +50,11 @@ async fn main() -> Result<(), Error> {
queue_handle.queue(MyJob::new(1, 2)).await?; queue_handle.queue(MyJob::new(1, 2)).await?;
queue_handle.queue(MyJob::new(3, 4)).await?; queue_handle.queue(MyJob::new(3, 4)).await?;
queue_handle.queue(MyJob::new(5, 6)).await?; queue_handle.queue(MyJob::new(5, 6)).await?;
queue_handle for i in 0..20 {
.schedule(MyJob::new(7, 8), SystemTime::now() + Duration::from_secs(2)) queue_handle
.await?; .schedule(MyJob::new(7, 8), SystemTime::now() + Duration::from_secs(i))
.await?;
}
// Block on Actix // Block on Actix
actix_rt::signal::ctrl_c().await?; actix_rt::signal::ctrl_c().await?;
@ -86,6 +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;
// 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

View file

@ -1,6 +1,6 @@
use actix_rt::Arbiter; use actix_rt::Arbiter;
use anyhow::Error; use anyhow::Error;
use background_jobs::{ActixJob as Job, MaxRetries, WorkerConfig}; use background_jobs::{ActixSpawner, MaxRetries, UnsendJob as Job, WorkerConfig};
use background_jobs_sled_storage::Storage; use background_jobs_sled_storage::Storage;
use std::{ use std::{
future::{ready, Future, Ready}, future::{ready, Future, Ready},
@ -88,6 +88,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;
// 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
@ -117,6 +118,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;
const NAME: &'static str = "LongJob"; const NAME: &'static str = "LongJob";

View file

@ -1,6 +1,6 @@
use actix_rt::Arbiter; use actix_rt::Arbiter;
use anyhow::Error; use anyhow::Error;
use background_jobs::{ActixJob as Job, MaxRetries, WorkerConfig}; use background_jobs::{ActixSpawner, MaxRetries, UnsendJob as Job, WorkerConfig};
use background_jobs_sled_storage::Storage; use background_jobs_sled_storage::Storage;
use std::{ use std::{
future::{ready, Ready}, future::{ready, Ready},
@ -100,6 +100,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;
// 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
@ -129,6 +130,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;
const NAME: &'static str = "StopJob"; const NAME: &'static str = "StopJob";
const QUEUE: &'static str = DEFAULT_QUEUE; const QUEUE: &'static str = DEFAULT_QUEUE;

View file

@ -1,9 +1,10 @@
use actix_rt::Arbiter; use actix_rt::Arbiter;
use anyhow::Error; use anyhow::Error;
use background_jobs::{ use background_jobs::{
// memory_storage::{ActixTimer, Storage}, ActixSpawner,
ActixJob as Job,
MaxRetries, MaxRetries,
// memory_storage::{ActixTimer, Storage},
UnsendJob as Job,
WorkerConfig, WorkerConfig,
}; };
use background_jobs_sled_storage::Storage; use background_jobs_sled_storage::Storage;
@ -91,6 +92,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;
// 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

View file

@ -1,6 +1,6 @@
use actix_rt::Arbiter; use actix_rt::Arbiter;
use anyhow::Error; use anyhow::Error;
use background_jobs::{ActixJob as Job, MaxRetries, WorkerConfig}; use background_jobs::{Job, MaxRetries, WorkerConfig};
use background_jobs_sled_storage::Storage; use background_jobs_sled_storage::Storage;
use std::{ use std::{
future::{ready, Ready}, future::{ready, Ready},