Skip to content

feat: restart tedge-agent on tedge connect #3347

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: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Context as _;
/// Extension trait for `SystemServiceManager`.
pub trait SystemServiceManagerExt {
fn start_and_enable_service(&self, service: SystemService<'_>) -> anyhow::Result<()>;
fn restart_and_enable_service(&self, service: SystemService<'_>) -> anyhow::Result<()>;
fn stop_and_disable_service(&self, service: SystemService<'_>) -> anyhow::Result<()>;
}

Expand All @@ -15,6 +16,13 @@ impl SystemServiceManagerExt for &dyn SystemServiceManager {
.with_context(|| format!("Failed to enable {service}"))
}

fn restart_and_enable_service(&self, service: SystemService<'_>) -> anyhow::Result<()> {
self.restart_service(service)
.with_context(|| format!("Failed to start {service}"))?;
self.enable_service(service)
.with_context(|| format!("Failed to enable {service}"))
}

fn stop_and_disable_service(&self, service: SystemService<'_>) -> anyhow::Result<()> {
self.stop_service(service)
.with_context(|| format!("Failed to stop {service}"))?;
Expand Down
4 changes: 3 additions & 1 deletion crates/core/tedge/src/cli/connect/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,10 @@ fn enable_software_management(
if bridge_config.use_agent {
if which("tedge-agent").is_ok() {
let spinner = Spinner::start("Enabling tedge-agent");
// Restart the agent in case it is already running
// and so it picks up the latest settings (e.g. updated mqtt port)
let _ = spinner
.finish(service_manager.start_and_enable_service(SystemService::TEdgeSMAgent));
.finish(service_manager.restart_and_enable_service(SystemService::TEdgeSMAgent));
} else {
println!("Info: Software management is not installed. So, skipping enabling related components.\n");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
*** Settings ***
Resource ../../resources/common.resource
Library ThinEdgeIO

Test Setup Test Setup
Test Teardown Get Logs

Test Tags theme:cli theme:mqtt theme:c8y


*** Test Cases ***
Restart agent on connection
[Documentation] tedge connect/reconnect should restart tedge-agent service to ensure it has the latest settings

Service Should Be Running tedge-agent
${agent_pid_before} Execute Command sudo systemctl show --property MainPID tedge-agent

# Change some configuration
Execute Command
... tedge config unset mqtt.client.auth.ca_file && tedge config unset mqtt.client.auth.cert_file && tedge config unset mqtt.client.auth.key_file
Execute Command tedge config set mqtt.bind.port 1884 && tedge config set mqtt.client.port 1884

Execute Command sudo tedge reconnect c8y
Execute Command sudo tedge connect c8y --test
${agent_pid_after} Execute Command sudo systemctl show --property MainPID tedge-agent
Should Not Be Equal ${agent_pid_before} ${agent_pid_after}

Service Should Be Running tedge-agent
Service Health Status Should Be Up tedge-agent


*** Keywords ***
Test Setup
Setup
Loading