Skip to content

Commit 8942037

Browse files
committed
main runtime in text context or main only
1 parent 8ca6fc3 commit 8942037

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed

src/bin/cratesfyi.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
use std::env;
2-
use std::fmt::Write;
3-
use std::net::SocketAddr;
4-
use std::path::PathBuf;
5-
use std::str::FromStr;
6-
use std::sync::Arc;
7-
81
use anyhow::{Context as _, Result, anyhow};
92
use clap::{Parser, Subcommand, ValueEnum};
10-
use docs_rs::db::{self, CrateId, Overrides, add_path_into_database};
11-
use docs_rs::utils::{
12-
ConfigName, get_config, get_crate_pattern_and_priority, list_crate_priorities, queue_builder,
13-
remove_crate_priority, set_config, set_crate_priority,
14-
};
153
use docs_rs::{
16-
Config, Context, PackageKind, RustwideBuilder, start_background_metrics_webserver,
17-
start_web_server,
4+
Config, Context, PackageKind, RustwideBuilder,
5+
db::{self, CrateId, Overrides, add_path_into_database},
6+
start_background_metrics_webserver, start_web_server,
7+
utils::{
8+
ConfigName, get_config, get_crate_pattern_and_priority, list_crate_priorities,
9+
queue_builder, remove_crate_priority, set_config, set_crate_priority,
10+
},
1811
};
1912
use futures_util::StreamExt;
2013
use sentry::{
2114
TransactionContext, integrations::panic as sentry_panic,
2215
integrations::tracing as sentry_tracing,
2316
};
17+
use std::{env, fmt::Write, net::SocketAddr, path::PathBuf, str::FromStr, sync::Arc};
18+
use tokio::runtime;
2419
use tracing_log::LogTracer;
2520
use tracing_subscriber::{EnvFilter, filter::Directive, prelude::*};
2621

@@ -183,7 +178,13 @@ enum CommandLine {
183178
impl CommandLine {
184179
fn handle_args(self) -> Result<()> {
185180
let config = Config::from_env()?.build()?;
186-
let ctx = Context::from_config(config)?;
181+
let runtime = Arc::new(
182+
runtime::Builder::new_multi_thread()
183+
.enable_all()
184+
.build()
185+
.expect("failed to initialize runtime"),
186+
);
187+
let ctx = runtime.block_on(Context::from_config(config))?;
187188

188189
match self {
189190
Self::Build { subcommand } => subcommand.handle_args(ctx)?,

src/context.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,10 @@ pub struct Context {
2323
pub registry_api: Arc<RegistryApi>,
2424
pub repository_stats_updater: Arc<RepositoryStatsUpdater>,
2525
pub runtime: runtime::Handle,
26-
pub owned_runtime: Option<Arc<runtime::Runtime>>,
2726
}
2827

2928
impl Context {
30-
pub fn from_config(config: Config) -> Result<Self> {
31-
let runtime = Arc::new(
32-
runtime::Builder::new_multi_thread()
33-
.enable_all()
34-
.build()
35-
.expect("failed to initialize runtime"),
36-
);
37-
let mut ctx = runtime.block_on(Self::from_config_async(config))?;
38-
ctx.owned_runtime = Some(runtime);
39-
Ok(ctx)
40-
}
41-
42-
pub async fn from_config_async(config: Config) -> Result<Self> {
29+
pub async fn from_config(config: Config) -> Result<Self> {
4330
let config = Arc::new(config);
4431

4532
let instance_metrics = Arc::new(InstanceMetrics::new()?);
@@ -90,7 +77,6 @@ impl Context {
9077
repository_stats_updater: Arc::new(RepositoryStatsUpdater::new(&config, pool)),
9178
runtime,
9279
config,
93-
owned_runtime: None,
9480
})
9581
}
9682
}

src/test/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ pub(crate) struct TestEnvironment {
311311
// because the tokio runtime from the context is gone.
312312
db: TestDatabase,
313313
pub context: Context,
314+
owned_runtime: Option<Arc<runtime::Runtime>>,
314315
}
315316

316317
pub(crate) fn init_logger() {
@@ -346,7 +347,7 @@ impl TestEnvironment {
346347
.expect("failed to initialize runtime"),
347348
);
348349
let mut env = runtime.block_on(Self::with_config_async(config));
349-
env.context.owned_runtime = Some(runtime);
350+
env.owned_runtime = Some(runtime);
350351
env
351352
}
352353

@@ -422,9 +423,9 @@ impl TestEnvironment {
422423
pool.clone(),
423424
)),
424425
runtime,
425-
owned_runtime: None,
426426
},
427427
db: test_db,
428+
owned_runtime: None,
428429
}
429430
}
430431

0 commit comments

Comments
 (0)