Strategy engine
This commit is contained in:
@@ -7,10 +7,13 @@ use pulse_wire::{
|
||||
StrategyMessage,
|
||||
},
|
||||
};
|
||||
|
||||
use tokio::{
|
||||
fs,
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
process::Command,
|
||||
sync::Mutex,
|
||||
task::JoinHandle,
|
||||
};
|
||||
|
||||
use crate::store::pulse_plugin;
|
||||
@@ -100,3 +103,40 @@ impl StrategyPair {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct StrategyEngine {
|
||||
pub pair: Mutex<StrategyPair>,
|
||||
|
||||
pub strategy_handle: Mutex<Option<JoinHandle<()>>>,
|
||||
pub risk_handle: Mutex<Option<JoinHandle<()>>>,
|
||||
}
|
||||
|
||||
impl StrategyEngine {
|
||||
pub async fn new(strategy_id: &str, risk_id: &str) -> tokio::io::Result<Self> {
|
||||
Ok(Self {
|
||||
pair: Mutex::new(StrategyPair::new(strategy_id, risk_id).await?),
|
||||
strategy_handle: Mutex::new(None),
|
||||
risk_handle: Mutex::new(None),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn reload(&self, strategy_id: &str, risk_id: &str) -> tokio::io::Result<()> {
|
||||
if let Some(handle) = &*self.strategy_handle.lock().await {
|
||||
handle.abort();
|
||||
}
|
||||
|
||||
if let Some(handle) = &*self.risk_handle.lock().await {
|
||||
handle.abort();
|
||||
}
|
||||
|
||||
let mut pair = self.pair.lock().await;
|
||||
|
||||
pair.strategy.process.kill().await?;
|
||||
pair.risk.process.kill().await?;
|
||||
|
||||
*pair = StrategyPair::new(strategy_id, risk_id).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user