Skip to content

Commit

Permalink
refactor: Make TaskToRestart fields optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Nukesor committed Aug 16, 2022
1 parent 93951c6 commit e524a17
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
22 changes: 14 additions & 8 deletions client/commands/restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
16 changes: 11 additions & 5 deletions daemon/network/message_handler/restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn restart_multiple(
/// new task, which is completely handled on the client-side.
fn restart(
state: &mut MutexGuard<State>,
to_restart: &TasksToRestart,
to_restart: &TaskToRestart,
stashed: bool,
settings: &Settings,
) {
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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

Expand Down
12 changes: 6 additions & 6 deletions lib/src/network/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TasksToRestart>,
pub tasks: Vec<TaskToRestart>,
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<String>,
/// Allow to restart the task with an updated path.
pub path: Option<PathBuf>,
}

#[derive(PartialEq, Eq, Clone, Debug, Deserialize, Serialize)]
Expand Down
6 changes: 3 additions & 3 deletions tests/daemon/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions tests/daemon/restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e524a17

Please sign in to comment.