Skip to content

Commit

Permalink
feat(prover): dynamicly detect sentry endpoint from coordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
amoylan2 committed Sep 29, 2024
1 parent 229eef2 commit 43fba89
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 49 deletions.
114 changes: 66 additions & 48 deletions prover/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,62 @@ struct Args {
log_file: Option<String>,
}

fn start_loop<'a>(
config: &'a Config,
prover: &'a Prover<'a>,
task_cache: Rc<TaskCache>,
log_file: Option<String>,
current_dsn: Option<String>,
) -> Option<String> {
let mut new_dsn: Option<String> = None;
let should_stop = || {
new_dsn = prover.coordinator_client.borrow().get_sentry_dsn();

current_dsn != new_dsn
};

let _guard = current_dsn.clone().map(|dsn| {
log::info!("successfully get dsn from coordinator");
let gurad = sentry::init((
dsn,
sentry::ClientOptions {
release: Some(version::get_version_cow()),
environment: Some(utils::get_environment()),
..Default::default()
},
));
utils::set_logger_with_sentry(log_file);
gurad
});

_guard.iter().for_each(|_| {
sentry::configure_scope(|scope| {
scope.set_tag("prover_type", config.prover_type);
scope.set_tag("partner_name", config.partner_name());
scope.set_tag("prover_name", config.prover_name.clone());

let public_key = sentry::protocol::Value::from(prover.get_public_key());
scope.set_extra("public_key", public_key);
});

sentry::capture_message("test message on start", sentry::Level::Info);
});

log::info!(
"prover start successfully. name: {}, type: {:?}, publickey: {}, version: {}",
config.prover_name,
config.prover_type,
prover.get_public_key(),
version::get_version(),
);

let task_processor = TaskProcessor::new(prover, task_cache);

task_processor.start(should_stop);

new_dsn
}

fn start() -> Result<()> {
let args = Args::parse();

Expand All @@ -64,54 +120,16 @@ fn start() -> Result<()> {

let prover = Prover::new(&config, coordinator_listener)?;

let _guard = prover
.coordinator_client
.borrow()
.get_sentry_dsn()
.map(|dsn| {
log::info!("successfully get dsn from coordinator");
let gurad = Some(sentry::init((
dsn,
sentry::ClientOptions {
release: Some(version::get_version_cow()),
environment: Some(utils::get_environment()),
..Default::default()
},
)));
utils::set_logger_with_sentry(args.log_file);
gurad
});

_guard.iter().for_each(|_| {
sentry::configure_scope(|scope| {
scope.set_tag("prover_type", config.prover_type);
scope.set_tag("partner_name", config.partner_name());
scope.set_tag("prover_name", config.prover_name.clone());
});

sentry::capture_message("test message on start", sentry::Level::Info);
});

_guard.iter().for_each(|_| {
sentry::configure_scope(|scope| {
let public_key = sentry::protocol::Value::from(prover.get_public_key());
scope.set_extra("public_key", public_key);
});
});

log::info!(
"prover start successfully. name: {}, type: {:?}, publickey: {}, version: {}",
config.prover_name,
config.prover_type,
prover.get_public_key(),
version::get_version(),
);

let task_processor = TaskProcessor::new(&prover, task_cache);

task_processor.start();

Ok(())
let mut current_dsn = None;
loop {
current_dsn = start_loop(
&config,
&prover,
task_cache.clone(),
args.log_file.clone(),
current_dsn,
);
}
}

fn main() {
Expand Down
9 changes: 8 additions & 1 deletion prover/src/task_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ impl<'a> TaskProcessor<'a> {
TaskProcessor { prover, task_cache }
}

pub fn start(&self) {
pub fn start<F>(&self, mut should_stop: F)
where
F: FnMut() -> bool,
{
loop {
if should_stop() {
log::info!("task processor should stop.");
break;
}
log::info!("start a new round.");
if let Err(err) = self.prove_and_submit() {
if err.is::<GetEmptyTaskError>() {
Expand Down

0 comments on commit 43fba89

Please sign in to comment.