Skip to content

Commit

Permalink
Rename BgExitHandlers -> ExecHandlers
Browse files Browse the repository at this point in the history
The new name is more appropriate, now that these handlers are used not
only for background scripts but also to trigger a sub operation or a
builtin action.

Signed-off-by: Didier Wenzek <[email protected]>
  • Loading branch information
didier-wenzek committed Aug 22, 2024
1 parent 0687985 commit 4ec89b9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
10 changes: 5 additions & 5 deletions crates/core/tedge_api/src/workflow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ pub enum OperationAction {
/// on_success = "<state>"
/// on_error = "<state>"
/// ```
BuiltIn(BgExitHandlers, AwaitHandlers),
BuiltIn(ExecHandlers, AwaitHandlers),

/// Trigger a built-in operation
///
/// ```toml
/// action = "<builtin-operation-name>"
/// on_exec = "<state>"
/// ```
BuiltInAction(OperationName, BgExitHandlers),
BuiltInAction(OperationName, ExecHandlers),

/// Await the outcome of a built-in operation
///
Expand Down Expand Up @@ -111,7 +111,7 @@ pub enum OperationAction {
/// background_script = "sudo systemctl restart tedge-agent"
/// on_exec = "<state>"
/// ```
BgScript(ShellScript, BgExitHandlers),
BgScript(ShellScript, ExecHandlers),

/// Trigger an operation and move to the next state from where the outcome of the operation will be awaited
///
Expand All @@ -125,7 +125,7 @@ pub enum OperationAction {
OperationName,
Option<ShellScript>,
StateExcerpt,
BgExitHandlers,
ExecHandlers,
),

/// Await the completion of a sub-operation
Expand Down Expand Up @@ -237,7 +237,7 @@ impl OperationWorkflow {
/// Create a built-in operation workflow
pub fn built_in(operation: OperationType) -> Self {
let operation_name = operation.to_string();
let exec_handler = BgExitHandlers::builtin_default();
let exec_handler = ExecHandlers::builtin_default();
let await_handler = AwaitHandlers::builtin_default();
let states = [
("init", OperationAction::MoveTo("scheduled".to_string())),
Expand Down
12 changes: 6 additions & 6 deletions crates/core/tedge_api/src/workflow/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,23 +297,23 @@ fn extract_script_output(stdout: String) -> Option<String> {
None
}

/// Define how to handle a background script
/// Define how to handle background scripts and actions
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct BgExitHandlers {
pub struct ExecHandlers {
pub on_exec: GenericStateUpdate,
}

impl BgExitHandlers {
impl ExecHandlers {
pub fn try_new(on_exec: Option<GenericStateUpdate>) -> Result<Self, ScriptDefinitionError> {
Ok(BgExitHandlers {
Ok(ExecHandlers {
on_exec: on_exec.unwrap_or_else(GenericStateUpdate::successful),
})
}
}

impl BgExitHandlers {
impl ExecHandlers {
pub fn builtin_default() -> Self {
BgExitHandlers {
ExecHandlers {
on_exec: GenericStateUpdate::executing(),
}
}
Expand Down
24 changes: 11 additions & 13 deletions crates/core/tedge_api/src/workflow/toml_config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::mqtt_topics::OperationType;
use crate::workflow::AwaitHandlers;
use crate::workflow::BgExitHandlers;
use crate::workflow::DefaultHandlers;
use crate::workflow::ExecHandlers;
use crate::workflow::ExitHandlers;
use crate::workflow::GenericCommandState;
use crate::workflow::GenericStateUpdate;
Expand Down Expand Up @@ -128,11 +128,11 @@ impl TryFrom<(TomlOperationState, DefaultHandlers)> for OperationAction {
Ok(OperationAction::Script(script, handlers))
}
TomlOperationAction::BackgroundScript(script) => {
let handlers = TryInto::<BgExitHandlers>::try_into((input.handlers, defaults))?;
let handlers = TryInto::<ExecHandlers>::try_into((input.handlers, defaults))?;
Ok(OperationAction::BgScript(script, handlers))
}
TomlOperationAction::Operation(operation) => {
let handlers = TryInto::<BgExitHandlers>::try_into((input.handlers, defaults))?;
let handlers = TryInto::<ExecHandlers>::try_into((input.handlers, defaults))?;
let input_script = input.input_script;
let cmd_input = input.input.try_into()?;
Ok(OperationAction::Operation(
Expand Down Expand Up @@ -173,9 +173,9 @@ impl TryFrom<(TomlOperationState, DefaultHandlers)> for OperationAction {
))
}
"builtin" => {
let exec_handlers = TryInto::<BgExitHandlers>::try_into((
let exec_handlers = TryInto::<ExecHandlers>::try_into((
input.handlers.clone(),
BgExitHandlers::builtin_default(),
ExecHandlers::builtin_default(),
))?;
let await_handlers = TryInto::<AwaitHandlers>::try_into((
input.handlers,
Expand All @@ -186,7 +186,7 @@ impl TryFrom<(TomlOperationState, DefaultHandlers)> for OperationAction {
operation_name => match operation_name.strip_prefix("await-") {
None => {
let handlers =
TryInto::<BgExitHandlers>::try_into((input.handlers, defaults))?;
TryInto::<ExecHandlers>::try_into((input.handlers, defaults))?;
Ok(OperationAction::BuiltInAction(
operation_name.to_string(),
handlers,
Expand Down Expand Up @@ -320,25 +320,23 @@ impl TryFrom<(TomlExitHandlers, DefaultHandlers)> for ExitHandlers {
}
}

impl TryFrom<(TomlExitHandlers, DefaultHandlers)> for BgExitHandlers {
impl TryFrom<(TomlExitHandlers, DefaultHandlers)> for ExecHandlers {
type Error = ScriptDefinitionError;

fn try_from(
(value, _defaults): (TomlExitHandlers, DefaultHandlers),
) -> Result<Self, Self::Error> {
let on_exec = value.on_exec.map(|u| u.into());
BgExitHandlers::try_new(on_exec)
ExecHandlers::try_new(on_exec)
}
}

impl TryFrom<(TomlExitHandlers, BgExitHandlers)> for BgExitHandlers {
impl TryFrom<(TomlExitHandlers, ExecHandlers)> for ExecHandlers {
type Error = ScriptDefinitionError;

fn try_from(
(value, defaults): (TomlExitHandlers, BgExitHandlers),
) -> Result<Self, Self::Error> {
fn try_from((value, defaults): (TomlExitHandlers, ExecHandlers)) -> Result<Self, Self::Error> {
let on_exec = value.on_exec.map(|u| u.into()).or(Some(defaults.on_exec));
BgExitHandlers::try_new(on_exec)
ExecHandlers::try_new(on_exec)
}
}

Expand Down

0 comments on commit 4ec89b9

Please sign in to comment.