Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
farmer restart mechanism added (#217)
Browse files Browse the repository at this point in the history
farmer restart mechanism added
  • Loading branch information
ozgunozerk committed May 20, 2022
1 parent 9b49842 commit 0852dc8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src-tauri/src/farmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::Arc;
use subspace_core_primitives::{PublicKey, PIECE_SIZE};
use subspace_farmer::multi_farming::{MultiFarming, Options as MultiFarmingOptions};
use subspace_farmer::{Identity, NodeRpcClient, ObjectMappings, Plot, RpcClient};
use tokio::sync::mpsc::Sender;

pub(crate) static PLOTTED_PIECES: AtomicUsize = AtomicUsize::new(0);

Expand All @@ -16,6 +17,7 @@ pub(crate) async fn farm(
node_rpc_url: &str,
reward_address: Option<PublicKey>,
plot_size: u64,
error_sender: Sender<()>,
) -> Result<(), anyhow::Error> {
raise_fd_limit();
let reward_address = if let Some(reward_address) = reward_address {
Expand Down Expand Up @@ -74,7 +76,16 @@ pub(crate) async fn farm(
}))
.detach();

tokio::spawn(async { multi_farming.wait().await });
tokio::spawn(async move {
let result = multi_farming.wait().await;
match result {
Err(error) => {
log::error!("{error}");
error_sender.send(()).await.unwrap() // this send should always be successful
}
Ok(_) => unreachable!("wait function should not return Ok()"),
}
});

Ok(())
}
Expand Down
40 changes: 37 additions & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use tauri::{
Env, Manager, RunEvent, WindowEvent,
};
use tauri_plugin_log::{LogTarget, LoggerBuilder};
use tokio::sync::mpsc;
use tokio::sync::mpsc::{Receiver, Sender};

#[derive(Serialize)]
struct DiskStats {
Expand All @@ -45,10 +47,42 @@ fn plot_progress_tracker() -> usize {

#[tauri::command]
async fn farming(path: String, reward_address: String, plot_size: u64) -> bool {
// create a channel to listen for farmer errors, and restart another farmer instance in case any error
let (error_sender, mut error_receiver): (Sender<()>, Receiver<()>) = mpsc::channel(1);

if let Ok(address) = farmer::parse_reward_address(&reward_address) {
farmer::farm(path.into(), "ws://127.0.0.1:9944", Some(address), plot_size)
.await
.unwrap();
farmer::farm(
path.clone().into(),
"ws://127.0.0.1:9944",
Some(address),
plot_size,
error_sender.clone(),
)
.await
.unwrap();

// farmer started successfully, now listen in the background for errors
tokio::spawn(async move {
// if there is an error, restart another farmer, and start listening again, in a loop
loop {
let result = error_receiver.recv().await;
match result {
// we have received an error, let's restart the farmer
Some(_) => farmer::farm(
path.clone().into(),
"ws://127.0.0.1:9944",
Some(address),
plot_size,
error_sender.clone(),
)
.await
.unwrap(),
None => unreachable!(
"sender should not have been dropped before sending an error message"
),
}
}
});
true
} else {
// reward address could not be parsed, and farmer did not start
Expand Down

0 comments on commit 0852dc8

Please sign in to comment.