From e524a17d35a9c607cf2b233389586d372bf75f77 Mon Sep 17 00:00:00 2001 From: Arne Beer Date: Tue, 16 Aug 2022 18:05:34 +0200 Subject: [PATCH] refactor: Make TaskToRestart fields optional --- client/commands/restart.rs | 22 ++++++++++++++-------- daemon/network/message_handler/restart.rs | 16 +++++++++++----- lib/CHANGELOG.md | 3 ++- lib/src/network/message.rs | 12 ++++++------ tests/daemon/aliases.rs | 6 +++--- tests/daemon/restart.rs | 12 ++++++------ 6 files changed, 42 insertions(+), 29 deletions(-) diff --git a/client/commands/restart.rs b/client/commands/restart.rs index 3b549f30..7e42d397 100644 --- a/client/commands/restart.rs +++ b/client/commands/restart.rs @@ -81,23 +81,28 @@ pub async fn restart( new_task.status = new_status.clone(); // Path and command can be edited, if the use specified the -e or -p flag. - let mut command = task.original_command.clone(); - let mut path = task.path.clone(); + let mut command = None; + let mut path = None; + + // Update the command if requested. if edit_command { - command = edit_line_wrapper(stream, *task_id, &command).await? + command = Some(edit_line_wrapper(stream, *task_id, &task.command).await?); }; + + // Update the path if requested. if edit_path { - let str_path = path + let str_path = task + .path .to_str() .context("Failed to convert task path to string")?; let changed_path = edit_line_wrapper(stream, *task_id, str_path).await?; - path = PathBuf::from(changed_path); + path = Some(PathBuf::from(changed_path)); } // Add the tasks to the singular message, if we want to restart the tasks in-place. // And continue with the next task. The message will then be sent after the for loop. if in_place { - restart_message.tasks.push(TasksToRestart { + restart_message.tasks.push(TaskToRestart { task_id: *task_id, command, path, @@ -106,10 +111,11 @@ pub async fn restart( continue; } + // In case we don't do in-place restarts, we have to add a new task. // Create a AddMessage to send the task to the daemon from the updated info and the old task. let add_task_message = Message::Add(AddMessage { - command, - path, + command: command.unwrap_or_else(|| task.command.clone()), + path: path.unwrap_or_else(|| task.path.clone()), envs: task.envs.clone(), start_immediately, stashed, diff --git a/daemon/network/message_handler/restart.rs b/daemon/network/message_handler/restart.rs index 6dd1a0ba..024bceb1 100644 --- a/daemon/network/message_handler/restart.rs +++ b/daemon/network/message_handler/restart.rs @@ -56,7 +56,7 @@ pub fn restart_multiple( /// new task, which is completely handled on the client-side. fn restart( state: &mut MutexGuard, - to_restart: &TasksToRestart, + to_restart: &TaskToRestart, stashed: bool, settings: &Settings, ) { @@ -79,10 +79,16 @@ fn restart( TaskStatus::Queued }; - // Update command and path. - task.original_command = to_restart.command.clone(); - task.command = insert_alias(settings, to_restart.command.clone()); - task.path = to_restart.path.clone(); + // Update command if applicable. + if let Some(new_command) = &to_restart.command { + task.original_command = new_command.clone(); + task.command = insert_alias(settings, new_command.clone()); + } + + // Update path if applicable. + if let Some(path) = &to_restart.path { + task.path = path.clone(); + } // Reset all variables of any previous run. task.start = None; diff --git a/lib/CHANGELOG.md b/lib/CHANGELOG.md index 5d254f82..7f02c9d9 100644 --- a/lib/CHANGELOG.md +++ b/lib/CHANGELOG.md @@ -8,7 +8,6 @@ The concept of SemVer is applied to the daemon/client API, but not the library A ## [0.21.0] - unreleased - ### Added - Added `Settings.shared.alias_file`, which can be used to specify the location of the `pueue_aliases.yml`. @@ -18,6 +17,8 @@ The concept of SemVer is applied to the daemon/client API, but not the library A - The process handling code has been moved from the daemon to `pueue_lib`. See [#336](https://github.com/Nukesor/pueue/issues/336). The reason for this is, that the client will need some of these process handling capabilitites to spawn shell commands when editing tasks. - The module structure of the platform specific networking code has been streamlined. +- Renamed `TasksToRestart` to `TaskToRestart`. +- Make `TaskToRestart::path` and `TaskToRestart::command` optional. ## [0.20.0] - 2022-07-21 diff --git a/lib/src/network/message.rs b/lib/src/network/message.rs index 546c1de2..7fab1e25 100644 --- a/lib/src/network/message.rs +++ b/lib/src/network/message.rs @@ -132,18 +132,18 @@ pub struct StartMessage { /// It's possible to update the command and paths when restarting tasks. #[derive(PartialEq, Eq, Clone, Debug, Deserialize, Serialize)] pub struct RestartMessage { - pub tasks: Vec, + pub tasks: Vec, pub start_immediately: bool, pub stashed: bool, } #[derive(PartialEq, Eq, Clone, Debug, Deserialize, Serialize)] -pub struct TasksToRestart { +pub struct TaskToRestart { pub task_id: usize, - /// The command that should be used when restarting the task. - pub command: String, - /// The path that should be used when restarting the task. - pub path: PathBuf, + /// Allow to restart the task with an updated command. + pub command: Option, + /// Allow to restart the task with an updated path. + pub path: Option, } #[derive(PartialEq, Eq, Clone, Debug, Deserialize, Serialize)] diff --git a/tests/daemon/aliases.rs b/tests/daemon/aliases.rs index 77a7ae09..25da9a65 100644 --- a/tests/daemon/aliases.rs +++ b/tests/daemon/aliases.rs @@ -60,10 +60,10 @@ async fn test_restart_with_alias() -> Result<()> { // Restart the task while editing its command. let message = Message::Restart(RestartMessage { - tasks: vec![TasksToRestart { + tasks: vec![TaskToRestart { task_id: 0, - command: "replaced_cmd test".to_string(), - path: daemon.tempdir.path().to_owned(), + command: Some("replaced_cmd test".to_string()), + path: None, }], start_immediately: true, stashed: false, diff --git a/tests/daemon/restart.rs b/tests/daemon/restart.rs index a871acd5..b10b6c7b 100644 --- a/tests/daemon/restart.rs +++ b/tests/daemon/restart.rs @@ -21,10 +21,10 @@ async fn test_restart_in_place() -> Result<()> { // Restart task 0 with an extended sleep command with a different path. let restart_message = Message::Restart(RestartMessage { - tasks: vec![TasksToRestart { + tasks: vec![TaskToRestart { task_id: 0, - command: "sleep 60".to_string(), - path: PathBuf::from("/tmp"), + command: Some("sleep 60".to_string()), + path: Some(PathBuf::from("/tmp")), }], start_immediately: false, stashed: false, @@ -60,10 +60,10 @@ async fn test_cannot_restart_running() -> Result<()> { // Restart task 0 with an extended sleep command. let restart_message = Message::Restart(RestartMessage { - tasks: vec![TasksToRestart { + tasks: vec![TaskToRestart { task_id: 0, - command: "sleep 60".to_string(), - path: PathBuf::from("/tmp"), + command: None, + path: None, }], start_immediately: false, stashed: false,