Fix deadlock
This commit is contained in:
+51
-20
@@ -71,11 +71,11 @@ impl<S: PulseWire, R: PulseWire> Plugin<S, R> {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct StrategyPair {
|
pub struct StrategyPair {
|
||||||
pub strategy: Plugin<StrategyEngineMessage, StrategyMessage>,
|
pub strategy: Mutex<Plugin<StrategyEngineMessage, StrategyMessage>>,
|
||||||
pub risk: Plugin<RiskEngineMessage, RiskMessage>,
|
pub risk: Mutex<Plugin<RiskEngineMessage, RiskMessage>>,
|
||||||
|
|
||||||
pub strategy_manifest: StrategyManifest,
|
pub strategy_manifest: Mutex<StrategyManifest>,
|
||||||
pub risk_manifest: RiskManifest,
|
pub risk_manifest: Mutex<RiskManifest>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StrategyPair {
|
impl StrategyPair {
|
||||||
@@ -84,32 +84,35 @@ impl StrategyPair {
|
|||||||
let risk = pulse_plugin(risk_id)?;
|
let risk = pulse_plugin(risk_id)?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
strategy: Plugin::new(
|
strategy: Mutex::new(Plugin::new(
|
||||||
Command::new("bash")
|
Command::new("bash")
|
||||||
.arg(strategy.join("strategy.bash"))
|
.arg(strategy.join("strategy.bash"))
|
||||||
.current_dir(&strategy)
|
.current_dir(&strategy)
|
||||||
.spawn()?,
|
.spawn()?,
|
||||||
),
|
)),
|
||||||
risk: Plugin::new(
|
risk: Mutex::new(Plugin::new(
|
||||||
Command::new("bash")
|
Command::new("bash")
|
||||||
.arg(strategy.join("risk.bash"))
|
.arg(strategy.join("risk.bash"))
|
||||||
.current_dir(&strategy)
|
.current_dir(&strategy)
|
||||||
.spawn()?,
|
.spawn()?,
|
||||||
),
|
)),
|
||||||
strategy_manifest: toml::from_slice(&fs::read(strategy.join("strategy.toml")).await?)
|
strategy_manifest: Mutex::new(
|
||||||
.map_err(|v| {
|
toml::from_slice(&fs::read(strategy.join("strategy.toml")).await?).map_err(
|
||||||
tokio::io::Error::new(std::io::ErrorKind::InvalidInput, v.to_string())
|
|
||||||
})?,
|
|
||||||
risk_manifest: toml::from_slice(&fs::read(risk.join("risk.toml")).await?).map_err(
|
|
||||||
|v| tokio::io::Error::new(std::io::ErrorKind::InvalidInput, v.to_string()),
|
|v| tokio::io::Error::new(std::io::ErrorKind::InvalidInput, v.to_string()),
|
||||||
)?,
|
)?,
|
||||||
|
),
|
||||||
|
risk_manifest: Mutex::new(
|
||||||
|
toml::from_slice(&fs::read(risk.join("risk.toml")).await?).map_err(|v| {
|
||||||
|
tokio::io::Error::new(std::io::ErrorKind::InvalidInput, v.to_string())
|
||||||
|
})?,
|
||||||
|
),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct StrategyEngine {
|
pub struct StrategyEngine {
|
||||||
pub pair: Mutex<StrategyPair>,
|
pub pair: StrategyPair,
|
||||||
|
|
||||||
pub strategy_handle: Mutex<Option<JoinHandle<tokio::io::Result<()>>>>,
|
pub strategy_handle: Mutex<Option<JoinHandle<tokio::io::Result<()>>>>,
|
||||||
pub risk_handle: Mutex<Option<JoinHandle<tokio::io::Result<()>>>>,
|
pub risk_handle: Mutex<Option<JoinHandle<tokio::io::Result<()>>>>,
|
||||||
@@ -120,7 +123,7 @@ pub struct StrategyEngine {
|
|||||||
impl StrategyEngine {
|
impl StrategyEngine {
|
||||||
pub async fn new(strategy_id: &str, risk_id: &str) -> tokio::io::Result<Self> {
|
pub async fn new(strategy_id: &str, risk_id: &str) -> tokio::io::Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
pair: Mutex::new(StrategyPair::new(strategy_id, risk_id).await?),
|
pair: StrategyPair::new(strategy_id, risk_id).await?,
|
||||||
strategy_handle: Mutex::new(None),
|
strategy_handle: Mutex::new(None),
|
||||||
risk_handle: Mutex::new(None),
|
risk_handle: Mutex::new(None),
|
||||||
engine: Weak::new(),
|
engine: Weak::new(),
|
||||||
@@ -166,7 +169,12 @@ impl StrategyEngine {
|
|||||||
*self.risk_handle.lock().await = Some(tokio::spawn(async move { engine.run_risk().await }));
|
*self.risk_handle.lock().await = Some(tokio::spawn(async move { engine.run_risk().await }));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn reload(&self, strategy_id: &str, risk_id: &str) -> tokio::io::Result<()> {
|
pub async fn reload(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
strategy_id: &str,
|
||||||
|
risk_id: &str,
|
||||||
|
) -> tokio::io::Result<()> {
|
||||||
|
{
|
||||||
if let Some(handle) = &*self.strategy_handle.lock().await {
|
if let Some(handle) = &*self.strategy_handle.lock().await {
|
||||||
handle.abort();
|
handle.abort();
|
||||||
}
|
}
|
||||||
@@ -175,12 +183,35 @@ impl StrategyEngine {
|
|||||||
handle.abort();
|
handle.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut pair = self.pair.lock().await;
|
self.pair.strategy.lock().await.process.kill().await?;
|
||||||
|
self.pair.risk.lock().await.process.kill().await?;
|
||||||
|
}
|
||||||
|
|
||||||
pair.strategy.process.kill().await?;
|
{
|
||||||
pair.risk.process.kill().await?;
|
let strategy = StrategyPair::new(strategy_id, risk_id).await?;
|
||||||
|
|
||||||
*pair = StrategyPair::new(strategy_id, risk_id).await?;
|
std::mem::swap(
|
||||||
|
&mut *self.pair.strategy.lock().await,
|
||||||
|
&mut *strategy.strategy.lock().await,
|
||||||
|
);
|
||||||
|
|
||||||
|
std::mem::swap(
|
||||||
|
&mut *self.pair.strategy_manifest.lock().await,
|
||||||
|
&mut *strategy.strategy_manifest.lock().await,
|
||||||
|
);
|
||||||
|
|
||||||
|
std::mem::swap(
|
||||||
|
&mut *self.pair.risk.lock().await,
|
||||||
|
&mut *strategy.risk.lock().await,
|
||||||
|
);
|
||||||
|
|
||||||
|
std::mem::swap(
|
||||||
|
&mut *self.pair.risk_manifest.lock().await,
|
||||||
|
&mut *strategy.risk_manifest.lock().await,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.spawn().await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user