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

View file

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

View file

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