Skip to content

Commit

Permalink
Fix iotedge restart command with workload sockets (#6891) (#6898)
Browse files Browse the repository at this point in the history
Fix the `iotedge restart` command so that it sends a stop request followed by a start request. This is necessary so that workload sockets are properly notified of the restart.
  • Loading branch information
gordonwang0 authored Feb 13, 2023
1 parent 37f51c2 commit 08dfac5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
23 changes: 17 additions & 6 deletions edgelet/iotedge/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,22 @@ impl ModuleRuntime for MgmtClient {
type Module = MgmtModule;
type ModuleRegistry = Self;

async fn restart(&self, id: &str) -> anyhow::Result<()> {
let path = format!("/modules/{}/restart?api-version={}", id, API_VERSION);
async fn start(&self, id: &str) -> anyhow::Result<()> {
let path = format!("/modules/{}/start?api-version={}", id, API_VERSION);
let uri = self.get_uri(&path)?;

let request: HttpRequest<(), _> = HttpRequest::post(self.connector.clone(), &uri, None);

request
.no_content_response()
.await
.context(Error::ModuleRuntime)?;

Ok(())
}

async fn stop(&self, id: &str, _wait_before_kill: Option<Duration>) -> anyhow::Result<()> {
let path = format!("/modules/{}/stop?api-version={}", id, API_VERSION);
let uri = self.get_uri(&path)?;

let request: HttpRequest<(), _> = HttpRequest::post(self.connector.clone(), &uri, None);
Expand Down Expand Up @@ -146,10 +160,7 @@ impl ModuleRuntime for MgmtClient {
async fn get(&self, _id: &str) -> anyhow::Result<(Self::Module, ModuleRuntimeState)> {
unimplemented!()
}
async fn start(&self, _id: &str) -> anyhow::Result<()> {
unimplemented!()
}
async fn stop(&self, _id: &str, _wait_before_kill: Option<Duration>) -> anyhow::Result<()> {
async fn restart(&self, _id: &str) -> anyhow::Result<()> {
unimplemented!()
}
async fn remove(&self, _id: &str) -> anyhow::Result<()> {
Expand Down
24 changes: 19 additions & 5 deletions edgelet/iotedge/src/restart.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.

use std::io::Write;
use std::fmt::Write;
use std::sync::{Arc, Mutex};

use anyhow::Context;
Expand Down Expand Up @@ -28,14 +28,28 @@ impl<M, W> Restart<M, W> {
impl<M, W> Restart<M, W>
where
M: ModuleRuntime,
W: Write + Send,
W: std::io::Write + Send,
{
pub async fn execute(&self) -> anyhow::Result<()> {
let write = self.output.clone();
self.runtime.restart(&self.id).await?;
let mut output = String::new();

// A stop request must be sent to workload socket manager first.
// To properly restart, both the stop and start APIs must be called.
if let Err(err) = self.runtime.stop(&self.id, None).await {
writeln!(
output,
"warn: {} was not stopped gracefully: {}",
self.id, err
)?;
}

self.runtime.start(&self.id).await?;
writeln!(output, "Restarted {}", self.id)?;

let write = self.output.clone();
let mut w = write.lock().unwrap();
writeln!(w, "{}", self.id).context(Error::WriteToStdout)?;
write!(w, "{}", output).context(Error::WriteToStdout)?;

Ok(())
}
}

0 comments on commit 08dfac5

Please sign in to comment.