From e2d4bbe2769e41bfc82359df41092d354f1b9d94 Mon Sep 17 00:00:00 2001 From: Ahmed Farghal Date: Tue, 25 Jul 2023 20:28:32 +0100 Subject: [PATCH] Remove *Store traits It was an unnecessary level of indirection, look are all those removed `Arc` Test Plan: Tests --- cronback-services/src/api/auth.rs | 11 ++- cronback-services/src/api/auth_store.rs | 43 ++-------- cronback-services/src/api/mod.rs | 6 +- .../src/dispatcher/attempt_store.rs | 61 +++++--------- .../src/dispatcher/dispatch_manager.rs | 27 +++--- cronback-services/src/dispatcher/handler.rs | 6 +- cronback-services/src/dispatcher/mod.rs | 14 ++-- cronback-services/src/dispatcher/run_store.rs | 58 +++---------- .../src/dispatcher/webhook_action.rs | 7 +- cronback-services/src/metadata/handler.rs | 9 +- .../src/metadata/metadata_store.rs | 43 ++-------- cronback-services/src/metadata/mod.rs | 7 +- cronback-services/src/scheduler/mod.rs | 8 +- .../src/scheduler/spinner/controller.rs | 4 +- .../src/scheduler/trigger_store.rs | 84 ++++--------------- 15 files changed, 107 insertions(+), 281 deletions(-) diff --git a/cronback-services/src/api/auth.rs b/cronback-services/src/api/auth.rs index fbe597c..483ab1e 100644 --- a/cronback-services/src/api/auth.rs +++ b/cronback-services/src/api/auth.rs @@ -45,11 +45,11 @@ impl From for ApiError { } pub struct Authenticator { - store: Box, + store: AuthStore, } impl Authenticator { - pub fn new(store: Box) -> Self { + pub fn new(store: AuthStore) -> Self { Self { store } } @@ -235,7 +235,6 @@ mod tests { use cronback_api_model::admin::CreateAPIkeyRequest; use super::*; - use crate::api::auth_store::SqlAuthStore; use crate::api::migrate_up; #[test] @@ -265,15 +264,15 @@ mod tests { } #[tokio::test] - async fn test_sql_auth_store() -> anyhow::Result<()> { + async fn test_auth_store() -> anyhow::Result<()> { let db = Database::in_memory().await?; migrate_up(&db).await?; - let store = SqlAuthStore::new(db); + let store = AuthStore::new(db); let prj1 = ProjectId::generate(); let prj2 = ProjectId::generate(); - let authenticator = Authenticator::new(Box::new(store)); + let authenticator = Authenticator::new(store); let key1 = authenticator .gen_key(build_create_key_request("key1"), &prj1) diff --git a/cronback-services/src/api/auth_store.rs b/cronback-services/src/api/auth_store.rs index 3b9cbf5..42a0587 100644 --- a/cronback-services/src/api/auth_store.rs +++ b/cronback-services/src/api/auth_store.rs @@ -1,4 +1,3 @@ -use async_trait::async_trait; use lib::prelude::*; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; @@ -6,47 +5,23 @@ use crate::api::db_model::{api_keys, ApiKey, ApiKeys}; pub type AuthStoreError = DatabaseError; -#[async_trait] -pub trait AuthStore { - async fn save_key(&self, key: ApiKey) -> Result<(), AuthStoreError>; - - async fn get_key( - &self, - key: &str, - ) -> Result, AuthStoreError>; - - /// Returns true if the key got deleted, false if the key didn't exist - async fn delete_key( - &self, - key_id: &str, - project: &ValidShardedId, - ) -> Result; - - async fn list_keys( - &self, - project: &ValidShardedId, - ) -> Result, AuthStoreError>; -} - -pub struct SqlAuthStore { +#[derive(Clone)] +pub struct AuthStore { db: Database, } -impl SqlAuthStore { +impl AuthStore { pub fn new(db: Database) -> Self { Self { db } } -} -#[async_trait] -impl AuthStore for SqlAuthStore { - async fn save_key(&self, key: ApiKey) -> Result<(), AuthStoreError> { + pub async fn save_key(&self, key: ApiKey) -> Result<(), AuthStoreError> { let active_model: api_keys::ActiveModel = key.into(); ApiKeys::insert(active_model).exec(&self.db.orm).await?; Ok(()) } - async fn get_key( + pub async fn get_key( &self, key_id: &str, ) -> Result, AuthStoreError> { @@ -54,7 +29,7 @@ impl AuthStore for SqlAuthStore { Ok(res) } - async fn delete_key( + pub async fn delete_key( &self, key_id: &str, project: &ValidShardedId, @@ -67,7 +42,7 @@ impl AuthStore for SqlAuthStore { Ok(res.rows_affected > 0) } - async fn list_keys( + pub async fn list_keys( &self, project: &ValidShardedId, ) -> Result, AuthStoreError> { @@ -104,10 +79,10 @@ mod tests { } #[tokio::test] - async fn test_sql_auth_store() -> anyhow::Result<()> { + async fn test_auth_store() -> anyhow::Result<()> { let db = Database::in_memory().await?; migrate_up(&db).await?; - let store = SqlAuthStore::new(db); + let store = AuthStore::new(db); let owner1 = ProjectId::generate(); let owner2 = ProjectId::generate(); diff --git a/cronback-services/src/api/mod.rs b/cronback-services/src/api/mod.rs index eb7b0dd..250ae94 100644 --- a/cronback-services/src/api/mod.rs +++ b/cronback-services/src/api/mod.rs @@ -15,7 +15,7 @@ use std::sync::Arc; use std::time::Instant; use auth::Authenticator; -use auth_store::SqlAuthStore; +use auth_store::AuthStore; use axum::extract::MatchedPath; use axum::http::{Request, StatusCode}; use axum::middleware::{self, Next}; @@ -87,9 +87,7 @@ pub async fn start_api_server( let shared_state = Arc::new(AppState { _context: context.clone(), config: config.clone(), - authenicator: Authenticator::new(Box::new(SqlAuthStore::new( - db.clone(), - ))), + authenicator: Authenticator::new(AuthStore::new(db)), scheduler_clients: Box::new(GrpcClientProvider::new(context.clone())), dispatcher_clients: Box::new(GrpcClientProvider::new(context.clone())), metadata_svc_clients: Box::new(GrpcClientProvider::new( diff --git a/cronback-services/src/dispatcher/attempt_store.rs b/cronback-services/src/dispatcher/attempt_store.rs index e367378..037edd1 100644 --- a/cronback-services/src/dispatcher/attempt_store.rs +++ b/cronback-services/src/dispatcher/attempt_store.rs @@ -1,60 +1,43 @@ -use async_trait::async_trait; use lib::prelude::*; +#[cfg(test)] use proto::common::PaginationIn; -use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter}; - -use super::db_model::{attempts, Attempt, Attempts}; - -pub type AttemptLogStoreError = DatabaseError; - -#[async_trait] -pub trait AttemptLogStore { - async fn log_attempt( - &self, - attempt: Attempt, - ) -> Result<(), AttemptLogStoreError>; +use sea_orm::ActiveModelTrait; +#[cfg(test)] +use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; - async fn get_attempts_for_run( - &self, - project: &ValidShardedId, - id: &RunId, - pagination: PaginationIn, - ) -> Result, AttemptLogStoreError>; +#[cfg(test)] +use super::db_model::Attempts; +use super::db_model::{attempts, Attempt}; - async fn get_attempt( - &self, - project: &ValidShardedId, - id: &AttemptId, - ) -> Result, AttemptLogStoreError>; -} +pub type AttemptStoreError = DatabaseError; -pub struct SqlAttemptLogStore { +#[derive(Clone)] +pub struct AttemptStore { db: Database, } -impl SqlAttemptLogStore { +impl AttemptStore { pub fn new(db: Database) -> Self { Self { db } } -} -#[async_trait] -impl AttemptLogStore for SqlAttemptLogStore { - async fn log_attempt( + pub async fn log_attempt( &self, attempt: Attempt, - ) -> Result<(), AttemptLogStoreError> { + ) -> Result<(), AttemptStoreError> { let active_model: attempts::ActiveModel = attempt.into(); active_model.insert(&self.db.orm).await?; Ok(()) } - async fn get_attempts_for_run( + // Leaving this under cfg(test) until someone actually need it. + #[cfg(test)] + pub async fn get_attempts_for_run( &self, project: &ValidShardedId, id: &RunId, pagination: PaginationIn, - ) -> Result, AttemptLogStoreError> { + ) -> Result, AttemptStoreError> { let query = Attempts::find() .filter(attempts::Column::RunId.eq(id.value())) .filter(attempts::Column::ProjectId.eq(project.value())) @@ -65,11 +48,13 @@ impl AttemptLogStore for SqlAttemptLogStore { Ok(PaginatedResponse::paginate(res, &pagination)) } - async fn get_attempt( + // Leaving this under cfg(test) until someone actually need it. + #[cfg(test)] + pub async fn get_attempt( &self, project_id: &ValidShardedId, id: &AttemptId, - ) -> Result, AttemptLogStoreError> { + ) -> Result, AttemptStoreError> { let res = Attempts::find_by_id((id.clone(), project_id.clone())) .one(&self.db.orm) .await?; @@ -118,10 +103,10 @@ mod tests { } #[tokio::test] - async fn test_sql_trigger_store() -> anyhow::Result<()> { + async fn test_trigger_store() -> anyhow::Result<()> { let db = Database::in_memory().await?; migrate_up(&db).await?; - let store = SqlAttemptLogStore::new(db); + let store = AttemptStore::new(db); let project = ProjectId::generate(); let project2 = ProjectId::generate(); diff --git a/cronback-services/src/dispatcher/dispatch_manager.rs b/cronback-services/src/dispatcher/dispatch_manager.rs index cfe269e..44d44e4 100644 --- a/cronback-services/src/dispatcher/dispatch_manager.rs +++ b/cronback-services/src/dispatcher/dispatch_manager.rs @@ -1,5 +1,4 @@ use std::debug_assert; -use std::sync::Arc; use std::time::Duration; use chrono::Utc; @@ -10,7 +9,7 @@ use proto::dispatcher_svc; use thiserror::Error; use tracing::{error, info}; -use super::attempt_store::AttemptLogStore; +use super::attempt_store::AttemptStore; use super::db_model::runs::RunStatus; use super::db_model::Run; use super::run_store::{RunStore, RunStoreError}; @@ -24,15 +23,15 @@ pub enum DispatcherManagerError { pub struct DispatchManager { _cell_id: u32, - attempt_store: Arc, - run_store: Arc, + attempt_store: AttemptStore, + run_store: RunStore, } impl DispatchManager { pub fn new( cell_id: u32, - run_store: Arc, - attempt_store: Arc, + run_store: RunStore, + attempt_store: AttemptStore, ) -> Self { Self { _cell_id: cell_id, @@ -57,8 +56,8 @@ impl DispatchManager { tokio::spawn( RunJob::from( r, - Arc::clone(&self.run_store), - Arc::clone(&self.attempt_store), + self.run_store.clone(), + self.attempt_store.clone(), ) .run(), ); @@ -76,8 +75,8 @@ impl DispatchManager { let run_job = RunJob::from( run, - Arc::clone(&self.run_store), - Arc::clone(&self.attempt_store), + self.run_store.clone(), + self.attempt_store.clone(), ); Ok(match mode { @@ -97,15 +96,15 @@ impl DispatchManager { pub struct RunJob { pub run: Run, - run_store: Arc, - attempt_store: Arc, + run_store: RunStore, + attempt_store: AttemptStore, } impl RunJob { fn from( run: Run, - run_store: Arc, - attempt_store: Arc, + run_store: RunStore, + attempt_store: AttemptStore, ) -> Self { Self { run, diff --git a/cronback-services/src/dispatcher/handler.rs b/cronback-services/src/dispatcher/handler.rs index a125a82..18f449d 100644 --- a/cronback-services/src/dispatcher/handler.rs +++ b/cronback-services/src/dispatcher/handler.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use chrono::Utc; use futures::TryFutureExt; use lib::prelude::*; @@ -27,14 +25,14 @@ pub(crate) struct DispatcherSvcHandler { #[allow(unused)] context: ServiceContext, dispatch_manager: DispatchManager, - run_store: Arc, + run_store: RunStore, } impl DispatcherSvcHandler { pub fn new( context: ServiceContext, dispatch_manager: DispatchManager, - run_store: Arc, + run_store: RunStore, ) -> Self { Self { context, diff --git a/cronback-services/src/dispatcher/mod.rs b/cronback-services/src/dispatcher/mod.rs index 9ac3a60..c4e9a17 100644 --- a/cronback-services/src/dispatcher/mod.rs +++ b/cronback-services/src/dispatcher/mod.rs @@ -7,14 +7,12 @@ mod retry; mod run_store; mod webhook_action; -use std::sync::Arc; - -use attempt_store::{AttemptLogStore, SqlAttemptLogStore}; +use attempt_store::AttemptStore; use dispatch_manager::DispatchManager; use lib::prelude::*; use lib::{netutils, service}; use proto::dispatcher_svc::dispatcher_svc_server::DispatcherSvcServer; -use run_store::{RunStore, SqlRunStore}; +use run_store::RunStore; use sea_orm::TransactionTrait; use sea_orm_migration::MigratorTrait; use tracing::info; @@ -41,16 +39,14 @@ pub async fn start_dispatcher_server( let db = Database::connect(&config.dispatcher.database_uri).await?; migrate_up(&db).await?; - let attempt_store: Arc = - Arc::new(SqlAttemptLogStore::new(db.clone())); + let attempt_store = AttemptStore::new(db.clone()); - let run_store: Arc = - Arc::new(SqlRunStore::new(db)); + let run_store = RunStore::new(db); let dispatch_manager = DispatchManager::new( config.dispatcher.cell_id, run_store.clone(), - attempt_store.clone(), + attempt_store, ); dispatch_manager.start().await?; diff --git a/cronback-services/src/dispatcher/run_store.rs b/cronback-services/src/dispatcher/run_store.rs index 01bfd05..6c2cb97 100644 --- a/cronback-services/src/dispatcher/run_store.rs +++ b/cronback-services/src/dispatcher/run_store.rs @@ -1,4 +1,3 @@ -use async_trait::async_trait; use lib::prelude::*; use proto::common::PaginationIn; use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter}; @@ -8,56 +7,23 @@ use super::db_model::{runs, Attempts, Run, Runs}; pub type RunStoreError = DatabaseError; -#[async_trait] -pub trait RunStore { - async fn store_run(&self, run: Run) -> Result<(), RunStoreError>; - - async fn update_run(&self, run: Run) -> Result<(), RunStoreError>; - - async fn get_run( - &self, - project: &ValidShardedId, - id: &RunId, - ) -> Result, RunStoreError>; - - async fn get_runs_by_trigger( - &self, - project: &ValidShardedId, - trigger_id: &TriggerId, - pagination: PaginationIn, - ) -> Result, RunStoreError>; - - async fn get_runs_by_project( - &self, - project: &ValidShardedId, - pagination: PaginationIn, - ) -> Result, RunStoreError>; - - async fn get_runs_by_status( - &self, - status: RunStatus, - ) -> Result, RunStoreError>; -} - -pub struct SqlRunStore { +#[derive(Clone)] +pub struct RunStore { db: Database, } -impl SqlRunStore { +impl RunStore { pub fn new(db: Database) -> Self { Self { db } } -} -#[async_trait] -impl RunStore for SqlRunStore { - async fn store_run(&self, run: Run) -> Result<(), RunStoreError> { + pub async fn store_run(&self, run: Run) -> Result<(), RunStoreError> { let active_model: runs::ActiveModel = run.into(); active_model.insert(&self.db.orm).await?; Ok(()) } - async fn update_run(&self, run: Run) -> Result<(), RunStoreError> { + pub async fn update_run(&self, run: Run) -> Result<(), RunStoreError> { let project = run.project_id.clone(); let active_model: runs::ActiveModel = run.into(); // Mark all the fields as dirty @@ -69,7 +35,7 @@ impl RunStore for SqlRunStore { Ok(()) } - async fn get_run( + pub async fn get_run( &self, project: &ValidShardedId, id: &RunId, @@ -85,7 +51,7 @@ impl RunStore for SqlRunStore { Ok(res) } - async fn get_runs_by_trigger( + pub async fn get_runs_by_trigger( &self, project: &ValidShardedId, trigger_id: &TriggerId, @@ -109,7 +75,9 @@ impl RunStore for SqlRunStore { Ok(PaginatedResponse::paginate(res, &pagination)) } - async fn get_runs_by_project( + // Leaving this under cfg(test) until someone actually need it. + #[cfg(test)] + pub async fn get_runs_by_project( &self, project: &ValidShardedId, pagination: PaginationIn, @@ -132,7 +100,7 @@ impl RunStore for SqlRunStore { Ok(PaginatedResponse::paginate(res, &pagination)) } - async fn get_runs_by_status( + pub async fn get_runs_by_status( &self, status: RunStatus, ) -> Result, RunStoreError> { @@ -189,10 +157,10 @@ mod tests { } #[tokio::test] - async fn test_sql_run_store() -> anyhow::Result<()> { + async fn test_run_store() -> anyhow::Result<()> { let db = Database::in_memory().await?; migrate_up(&db).await?; - let store = SqlRunStore::new(db); + let store = RunStore::new(db); let project1 = ProjectId::generate(); let project2 = ProjectId::generate(); diff --git a/cronback-services/src/dispatcher/webhook_action.rs b/cronback-services/src/dispatcher/webhook_action.rs index a66a3f3..cd7701d 100644 --- a/cronback-services/src/dispatcher/webhook_action.rs +++ b/cronback-services/src/dispatcher/webhook_action.rs @@ -1,4 +1,3 @@ -use std::sync::Arc; use std::time::Instant; use chrono::Utc; @@ -10,7 +9,7 @@ use reqwest::Method; use tracing::{debug, error, info, warn}; use validator::Validate; -use super::attempt_store::AttemptLogStore; +use super::attempt_store::AttemptStore; use super::db_model::attempts::{ AttemptDetails, AttemptStatus, @@ -34,8 +33,8 @@ fn to_reqwest_http_method(method: &HttpMethod) -> reqwest::Method { pub struct WebhookActionJob { pub run: Run, - pub run_store: Arc, - pub attempt_store: Arc, + pub run_store: RunStore, + pub attempt_store: AttemptStore, } impl WebhookActionJob { diff --git a/cronback-services/src/metadata/handler.rs b/cronback-services/src/metadata/handler.rs index c90c60a..d40da6c 100644 --- a/cronback-services/src/metadata/handler.rs +++ b/cronback-services/src/metadata/handler.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use chrono::Utc; use lib::prelude::*; use lib::service::ServiceContext; @@ -23,14 +21,11 @@ use super::metadata_store::MetadataStore; pub(crate) struct MetadataSvcHandler { #[allow(unused)] context: ServiceContext, - project_store: Arc, + project_store: MetadataStore, } impl MetadataSvcHandler { - pub fn new( - context: ServiceContext, - project_store: Arc, - ) -> Self { + pub fn new(context: ServiceContext, project_store: MetadataStore) -> Self { Self { context, project_store, diff --git a/cronback-services/src/metadata/metadata_store.rs b/cronback-services/src/metadata/metadata_store.rs index b797b55..d4d9a94 100644 --- a/cronback-services/src/metadata/metadata_store.rs +++ b/cronback-services/src/metadata/metadata_store.rs @@ -1,4 +1,3 @@ -use async_trait::async_trait; use chrono::Utc; use lib::prelude::*; use sea_orm::{ActiveModelTrait, EntityTrait, Set}; @@ -7,43 +6,17 @@ use super::db_model::{projects, Project, ProjectStatus, Projects}; pub type MetadataStoreError = DatabaseError; -#[async_trait] -pub trait MetadataStore { - async fn store_project( - &self, - project: Project, - ) -> Result<(), MetadataStoreError>; - - async fn set_status( - &self, - id: &ValidShardedId, - status: ProjectStatus, - ) -> Result<(), MetadataStoreError>; - - async fn get_status( - &self, - id: &ValidShardedId, - ) -> Result, MetadataStoreError>; - - async fn exists( - &self, - id: &ValidShardedId, - ) -> Result; -} - -pub struct SqlMetadataStore { +#[derive(Clone)] +pub struct MetadataStore { db: Database, } -impl SqlMetadataStore { +impl MetadataStore { pub fn new(db: Database) -> Self { Self { db } } -} -#[async_trait] -impl MetadataStore for SqlMetadataStore { - async fn store_project( + pub async fn store_project( &self, project: Project, ) -> Result<(), MetadataStoreError> { @@ -52,7 +25,7 @@ impl MetadataStore for SqlMetadataStore { Ok(()) } - async fn set_status( + pub async fn set_status( &self, id: &ValidShardedId, status: ProjectStatus, @@ -68,7 +41,7 @@ impl MetadataStore for SqlMetadataStore { Ok(()) } - async fn get_status( + pub async fn get_status( &self, id: &ValidShardedId, ) -> Result, MetadataStoreError> { @@ -78,7 +51,7 @@ impl MetadataStore for SqlMetadataStore { .map(|p| p.status)) } - async fn exists( + pub async fn exists( &self, id: &ValidShardedId, ) -> Result { @@ -107,7 +80,7 @@ mod tests { async fn test_sql_project_store() -> anyhow::Result<()> { let db = Database::in_memory().await?; migrate_up(&db).await?; - let store = SqlMetadataStore::new(db); + let store = MetadataStore::new(db); let project1 = build_project(ProjectStatus::Enabled); let project2 = build_project(ProjectStatus::QuotaExceeded); diff --git a/cronback-services/src/metadata/mod.rs b/cronback-services/src/metadata/mod.rs index 3a252d6..5746679 100644 --- a/cronback-services/src/metadata/mod.rs +++ b/cronback-services/src/metadata/mod.rs @@ -3,11 +3,9 @@ mod handler; mod metadata_store; mod migration; -use std::sync::Arc; - use lib::prelude::*; use lib::{netutils, service}; -use metadata_store::{MetadataStore, SqlMetadataStore}; +use metadata_store::MetadataStore; use proto::metadata_svc::metadata_svc_server::MetadataSvcServer; use sea_orm::TransactionTrait; use sea_orm_migration::MigratorTrait; @@ -33,8 +31,7 @@ pub async fn start_metadata_server( let db = Database::connect(&config.metadata.database_uri).await?; migrate_up(&db).await?; - let store: Arc = - Arc::new(SqlMetadataStore::new(db)); + let store = MetadataStore::new(db); let handler = handler::MetadataSvcHandler::new(context.clone(), store); let svc = MetadataSvcServer::new(handler); diff --git a/cronback-services/src/scheduler/mod.rs b/cronback-services/src/scheduler/mod.rs index 9a878ad..98dc0fe 100644 --- a/cronback-services/src/scheduler/mod.rs +++ b/cronback-services/src/scheduler/mod.rs @@ -15,7 +15,7 @@ use proto::scheduler_svc::scheduler_svc_server::SchedulerSvcServer; use sea_orm::TransactionTrait; use sea_orm_migration::MigratorTrait; use spinner::controller::SpinnerController; -use trigger_store::SqlTriggerStore; +use trigger_store::TriggerStore; // TODO: Move database migration into a new service trait. pub async fn migrate_up(db: &Database) -> Result<(), DatabaseError> { @@ -34,13 +34,13 @@ pub async fn start_scheduler_server( let db = Database::connect(&config.scheduler.database_uri).await?; migrate_up(&db).await?; - let trigger_store = Arc::new(SqlTriggerStore::new(db)); + let trigger_store = TriggerStore::new(db); let dispatcher_clients = Arc::new(GrpcClientProvider::new(context.clone())); let controller = Arc::new(SpinnerController::new( context.clone(), - trigger_store.clone(), + trigger_store, dispatcher_clients, )); @@ -103,7 +103,7 @@ pub mod test_helpers { let db = Database::in_memory().await.unwrap(); migrate_up(&db).await.unwrap(); - let trigger_store = Arc::new(SqlTriggerStore::new(db)); + let trigger_store = TriggerStore::new(db); let controller = Arc::new(SpinnerController::new( context.clone(), trigger_store, diff --git a/cronback-services/src/scheduler/spinner/controller.rs b/cronback-services/src/scheduler/spinner/controller.rs index 8e02e36..f12a870 100644 --- a/cronback-services/src/scheduler/spinner/controller.rs +++ b/cronback-services/src/scheduler/spinner/controller.rs @@ -54,7 +54,7 @@ pub(crate) struct SpinnerController { context: ServiceContext, triggers: Arc>, spinner: Mutex>, - store: Arc, + store: TriggerStore, name_cache: Arc>, dispatcher_clients: Arc>, } @@ -62,7 +62,7 @@ pub(crate) struct SpinnerController { impl SpinnerController { pub fn new( context: ServiceContext, - store: Arc, + store: TriggerStore, dispatcher_clients: Arc>, ) -> Self { let name_cacher_fetcher = { diff --git a/cronback-services/src/scheduler/trigger_store.rs b/cronback-services/src/scheduler/trigger_store.rs index 7cfbcd0..0a03bc1 100644 --- a/cronback-services/src/scheduler/trigger_store.rs +++ b/cronback-services/src/scheduler/trigger_store.rs @@ -1,4 +1,3 @@ -use async_trait::async_trait; use lib::prelude::*; use proto::common::PaginationIn; use sea_orm::{ @@ -14,72 +13,17 @@ use super::db_model::{Trigger, Triggers}; pub type TriggerStoreError = DatabaseError; -#[async_trait] -pub trait TriggerStore { - async fn install_trigger( - &self, - trigger: Trigger, - ) -> Result<(), TriggerStoreError>; - - async fn update_trigger( - &self, - trigger: Trigger, - ) -> Result<(), TriggerStoreError>; - - async fn delete_trigger( - &self, - project: &ValidShardedId, - name: &TriggerId, - ) -> Result<(), TriggerStoreError>; - - async fn get_all_active_triggers( - &self, - ) -> Result, TriggerStoreError>; - - async fn get_trigger_by_name( - &self, - project: &ProjectId, - name: &str, - ) -> Result, TriggerStoreError>; - - async fn find_trigger_id_for_name( - &self, - project: &ProjectId, - name: &str, - ) -> Result, TriggerStoreError>; - - async fn get_status( - &self, - project: &ProjectId, - name: &str, - ) -> Result, TriggerStoreError>; - - async fn get_triggers_by_project( - &self, - project: &ProjectId, - pagination: PaginationIn, - statuses: Option>, - ) -> Result, TriggerStoreError>; - - async fn delete_triggers_by_project( - &self, - project: &ProjectId, - ) -> Result<(), TriggerStoreError>; -} - -pub struct SqlTriggerStore { +#[derive(Clone)] +pub struct TriggerStore { db: Database, } -impl SqlTriggerStore { +impl TriggerStore { pub fn new(db: Database) -> Self { Self { db } } -} -#[async_trait] -impl TriggerStore for SqlTriggerStore { - async fn install_trigger( + pub async fn install_trigger( &self, trigger: Trigger, ) -> Result<(), TriggerStoreError> { @@ -88,7 +32,7 @@ impl TriggerStore for SqlTriggerStore { Ok(()) } - async fn update_trigger( + pub async fn update_trigger( &self, trigger: Trigger, ) -> Result<(), TriggerStoreError> { @@ -103,7 +47,7 @@ impl TriggerStore for SqlTriggerStore { Ok(()) } - async fn delete_trigger( + pub async fn delete_trigger( &self, project: &ValidShardedId, trigger_id: &TriggerId, @@ -114,7 +58,7 @@ impl TriggerStore for SqlTriggerStore { Ok(()) } - async fn delete_triggers_by_project( + pub async fn delete_triggers_by_project( &self, project: &ProjectId, ) -> Result<(), TriggerStoreError> { @@ -125,7 +69,7 @@ impl TriggerStore for SqlTriggerStore { Ok(()) } - async fn get_all_active_triggers( + pub async fn get_all_active_triggers( &self, ) -> Result, TriggerStoreError> { let res = Triggers::find() @@ -138,7 +82,7 @@ impl TriggerStore for SqlTriggerStore { Ok(res) } - async fn get_triggers_by_project( + pub async fn get_triggers_by_project( &self, project: &ProjectId, pagination: PaginationIn, @@ -157,7 +101,7 @@ impl TriggerStore for SqlTriggerStore { Ok(PaginatedResponse::paginate(res, &pagination)) } - async fn get_trigger_by_name( + pub async fn get_trigger_by_name( &self, project_id: &ProjectId, name: &str, @@ -170,7 +114,7 @@ impl TriggerStore for SqlTriggerStore { Ok(res) } - async fn find_trigger_id_for_name( + pub async fn find_trigger_id_for_name( &self, project: &ProjectId, name: &str, @@ -186,7 +130,7 @@ impl TriggerStore for SqlTriggerStore { Ok(res) } - async fn get_status( + pub async fn get_status( &self, project: &ProjectId, name: &str, @@ -244,11 +188,11 @@ mod tests { } #[tokio::test] - async fn test_sql_trigger_store() -> anyhow::Result<()> { + async fn test_trigger_store() -> anyhow::Result<()> { let db = Database::in_memory().await?; migrate_up(&db).await?; - let store = SqlTriggerStore::new(db); + let store = TriggerStore::new(db); let project1 = ProjectId::generate(); let project2 = ProjectId::generate();