mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-04-20 16:06:00 -04:00
feat: Kill monero-wallet-rpc on GUI exit
This commit is contained in:
parent
acdb0231b5
commit
718132b8b4
@ -11,7 +11,7 @@ use swap::{
|
||||
},
|
||||
cli::command::{Bitcoin, Monero},
|
||||
};
|
||||
use tauri::{Manager, State};
|
||||
use tauri::{Manager, RunEvent, State};
|
||||
|
||||
trait ToStringResult<T> {
|
||||
fn to_string_result(self) -> Result<T, String>;
|
||||
@ -107,6 +107,16 @@ pub fn run() {
|
||||
withdraw_btc
|
||||
])
|
||||
.setup(setup)
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while building tauri application")
|
||||
.run(|app, event| match event {
|
||||
RunEvent::Exit | RunEvent::ExitRequested { .. } => {
|
||||
let context = app.state::<Arc<Context>>().inner();
|
||||
|
||||
if let Err(err) = context.cleanup() {
|
||||
println!("Cleanup failed {}", err);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
}
|
||||
|
@ -7,14 +7,14 @@ use crate::network::rendezvous::XmrBtcNamespace;
|
||||
use crate::protocol::Database;
|
||||
use crate::seed::Seed;
|
||||
use crate::{bitcoin, cli, monero};
|
||||
use anyhow::{bail, Context as AnyContext, Error, Result};
|
||||
use anyhow::{anyhow, bail, Context as AnyContext, Error, Result};
|
||||
use futures::future::try_join_all;
|
||||
use std::fmt;
|
||||
use std::future::Future;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Once};
|
||||
use tokio::sync::{broadcast, broadcast::Sender, Mutex, RwLock};
|
||||
use std::sync::{Arc, Mutex, Once};
|
||||
use tokio::sync::{broadcast, broadcast::Sender, Mutex as TokioMutex, RwLock};
|
||||
use tokio::task::JoinHandle;
|
||||
use url::Url;
|
||||
|
||||
@ -36,7 +36,7 @@ pub struct Config {
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct PendingTaskList(Mutex<Vec<JoinHandle<()>>>);
|
||||
pub struct PendingTaskList(TokioMutex<Vec<JoinHandle<()>>>);
|
||||
|
||||
impl PendingTaskList {
|
||||
pub async fn spawn<F, T>(&self, future: F)
|
||||
@ -164,7 +164,7 @@ pub struct Context {
|
||||
pub db: Arc<dyn Database + Send + Sync>,
|
||||
bitcoin_wallet: Option<Arc<bitcoin::Wallet>>,
|
||||
monero_wallet: Option<Arc<monero::Wallet>>,
|
||||
monero_rpc_process: Option<Arc<monero::WalletRpcProcess>>,
|
||||
monero_rpc_process: Option<Arc<Mutex<monero::WalletRpcProcess>>>,
|
||||
pub swap_lock: Arc<SwapLock>,
|
||||
pub config: Config,
|
||||
pub tasks: Arc<PendingTaskList>,
|
||||
@ -228,7 +228,7 @@ impl Context {
|
||||
db: open_db(data_dir.join("sqlite")).await?,
|
||||
bitcoin_wallet,
|
||||
monero_wallet,
|
||||
monero_rpc_process: monero_rpc_process.map(Arc::new),
|
||||
monero_rpc_process: monero_rpc_process.map(|prc| Arc::new(Mutex::new(prc))),
|
||||
config: Config {
|
||||
tor_socks5_port,
|
||||
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
|
||||
@ -268,6 +268,19 @@ impl Context {
|
||||
tasks: Arc::new(PendingTaskList::default()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cleanup(&self) -> Result<()> {
|
||||
if let Some(ref monero_rpc_process) = self.monero_rpc_process {
|
||||
let mut process = monero_rpc_process
|
||||
.lock()
|
||||
.map_err(|_| anyhow!("Failed to lock monero_rpc_process for cleanup"))?;
|
||||
|
||||
process.kill()?;
|
||||
println!("Killed monero-wallet-rpc process");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Context {
|
||||
|
@ -8,12 +8,12 @@ use reqwest::header::CONTENT_LENGTH;
|
||||
use reqwest::Url;
|
||||
use serde::Deserialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fmt;
|
||||
use std::fmt::{Debug, Display, Formatter};
|
||||
use std::io::ErrorKind;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Stdio;
|
||||
use std::time::Duration;
|
||||
use std::{fmt, io};
|
||||
use tokio::fs::{remove_file, OpenOptions};
|
||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::process::{Child, Command};
|
||||
@ -178,6 +178,10 @@ impl WalletRpcProcess {
|
||||
Url::parse(&format!("http://127.0.0.1:{}/json_rpc", self.port))
|
||||
.expect("Static url template is always valid")
|
||||
}
|
||||
|
||||
pub fn kill(&mut self) -> io::Result<()> {
|
||||
self._child.start_kill()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WalletRpc {
|
||||
|
Loading…
x
Reference in New Issue
Block a user