feat: refactor tasks
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
use std::fmt::Debug;
|
||||
use std::{fmt::Debug, sync::Arc};
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
use typed_builder::TypedBuilder;
|
||||
|
||||
use crate::app::AppContextTrait;
|
||||
|
||||
#[derive(TypedBuilder)]
|
||||
#[builder(field_defaults(default, setter(strip_option)))]
|
||||
pub struct UnitTestAppContext {
|
||||
pub struct TestingAppContext {
|
||||
logger: Option<crate::logger::LoggerService>,
|
||||
db: Option<crate::database::DatabaseService>,
|
||||
config: Option<crate::app::AppConfig>,
|
||||
@@ -16,7 +17,8 @@ pub struct UnitTestAppContext {
|
||||
graphql: Option<crate::graphql::GraphQLService>,
|
||||
storage: Option<crate::storage::StorageService>,
|
||||
crypto: Option<crate::crypto::CryptoService>,
|
||||
task: Option<crate::task::TaskService>,
|
||||
#[builder(default = Arc::new(OnceCell::new()), setter(!strip_option))]
|
||||
task: Arc<OnceCell<crate::task::TaskService>>,
|
||||
message: Option<crate::message::MessageService>,
|
||||
#[builder(default = Some(String::from(env!("CARGO_MANIFEST_DIR"))))]
|
||||
working_dir: Option<String>,
|
||||
@@ -24,13 +26,19 @@ pub struct UnitTestAppContext {
|
||||
environment: crate::app::Environment,
|
||||
}
|
||||
|
||||
impl Debug for UnitTestAppContext {
|
||||
impl TestingAppContext {
|
||||
pub fn set_task(&self, task: crate::task::TaskService) {
|
||||
self.task.get_or_init(|| task);
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for TestingAppContext {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "UnitTestAppContext")
|
||||
}
|
||||
}
|
||||
|
||||
impl AppContextTrait for UnitTestAppContext {
|
||||
impl AppContextTrait for TestingAppContext {
|
||||
fn logger(&self) -> &crate::logger::LoggerService {
|
||||
self.logger.as_ref().expect("should set logger")
|
||||
}
|
||||
@@ -76,7 +84,7 @@ impl AppContextTrait for UnitTestAppContext {
|
||||
}
|
||||
|
||||
fn task(&self) -> &crate::task::TaskService {
|
||||
self.task.as_ref().expect("should set tasks")
|
||||
self.task.get().expect("should set task")
|
||||
}
|
||||
|
||||
fn message(&self) -> &crate::message::MessageService {
|
||||
|
||||
@@ -3,8 +3,20 @@ use crate::{
|
||||
errors::RecorderResult,
|
||||
};
|
||||
|
||||
pub struct TestingDatabaseServiceConfig {
|
||||
pub auto_migrate: bool,
|
||||
}
|
||||
|
||||
impl Default for TestingDatabaseServiceConfig {
|
||||
fn default() -> Self {
|
||||
Self { auto_migrate: true }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "testcontainers")]
|
||||
pub async fn build_testing_database_service() -> RecorderResult<DatabaseService> {
|
||||
pub async fn build_testing_database_service(
|
||||
config: TestingDatabaseServiceConfig,
|
||||
) -> RecorderResult<DatabaseService> {
|
||||
use testcontainers::{ImageExt, runners::AsyncRunner};
|
||||
use testcontainers_ext::{ImageDefaultLogConsumerExt, ImagePruneExistedLabelExt};
|
||||
use testcontainers_modules::postgres::Postgres;
|
||||
@@ -34,7 +46,7 @@ pub async fn build_testing_database_service() -> RecorderResult<DatabaseService>
|
||||
connect_timeout: 5000,
|
||||
idle_timeout: 10000,
|
||||
acquire_timeout: None,
|
||||
auto_migrate: true,
|
||||
auto_migrate: config.auto_migrate,
|
||||
})
|
||||
.await?;
|
||||
db_service.container = Some(container);
|
||||
|
||||
@@ -3,4 +3,5 @@ pub mod crypto;
|
||||
pub mod database;
|
||||
pub mod mikan;
|
||||
pub mod storage;
|
||||
pub mod task;
|
||||
pub mod tracing;
|
||||
|
||||
15
apps/recorder/src/test_utils/task.rs
Normal file
15
apps/recorder/src/test_utils/task.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
errors::RecorderResult,
|
||||
task::{TaskConfig, TaskService},
|
||||
};
|
||||
|
||||
pub async fn build_testing_task_service(
|
||||
ctx: Arc<dyn AppContextTrait>,
|
||||
) -> RecorderResult<TaskService> {
|
||||
let config = TaskConfig {};
|
||||
let task_service = TaskService::from_config_and_ctx(config, ctx).await?;
|
||||
Ok(task_service)
|
||||
}
|
||||
Reference in New Issue
Block a user