Skip to content

Commit

Permalink
schedule task
Browse files Browse the repository at this point in the history
  • Loading branch information
biezhihua committed Sep 11, 2023
1 parent 5420774 commit d368652
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 21 deletions.
Binary file modified .images/biezhihua.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
- [x] 完成资源整理任务调用
- [x] 扩展路径命名方式
- [x] 资源整理多级目录支持
- [ ] 资源分类配置化
- [x] 资源命名配置化
- [x] Web端支持资源命名配置化
- [ ] Web端支持分类配置化
- [ ] 资源刮削能力
- [ ] 定时能力
- [ ] 自动添加媒体库的能力
Expand Down
2 changes: 1 addition & 1 deletion soda_media_tools_server/src/api/directory_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn convert_paths(paths: Vec<PathInfo>) -> Vec<SodaPathInfo> {
}

pub fn stage() -> AdHoc {
AdHoc::on_ignite("directory tree api stage", |rocket| async {
AdHoc::on_ignite("api directory tree stage", |rocket| async {
rocket.mount("/", routes![api_local_path_get])
})
}
7 changes: 4 additions & 3 deletions soda_media_tools_server/src/api/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ use crate::config;
use crate::db::{Db, table_resource_sync_config_helper};
use crate::db::models::ResourceSyncConfigItem;
use crate::entity::{SodaResourceSetting, SodaResourceSyncConfigItem, SodaResponse, SodaResult, SodaTaskMsg};
use crate::task::resource::StateResource;

#[get("/api/resource/management/sync/organization?<id>")]
pub(crate) async fn api_resource_sync_organization(db: Db, tx: &State<Sender<SodaTaskMsg>>, id: String) -> SodaResult<Json<SodaResponse<ResourceSyncConfigItem>>> {
pub(crate) async fn api_resource_sync_organization(db: Db, resource_tx: &State<StateResource>, id: String) -> SodaResult<Json<SodaResponse<ResourceSyncConfigItem>>> {
let config_item = table_resource_sync_config_helper::get_with_id(db, id.parse().unwrap()).await?;

if config_item.is_some() {
let raw_config_item = config_item.unwrap();

let sender = tx.clone();
let sender = resource_tx.resource_tx.clone();
sender.try_send(SodaTaskMsg::create_organization_task_msg(raw_config_item)).unwrap();

Ok(Json(SodaResponse::success_default()))
Expand Down Expand Up @@ -93,7 +94,7 @@ pub(crate) async fn api_resource_setting_update(item: Json<SodaResourceSetting>)
}

pub fn stage() -> AdHoc {
AdHoc::on_ignite("resource api stage", |rocket| async {
AdHoc::on_ignite("api resource stage", |rocket| async {
rocket.mount("/", routes![
api_resource_sync_organization,
api_resource_sync_list,
Expand Down
2 changes: 1 addition & 1 deletion soda_media_tools_server/src/api/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) async fn api_basic_setting_get() -> Value {
}

pub(crate) fn stage() -> AdHoc {
AdHoc::on_ignite("setting api stage", |rocket| async {
AdHoc::on_ignite("api setting stage", |rocket| async {
rocket.mount("/", routes![
api_basic_setting_update,
api_basic_setting_get
Expand Down
2 changes: 1 addition & 1 deletion soda_media_tools_server/src/api/task.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rocket::fairing::AdHoc;

pub(crate) fn stage() -> AdHoc {
AdHoc::on_ignite("task stage", |rocket| async {
AdHoc::on_ignite("api task stage", |rocket| async {
rocket.mount("/", routes![])
})
}
2 changes: 1 addition & 1 deletion soda_media_tools_server/src/api/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub(crate) async fn api_user_login_out(token: Option<SodaToken>) -> Json<SodaRes
}

pub(crate) fn stage() -> AdHoc {
AdHoc::on_ignite("user api stage", |rocket| async {
AdHoc::on_ignite("api user stage", |rocket| async {
rocket.mount("/", routes![
api_user_current_get,
api_user_login_account,
Expand Down
2 changes: 1 addition & 1 deletion soda_media_tools_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ fn rocket() -> Rocket<Build> {
rocket::build()
.attach(api::stage())
.attach(db::stage())
.attach(task::resource_organization::stage())
.attach(task::stage())
.mount("/", routes![index])
}
13 changes: 12 additions & 1 deletion soda_media_tools_server/src/task.rs
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
pub(crate) mod resource_organization;
use rocket::fairing::AdHoc;

pub(crate) mod resource;
pub(crate) mod schedule;

pub(crate) fn stage() -> AdHoc {
AdHoc::on_ignite("task stage", |rocket| async {
rocket
.attach(resource::stage())
.attach(schedule::stage())
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::SystemTime;

use rocket::fairing::AdHoc;
use tokio::sync::mpsc;
use tokio::sync::mpsc::Receiver;
use tokio::sync::mpsc::{Receiver, Sender};

use soda_media_tools_lib::soda_media::media_context::SodaContext;
use soda_media_tools_lib::soda_media::media_entity::SodaConfig;
Expand All @@ -12,7 +12,6 @@ use crate::entity::{SodaResourceSyncConfigItem, SodaTaskMsg};

async fn start_task(mut rx: Receiver<SodaTaskMsg>) {
loop {
println!("resource organization task");
if let Some(msg) = rx.recv().await {
println!("receive new msg key = {:?}", msg.key);
let start_time = SystemTime::now();
Expand Down Expand Up @@ -48,14 +47,18 @@ fn do_task_resource_organization(item: SodaResourceSyncConfigItem) {
}
}

pub(crate) struct StateResource {
pub(crate) resource_tx: Sender<SodaTaskMsg>,
}

pub fn stage() -> AdHoc {
AdHoc::on_ignite("resource organization", |rocket| async {
let (tx, rx) = mpsc::channel(128);
AdHoc::on_ignite("task resource", |rocket| async {
let (resource_tx, resource_rx) = mpsc::channel(128);
tokio::spawn(async move {
println!("resource organization task start");
start_task(rx).await;
println!("resource organization task stop")
println!("resource task start");
start_task(resource_rx).await;
println!("resource task stop")
});
rocket.manage(tx)
rocket.manage(StateResource { resource_tx })
})
}
38 changes: 38 additions & 0 deletions soda_media_tools_server/src/task/schedule.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::time::Duration;

use rocket::fairing::AdHoc;
use tokio::sync::mpsc;
use tokio::sync::mpsc::{Receiver, Sender};

async fn start_task(tx: Sender<i32>) {
let secs = 60 * 60 * 24;
let mut interval_timer = tokio::time::interval(Duration::new(secs, 0));
loop {
interval_timer.tick().await;
let sender = tx.clone();
tokio::spawn(async {
do_resource_task(sender).await;
}); // For async task
}
}

async fn do_resource_task(tx: Sender<i32>) {
println!("do_resource_task");
}

pub(crate) struct StateSchedule {
pub(crate) schedule_repeating_rx: Receiver<i32>,
}


pub fn stage() -> AdHoc {
AdHoc::on_ignite("task schedule_repeating", |rocket| async {
let (schedule_repeating_tx, schedule_repeating_rx) = mpsc::channel(16);
tokio::spawn(async move {
println!("schedule_repeating task start");
start_task(schedule_repeating_tx).await;
println!("schedule_repeating task stop")
});
rocket.manage(StateSchedule { schedule_repeating_rx })
})
}
2 changes: 1 addition & 1 deletion soda_media_tools_webui/src/pages/ResourceSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const ResourceSetting: React.FC = () => {
<Card
title={intl.formatMessage({
id: 'pages.setting.resource-setting.rename',
defaultMessage: '资源重命名规则',
defaultMessage: '影视资源重命名规则',
})}
style={{
borderRadius: 8,
Expand Down
4 changes: 3 additions & 1 deletion soda_media_tools_webui/src/pages/ResourceSync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ const ResourceSync: React.FC = () => {
await api_resource_sync_delete(itemData.id);
actionRef.current?.reloadAndRest?.();
} else if ("edit" === key) {
console.log(itemData)
setModalUpdateItem(itemData);
setModalUpdate(true);
}else if ("organization" === key) {
api_resource_sync_organization(itemData.id).then();
}
}}
key="actionGroup"
menus={[
{key: 'organization', name: '整理'},
{key: 'edit', name: '编辑'},
{key: 'delete', name: '删除'},
]}
Expand Down

0 comments on commit d368652

Please sign in to comment.