rename services to jobs

Avoids a potential name clash with request
handlers that actix-web calls services
This commit is contained in:
anna 2022-12-09 22:49:05 +01:00
parent 57026cb21d
commit ef911481a3
Signed by: fef
GPG key ID: EC22E476DC2D3D84
3 changed files with 12 additions and 15 deletions

View file

@ -8,14 +8,14 @@ use crate::core::*;
use crate::state::AppState; use crate::state::AppState;
#[async_trait] #[async_trait]
pub trait Service: Send + Sync + fmt::Debug { pub trait Job: Send + Sync + fmt::Debug {
async fn perform(&self) -> Result<()>; async fn perform(&self) -> Result<()>;
fn name(&self) -> &'static str; fn name(&self) -> &'static str;
} }
pub struct Scheduler { pub struct Scheduler {
arbiter: Arbiter, arbiter: Arbiter,
sender: Sender<Box<dyn Service>>, sender: Sender<Box<dyn Job>>,
} }
impl Scheduler { impl Scheduler {
@ -27,24 +27,21 @@ impl Scheduler {
}; };
if !sched.arbiter.spawn(worker(rx)) { if !sched.arbiter.spawn(worker(rx)) {
panic!("Service scheduler initialization failed"); panic!("Job scheduler initialization failed");
} }
sched sched
} }
pub async fn schedule(&self, service: Box<dyn Service>) { pub async fn schedule(&self, job: Box<dyn Job>) {
self.sender self.sender.send(job).await.expect("Job send failed");
.send(service)
.await
.expect("Service send failed");
} }
} }
async fn worker(mut receiver: Receiver<Box<dyn Service>>) { async fn worker(mut receiver: Receiver<Box<dyn Job>>) {
while let Some(service) = receiver.recv().await { while let Some(job) = receiver.recv().await {
debug!(target: "service", "Starting service {}", service.name()); debug!(target: "job", "Starting job {}", job.name());
if let Err(e) = service.perform().await { if let Err(e) = job.perform().await {
error!(target: "service", "Service {} failed: {}", service.name(), e); error!(target: "job", "Service {} failed: {}", job.name(), e);
} }
} }
} }

View file

@ -5,10 +5,10 @@ use sqlx::{migrate, PgPool};
mod conf; mod conf;
mod core; mod core;
mod data; mod data;
mod job;
mod model; mod model;
mod repo; mod repo;
mod route; mod route;
mod service;
mod state; mod state;
mod util; mod util;
use crate::core::*; use crate::core::*;

View file

@ -3,8 +3,8 @@ use sqlx::PgPool;
use std::sync::Arc; use std::sync::Arc;
use crate::conf::Config; use crate::conf::Config;
use crate::job::Scheduler;
use crate::repo::AppRepo; use crate::repo::AppRepo;
use crate::service::Scheduler;
pub struct State { pub struct State {
pub config: Config, pub config: Config,