Skip to content

Commit

Permalink
Extract execute_on_runtime() into td_util
Browse files Browse the repository at this point in the history
Summary: This is to unify all the ways we create Tokio runtime in TD. Until TD is properly async, it will be used to wrap async parts within still-sync parts.

Reviewed By: ndmitchell

Differential Revision: D61613945

fbshipit-source-id: a86e755b3a2671e0ec1eb639f83996893eb68702
  • Loading branch information
Fedir Panasenko authored and facebook-github-bot committed Aug 23, 2024
1 parent 9994b1a commit 8d24251
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
13 changes: 2 additions & 11 deletions supertd/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@

#![forbid(unsafe_code)]

use std::future::Future;

use clap::CommandFactory;
use clap::FromArgMatches;
use clap::Parser;
use fbinit::FacebookInit;
use td_util::cli::get_args;
use td_util::executor::run_as_sync;

/// Generic binary for the pieces of the new target-determinator framework.
#[allow(clippy::large_enum_variant)] // Only one instance, so not a big deal
Expand Down Expand Up @@ -62,7 +61,7 @@ pub fn main(fb: FacebookInit) -> anyhow::Result<()> {
#[cfg(fbcode_build)]
Args::VerifiableMatcher(args) => verifiable_matcher::main(args),
#[cfg(fbcode_build)]
Args::Ranker(args) => execute_on_runtime(ranker::main(args)),
Args::Ranker(args) => run_as_sync(ranker::main(args)),
#[cfg(fbcode_build)]
Args::Rerun(args) => rerun::main(fb, args),
#[cfg(fbcode_build)]
Expand All @@ -84,14 +83,6 @@ fn get_version() -> &'static str {
env!("CARGO_PKG_VERSION")
}

fn execute_on_runtime(f: impl Future<Output = anyhow::Result<()>>) -> anyhow::Result<()> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(f)
}

#[cfg(test)]
mod tests {
use clap::Command;
Expand Down
1 change: 1 addition & 0 deletions td_util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rayon = "1.6.1"
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0.66"
tempfile = "3.1.0"
tokio = { version = "1.37", features = ["full"] }
tracing = "0.1.22"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
zstd = "0.12.3"
20 changes: 20 additions & 0 deletions td_util/src/executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/

//! Adapter to bridge the gap between sync and async code.
use std::future::Future;

pub fn run_as_sync<F: Future>(future: F) -> F::Output {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(future)
}
1 change: 1 addition & 0 deletions td_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
pub mod cli;
pub mod command;
pub mod directives;
pub mod executor;
pub mod json;
pub mod knobs;
pub mod no_hash;
Expand Down

0 comments on commit 8d24251

Please sign in to comment.