Allow multiple labrinth instances (#3360)

* Move a lot of scheduled tasks to be runnable from the command-line

* Use pubsub to handle sockets connected to multiple Labrinths

* Clippy fix

* Fix build and merge some stuff

* Fix build fmt
:

---------

Signed-off-by: Jai Agrawal <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Jai Agrawal <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
Josiah Glosson
2025-03-15 09:28:20 -05:00
committed by GitHub
parent 84a9438a70
commit c998d2566e
21 changed files with 1056 additions and 692 deletions

View File

@@ -1,5 +1,7 @@
use actix_web::{App, HttpServer};
use actix_web_prom::PrometheusMetricsBuilder;
use clap::Parser;
use labrinth::background_task::BackgroundTask;
use labrinth::database::redis::RedisPool;
use labrinth::file_hosting::S3Host;
use labrinth::search;
@@ -23,8 +25,23 @@ pub struct Pepper {
pub pepper: String,
}
#[derive(Parser)]
#[command(version)]
struct Args {
/// Don't run regularly scheduled background tasks. This means the tasks should be run
/// manually with --run-background-task.
#[arg(long)]
no_background_tasks: bool,
/// Run a single background task and then exit. Perfect for cron jobs.
#[arg(long, value_enum, id = "task")]
run_background_task: Option<BackgroundTask>,
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
let args = Args::parse();
dotenvy::dotenv().ok();
console_subscriber::init();
@@ -44,10 +61,12 @@ async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_BACKTRACE", "1");
}
info!(
"Starting Labrinth on {}",
dotenvy::var("BIND_ADDR").unwrap()
);
if args.run_background_task.is_none() {
info!(
"Starting Labrinth on {}",
dotenvy::var("BIND_ADDR").unwrap()
);
}
database::check_for_migrations()
.await
@@ -91,6 +110,18 @@ async fn main() -> std::io::Result<()> {
info!("Initializing clickhouse connection");
let mut clickhouse = clickhouse::init_client().await.unwrap();
let search_config = search::SearchConfig::new(None);
let stripe_client =
stripe::Client::new(dotenvy::var("STRIPE_API_KEY").unwrap());
if let Some(task) = args.run_background_task {
info!("Running task {task:?} and exiting");
task.run(pool, redis_pool, search_config, clickhouse, stripe_client)
.await;
return Ok(());
}
let maxmind_reader =
Arc::new(queue::maxmind::MaxMindIndexer::new().await.unwrap());
@@ -115,8 +146,6 @@ async fn main() -> std::io::Result<()> {
labrinth::routes::debug::jemalloc_mmeory_stats(&prometheus.registry)
.expect("Failed to register jemalloc metrics");
let search_config = search::SearchConfig::new(None);
let labrinth_config = labrinth::app_setup(
pool.clone(),
redis_pool.clone(),
@@ -124,6 +153,8 @@ async fn main() -> std::io::Result<()> {
&mut clickhouse,
file_host.clone(),
maxmind_reader.clone(),
stripe_client,
!args.no_background_tasks,
);
info!("Starting Actix HTTP server!");