feat: more task system
This commit is contained in:
@@ -67,19 +67,9 @@ impl AppBuilder {
|
||||
}
|
||||
|
||||
pub async fn build(self) -> RecorderResult<App> {
|
||||
AppConfig::load_dotenv(
|
||||
&self.environment,
|
||||
&self.working_dir,
|
||||
self.dotenv_file.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
self.load_env().await?;
|
||||
|
||||
let config = AppConfig::load_config(
|
||||
&self.environment,
|
||||
&self.working_dir,
|
||||
self.config_file.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
let config = self.load_config().await?;
|
||||
|
||||
let app_context =
|
||||
AppContext::new(self.environment.clone(), config, self.working_dir.clone()).await?;
|
||||
@@ -90,6 +80,26 @@ impl AppBuilder {
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn load_env(&self) -> RecorderResult<()> {
|
||||
AppConfig::load_dotenv(
|
||||
&self.environment,
|
||||
&self.working_dir,
|
||||
self.dotenv_file.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn load_config(&self) -> RecorderResult<AppConfig> {
|
||||
let config = AppConfig::load_config(
|
||||
&self.environment,
|
||||
&self.working_dir,
|
||||
self.config_file.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub fn working_dir(self, working_dir: String) -> Self {
|
||||
let mut ret = self;
|
||||
ret.working_dir = working_dir;
|
||||
|
||||
@@ -20,3 +20,5 @@ complexity_limit = inf
|
||||
[crypto]
|
||||
|
||||
[task]
|
||||
|
||||
[message]
|
||||
|
||||
@@ -11,7 +11,8 @@ use super::env::Environment;
|
||||
use crate::{
|
||||
auth::AuthConfig, cache::CacheConfig, crypto::CryptoConfig, database::DatabaseConfig,
|
||||
errors::RecorderResult, extract::mikan::MikanConfig, graphql::GraphQLConfig,
|
||||
logger::LoggerConfig, storage::StorageConfig, tasks::TaskConfig, web::WebServerConfig,
|
||||
logger::LoggerConfig, message::MessageConfig, storage::StorageConfig, task::TaskConfig,
|
||||
web::WebServerConfig,
|
||||
};
|
||||
|
||||
const DEFAULT_CONFIG_MIXIN: &str = include_str!("./default_mixin.toml");
|
||||
@@ -28,7 +29,8 @@ pub struct AppConfig {
|
||||
pub graphql: GraphQLConfig,
|
||||
pub logger: LoggerConfig,
|
||||
pub database: DatabaseConfig,
|
||||
pub tasks: TaskConfig,
|
||||
pub task: TaskConfig,
|
||||
pub message: MessageConfig,
|
||||
}
|
||||
|
||||
impl AppConfig {
|
||||
|
||||
@@ -12,8 +12,9 @@ use crate::{
|
||||
extract::mikan::MikanClient,
|
||||
graphql::GraphQLService,
|
||||
logger::LoggerService,
|
||||
message::MessageService,
|
||||
storage::{StorageService, StorageServiceTrait},
|
||||
tasks::TaskService,
|
||||
task::TaskService,
|
||||
};
|
||||
|
||||
pub trait AppContextTrait: Send + Sync + Debug {
|
||||
@@ -29,6 +30,7 @@ pub trait AppContextTrait: Send + Sync + Debug {
|
||||
fn environment(&self) -> &Environment;
|
||||
fn crypto(&self) -> &CryptoService;
|
||||
fn task(&self) -> &TaskService;
|
||||
fn message(&self) -> &MessageService;
|
||||
}
|
||||
|
||||
pub struct AppContext {
|
||||
@@ -43,6 +45,7 @@ pub struct AppContext {
|
||||
crypto: CryptoService,
|
||||
working_dir: String,
|
||||
environment: Environment,
|
||||
message: MessageService,
|
||||
task: OnceCell<TaskService>,
|
||||
}
|
||||
|
||||
@@ -58,6 +61,7 @@ impl AppContext {
|
||||
let cache = CacheService::from_config(config.cache).await?;
|
||||
let db = DatabaseService::from_config(config.database).await?;
|
||||
let storage = StorageService::from_config(config.storage).await?;
|
||||
let message = MessageService::from_config(config.message).await?;
|
||||
let auth = AuthService::from_conf(config.auth).await?;
|
||||
let mikan = MikanClient::from_config(config.mikan).await?;
|
||||
let crypto = CryptoService::from_config(config.crypto).await?;
|
||||
@@ -75,12 +79,13 @@ impl AppContext {
|
||||
working_dir: working_dir.to_string(),
|
||||
graphql,
|
||||
crypto,
|
||||
message,
|
||||
task: OnceCell::new(),
|
||||
});
|
||||
|
||||
ctx.task
|
||||
.get_or_try_init(async || {
|
||||
TaskService::from_config_and_ctx(config.tasks, ctx.clone()).await
|
||||
TaskService::from_config_and_ctx(config.task, ctx.clone()).await
|
||||
})
|
||||
.await?;
|
||||
|
||||
@@ -131,4 +136,7 @@ impl AppContextTrait for AppContext {
|
||||
fn task(&self) -> &TaskService {
|
||||
self.task.get().expect("task should be set")
|
||||
}
|
||||
fn message(&self) -> &MessageService {
|
||||
&self.message
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user