Skip to content

Add restart count increment on task restarts #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/shared/src/models/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub struct Task {
pub scheduling_config: Option<SchedulingConfig>,
#[serde(default)]
pub storage_config: Option<StorageConfig>,
#[serde(default)]
pub restart_count: u32,
}

impl Default for Task {
Expand All @@ -103,6 +105,7 @@ impl Default for Task {
updated_at: None,
scheduling_config: None,
storage_config: None,
restart_count: 0,
}
}
}
Expand Down Expand Up @@ -158,6 +161,7 @@ impl TryFrom<TaskRequest> for Task {
updated_at: None,
scheduling_config: request.scheduling_config,
storage_config: request.storage_config,
restart_count: 0,
})
}
}
Expand Down
9 changes: 8 additions & 1 deletion crates/worker/src/docker/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,12 @@ impl DockerService {
pub async fn restart_task(&self) -> Result<(), Box<dyn std::error::Error>> {
let current_task = self.state.get_current_task().await;
match current_task {
Some(task) => {
Some(mut task) => {
let container_id = format!("{}-{}", TASK_PREFIX, task.id);
self.docker_manager.restart_container(&container_id).await?;
task.restart_count = task.restart_count.saturating_add(1);
task.state = TaskState::RESTARTING;
self.state.set_current_task(Some(task.clone())).await;
Ok(())
}
None => Ok(()),
Expand Down Expand Up @@ -387,6 +390,10 @@ mod tests {
.state
.set_current_task(Some(task_clone))
.await;
docker_service.restart_task().await.unwrap();
let updated_task = docker_service.state.get_current_task().await.unwrap();
assert_eq!(updated_task.restart_count, 1);
assert_eq!(updated_task.state, TaskState::RESTARTING);

assert_eq!(
docker_service.state.get_current_task().await.unwrap().name,
Expand Down
8 changes: 8 additions & 0 deletions crates/worker/src/docker/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ impl DockerState {
let mut is_running_guard = self.is_running.lock().await;
*is_running_guard = is_running;
}

pub async fn increment_restart_count(&self) {
let mut current_task = self.current_task.lock().await;
if let Some(task) = current_task.as_mut() {
task.restart_count += 1;
}
}
}