Skip to content

Commit 6efaf27

Browse files
daywalker90vincenzopalazzo
authored andcommitted
feat(core): consistent dynamic plugins install/remove
dynamic plugins now get saved persistently on remove dynamic plugins get stopped do not return on every Err, if it's expected part of #245
1 parent 71c7f30 commit 6efaf27

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

coffee_core/src/coffee.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ impl CoffeeManager {
169169
Ok(())
170170
}
171171

172+
pub async fn stop_plugin(&self, path: &str) -> Result<(), CoffeeError> {
173+
let mut payload = json_utils::init_payload();
174+
json_utils::add_str(&mut payload, "subcommand", "stop");
175+
json_utils::add_str(&mut payload, "plugin", path);
176+
let response = self
177+
.cln::<serde_json::Value, serde_json::Value>("plugin", payload)
178+
.await?;
179+
log::debug!("plugin stopped: {response}");
180+
Ok(())
181+
}
182+
172183
pub fn storage_info(&self) -> CoffeeStorageInfo {
173184
CoffeeStorageInfo::from(self)
174185
}
@@ -300,6 +311,8 @@ impl PluginManager for CoffeeManager {
300311
self.flush().await?;
301312
self.update_conf().await?;
302313
} else {
314+
self.config.plugins.push(plugin);
315+
self.flush().await?;
303316
self.start_plugin(&path).await?;
304317
}
305318
return Ok(());
@@ -328,9 +341,19 @@ impl PluginManager for CoffeeManager {
328341
log::debug!("runnable plugin path: {exec_path}");
329342
plugins.remove(index);
330343
log::debug!("coffee cln config: {}", self.coffee_cln_config);
331-
self.coffee_cln_config
332-
.rm_conf("plugin", Some(&exec_path.to_owned()))
333-
.map_err(|err| error!("{}", &err.cause))?;
344+
let remove_config = self
345+
.coffee_cln_config
346+
.rm_conf("plugin", Some(&exec_path.to_owned()));
347+
if let Err(err) = remove_config {
348+
// if this is true, we are probably a dynamic plugin:
349+
if err.cause.contains("field with `plugin` not present") {
350+
if let Err(e) = self.stop_plugin(&exec_path).await {
351+
log::warn!("{}", e);
352+
};
353+
} else {
354+
return Err(error!("{}", &err.cause));
355+
}
356+
}
334357
self.flush().await?;
335358
self.update_conf().await?;
336359
Ok(CoffeeRemove { plugin })

0 commit comments

Comments
 (0)