Skip to content

Commit

Permalink
refactor(core): add task name for tokio-console better display
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist committed Sep 24, 2024
1 parent b1e8ecd commit a518e43
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 7 deletions.
3 changes: 3 additions & 0 deletions crates/rspack_core/src/compiler/make/repair/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub struct AddTask {
}

impl Task<MakeTaskContext> for AddTask {
fn name(&self) -> &'static str {
"add_task"
}
fn get_task_type(&self) -> TaskType {
TaskType::Sync
}
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_core/src/compiler/make/repair/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct BuildTask {

#[async_trait::async_trait]
impl Task<MakeTaskContext> for BuildTask {
fn name(&self) -> &'static str {
"build_task"
}
fn get_task_type(&self) -> TaskType {
TaskType::Async
}
Expand Down Expand Up @@ -106,6 +109,9 @@ struct BuildResultTask {
}

impl Task<MakeTaskContext> for BuildResultTask {
fn name(&self) -> &'static str {
"build_result"
}
fn get_task_type(&self) -> TaskType {
TaskType::Sync
}
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_core/src/compiler/make/repair/factorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub struct FactorizeTask {

#[async_trait::async_trait]
impl Task<MakeTaskContext> for FactorizeTask {
fn name(&self) -> &'static str {
"factorize_task"
}
fn get_task_type(&self) -> TaskType {
TaskType::Async
}
Expand Down Expand Up @@ -198,6 +201,9 @@ impl FactorizeResultTask {
}

impl Task<MakeTaskContext> for FactorizeResultTask {
fn name(&self) -> &'static str {
"factorize_result"
}
fn get_task_type(&self) -> TaskType {
TaskType::Sync
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct ProcessDependenciesTask {
}

impl Task<MakeTaskContext> for ProcessDependenciesTask {
fn name(&self) -> &'static str {
"process_dependencies_task"
}
fn get_task_type(&self) -> TaskType {
TaskType::Sync
}
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_core/src/compiler/module_executor/ctrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ impl CtrlTask {

#[async_trait::async_trait]
impl Task<MakeTaskContext> for CtrlTask {
fn name(&self) -> &'static str {
"control_task"
}
fn get_task_type(&self) -> TaskType {
TaskType::Async
}
Expand Down Expand Up @@ -162,6 +165,9 @@ struct FinishModuleTask {
}

impl Task<MakeTaskContext> for FinishModuleTask {
fn name(&self) -> &'static str {
return "finish_module_task";

Check failure on line 169 in crates/rspack_core/src/compiler/module_executor/ctrl.rs

View workflow job for this annotation

GitHub Actions / Rust check

unneeded `return` statement
}
fn get_task_type(&self) -> TaskType {
TaskType::Sync
}
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/compiler/module_executor/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub struct EntryTask {
}

impl Task<MakeTaskContext> for EntryTask {
fn name(&self) -> &'static str {
"entry_task"
}
fn get_task_type(&self) -> TaskType {
TaskType::Sync
}
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/compiler/module_executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub struct ExecuteTask {
}

impl Task<MakeTaskContext> for ExecuteTask {
fn name(&self) -> &'static str {
"execute_task"
}
fn get_task_type(&self) -> TaskType {
TaskType::Sync
}
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/compiler/module_executor/overwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub struct OverwriteTask {

#[async_trait::async_trait]
impl Task<MakeTaskContext> for OverwriteTask {
fn name(&self) -> &'static str {
"overwrite_task"
}
fn get_task_type(&self) -> TaskType {
self.origin_task.get_task_type()
}
Expand Down
25 changes: 18 additions & 7 deletions crates/rspack_core/src/utils/task_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub trait Task<Ctx>: Send + Any + AsAny {
/// Return `TaskType::Sync` will run `self::sync_run`
/// Return `TaskType::Async` will run `self::async_run`
fn get_task_type(&self) -> TaskType;

/// get task name for monitor
fn name(&self) -> &'static str;
/// Sync task process
///
/// The context is shared with all tasks
Expand Down Expand Up @@ -85,12 +86,16 @@ pub fn run_task_loop_with_event<Ctx: 'static>(
let tx = tx.clone();
let is_expected_shutdown = is_expected_shutdown.clone();
active_task_count += 1;
tokio::spawn(async move {
let r = task.async_run().await;
if !is_expected_shutdown.load(Ordering::Relaxed) {
tx.send(r).expect("failed to send error message");
}
});
// safe expect here since spawn always return Ok
tokio::task::Builder::new()
.name(task.name())
.spawn(async move {
let r = task.async_run().await;
if !is_expected_shutdown.load(Ordering::Relaxed) {
tx.send(r).expect("failed to send error message");
}
})
.expect("spawn task failed");
}
TaskType::Sync => {
// merge sync task result directly
Expand Down Expand Up @@ -150,6 +155,9 @@ mod test {

struct SyncTask;
impl Task<Context> for SyncTask {
fn name(&self) -> &'static str {
"sync_task"
}
fn get_task_type(&self) -> TaskType {
TaskType::Sync
}
Expand All @@ -175,6 +183,9 @@ mod test {
}
#[async_trait::async_trait]
impl Task<Context> for AsyncTask {
fn name(&self) -> &'static str {
"async_task"
}
fn get_task_type(&self) -> TaskType {
TaskType::Async
}
Expand Down

0 comments on commit a518e43

Please sign in to comment.