Skip to content

Commit

Permalink
Merge pull request #2846 from didier-wenzek/refactor/use_uniform_comm…
Browse files Browse the repository at this point in the history
…and_repr

Use Command<Payload> for log, config and firmware commands
  • Loading branch information
albinsuresh authored Apr 29, 2024
2 parents d5478ce + 03e8699 commit e2b9678
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 35 deletions.
1 change: 1 addition & 0 deletions crates/core/tedge_agent/src/operation_file_cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use tedge_actors::RuntimeRequestSink;
use tedge_actors::Sender;
use tedge_actors::Service;
use tedge_actors::SimpleMessageBoxBuilder;
use tedge_api::commands::CommandPayload;
use tedge_api::commands::ConfigUpdateCmdPayload;
use tedge_api::mqtt_topics::Channel;
use tedge_api::mqtt_topics::ChannelFilter;
Expand Down
124 changes: 89 additions & 35 deletions crates/core/tedge_api/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ where
.with_retain()
.with_qos(QoS::AtLeastOnce)
}

/// Mark the command as executing
pub fn executing(&mut self) {
self.payload.executing();
}

/// Mark the command as successful
pub fn successful(&mut self) {
self.payload.successful();
}

/// Mark the command as failed
pub fn failed(&mut self, reason: impl Into<String>) {
self.payload.failed(reason);
}
}

impl<Payload> Command<Payload>
Expand Down Expand Up @@ -166,8 +181,27 @@ pub trait CommandPayload {
fn set_status(&mut self, status: CommandStatus);

/// Set the failure reason of the command
fn set_error(&mut self, reason: String) {
self.set_status(CommandStatus::Failed { reason });
fn set_error(&mut self, reason: impl Into<String>) {
self.set_status(CommandStatus::Failed {
reason: reason.into(),
});
}

/// Mark the command as executing
fn executing(&mut self) {
self.set_status(CommandStatus::Executing);
}

/// Mark the command as successful
fn successful(&mut self) {
self.set_status(CommandStatus::Successful);
}

/// Mark the command as failed
fn failed(&mut self, reason: impl Into<String>) {
self.set_status(CommandStatus::Failed {
reason: reason.into(),
});
}
}

Expand Down Expand Up @@ -654,6 +688,9 @@ pub enum OperationStatus {
Executing,
}

/// Command to request a log file to be uploaded
pub type LogUploadCmd = Command<LogUploadCmdPayload>;

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct LogMetadata {
Expand Down Expand Up @@ -681,22 +718,23 @@ pub struct LogUploadCmdPayload {

impl<'a> Jsonify<'a> for LogUploadCmdPayload {}

impl LogUploadCmdPayload {
pub fn executing(&mut self) {
self.status = CommandStatus::Executing;
impl CommandPayload for LogUploadCmdPayload {
fn operation_type() -> OperationType {
OperationType::LogUpload
}

pub fn successful(&mut self) {
self.status = CommandStatus::Successful;
fn status(&self) -> CommandStatus {
self.status.clone()
}

pub fn failed(&mut self, reason: impl Into<String>) {
self.status = CommandStatus::Failed {
reason: reason.into(),
};
fn set_status(&mut self, status: CommandStatus) {
self.status = status
}
}

/// Command to request a configuration snapshot to be uploaded
pub type ConfigSnapshotCmd = Command<ConfigSnapshotCmdPayload>;

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ConfigMetadata {
Expand All @@ -720,6 +758,20 @@ pub struct ConfigSnapshotCmdPayload {

impl<'a> Jsonify<'a> for ConfigSnapshotCmdPayload {}

impl CommandPayload for ConfigSnapshotCmdPayload {
fn operation_type() -> OperationType {
OperationType::ConfigSnapshot
}

fn status(&self) -> CommandStatus {
self.status.clone()
}

fn set_status(&mut self, status: CommandStatus) {
self.status = status
}
}

impl ConfigSnapshotCmdPayload {
pub fn executing(&mut self, tedge_url: Option<String>) {
self.status = CommandStatus::Executing;
Expand All @@ -732,14 +784,11 @@ impl ConfigSnapshotCmdPayload {
self.status = CommandStatus::Successful;
self.path = Some(path.into())
}

pub fn failed(&mut self, reason: impl Into<String>) {
self.status = CommandStatus::Failed {
reason: reason.into(),
}
}
}

/// Command to request a configuration to be updated
pub type ConfigUpdateCmd = Command<ConfigUpdateCmdPayload>;

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ConfigUpdateCmdPayload {
Expand All @@ -756,23 +805,30 @@ pub struct ConfigUpdateCmdPayload {

impl<'a> Jsonify<'a> for ConfigUpdateCmdPayload {}

impl ConfigUpdateCmdPayload {
pub fn executing(&mut self) {
self.status = CommandStatus::Executing;
impl CommandPayload for ConfigUpdateCmdPayload {
fn operation_type() -> OperationType {
OperationType::ConfigUpdate
}

fn status(&self) -> CommandStatus {
self.status.clone()
}

fn set_status(&mut self, status: CommandStatus) {
self.status = status
}
}

impl ConfigUpdateCmdPayload {
pub fn successful(&mut self, path: impl Into<String>) {
self.status = CommandStatus::Successful;
self.path = Some(path.into())
}

pub fn failed(&mut self, reason: impl Into<String>) {
self.status = CommandStatus::Failed {
reason: reason.into(),
};
}
}

/// Command to update the device firmware
pub type FirmwareUpdateCmd = Command<FirmwareUpdateCmdPayload>;

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FirmwareInfo {
Expand All @@ -798,19 +854,17 @@ pub struct FirmwareUpdateCmdPayload {

impl<'a> Jsonify<'a> for FirmwareUpdateCmdPayload {}

impl FirmwareUpdateCmdPayload {
pub fn executing(&mut self) {
self.status = CommandStatus::Executing;
impl CommandPayload for FirmwareUpdateCmdPayload {
fn operation_type() -> OperationType {
OperationType::FirmwareUpdate
}

pub fn successful(&mut self) {
self.status = CommandStatus::Successful;
fn status(&self) -> CommandStatus {
self.status.clone()
}

pub fn failed(&mut self, reason: impl Into<String>) {
self.status = CommandStatus::Failed {
reason: reason.into(),
};
fn set_status(&mut self, status: CommandStatus) {
self.status = status
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/extensions/tedge_config_manager/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tedge_actors::LoggingSender;
use tedge_actors::MessageReceiver;
use tedge_actors::RuntimeError;
use tedge_actors::Sender;
use tedge_api::commands::CommandPayload;
use tedge_api::commands::CommandStatus;
use tedge_api::commands::ConfigSnapshotCmdPayload;
use tedge_api::commands::ConfigUpdateCmdPayload;
Expand Down
1 change: 1 addition & 0 deletions crates/extensions/tedge_log_manager/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tedge_actors::NoMessage;
use tedge_actors::RuntimeError;
use tedge_actors::Sender;
use tedge_actors::SimpleMessageBox;
use tedge_api::commands::CommandPayload;
use tedge_api::commands::CommandStatus;
use tedge_api::commands::LogUploadCmdPayload;
use tedge_api::Jsonify;
Expand Down

0 comments on commit e2b9678

Please sign in to comment.