2024-05-11 22:47:29 +02:00
|
|
|
use std::path::PathBuf;
|
2024-03-16 03:29:06 +01:00
|
|
|
use clap::{Parser, Subcommand};
|
2024-04-30 16:48:28 +02:00
|
|
|
use sea_orm::{ConnectOptions, Database};
|
2024-06-10 04:13:15 +02:00
|
|
|
use signal_hook::consts::signal::*;
|
|
|
|
use signal_hook_tokio::Signals;
|
|
|
|
use futures::stream::StreamExt;
|
2024-03-16 03:29:06 +01:00
|
|
|
|
2024-06-10 04:13:15 +02:00
|
|
|
use upub::ext::LoggableError;
|
2024-05-31 04:07:39 +02:00
|
|
|
#[cfg(feature = "cli")]
|
|
|
|
use upub_cli as cli;
|
|
|
|
|
|
|
|
#[cfg(feature = "migrate")]
|
|
|
|
use upub_migrations as migrations;
|
|
|
|
|
|
|
|
#[cfg(feature = "serve")]
|
|
|
|
use upub_routes as routes;
|
2024-03-28 04:52:17 +01:00
|
|
|
|
2024-06-06 02:21:36 +02:00
|
|
|
#[cfg(feature = "worker")]
|
|
|
|
use upub_worker as worker;
|
|
|
|
|
2024-03-25 05:02:20 +01:00
|
|
|
|
2024-03-16 03:29:06 +01:00
|
|
|
#[derive(Parser)]
|
|
|
|
/// all names were taken
|
2024-05-06 01:09:14 +02:00
|
|
|
struct Args {
|
2024-03-16 03:29:06 +01:00
|
|
|
#[clap(subcommand)]
|
|
|
|
/// command to run
|
2024-05-06 01:09:14 +02:00
|
|
|
command: Mode,
|
2024-03-16 03:29:06 +01:00
|
|
|
|
2024-05-11 22:47:29 +02:00
|
|
|
/// path to config file, leave empty to not use any
|
|
|
|
#[arg(short, long)]
|
|
|
|
config: Option<PathBuf>,
|
|
|
|
|
|
|
|
#[arg(long = "db")]
|
|
|
|
/// database connection uri, overrides config value
|
|
|
|
database: Option<String>,
|
2024-03-16 03:29:06 +01:00
|
|
|
|
2024-05-11 22:47:29 +02:00
|
|
|
#[arg(long)]
|
|
|
|
/// instance base domain, for AP ids, overrides config value
|
|
|
|
domain: Option<String>,
|
2024-03-20 08:56:35 +01:00
|
|
|
|
2024-03-16 03:29:06 +01:00
|
|
|
#[arg(long, default_value_t=false)]
|
|
|
|
/// run with debug level tracing
|
|
|
|
debug: bool,
|
2024-06-14 15:51:55 +02:00
|
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
/// force set number of worker threads for async runtime, defaults to number of cores
|
|
|
|
threads: Option<usize>,
|
2024-03-16 03:29:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Subcommand)]
|
2024-05-06 01:09:14 +02:00
|
|
|
enum Mode {
|
2024-05-11 22:47:29 +02:00
|
|
|
/// print current or default configuration
|
|
|
|
Config,
|
2024-03-16 03:29:06 +01:00
|
|
|
|
2024-05-31 04:07:39 +02:00
|
|
|
#[cfg(feature = "migrate")]
|
2024-03-16 03:29:06 +01:00
|
|
|
/// apply database migrations
|
|
|
|
Migrate,
|
2024-03-16 05:46:14 +01:00
|
|
|
|
2024-05-06 01:09:14 +02:00
|
|
|
#[cfg(feature = "cli")]
|
|
|
|
/// run maintenance CLI tasks
|
|
|
|
Cli {
|
|
|
|
#[clap(subcommand)]
|
|
|
|
/// task to run
|
|
|
|
command: cli::CliCommand,
|
2024-04-29 20:53:19 +02:00
|
|
|
},
|
2024-05-31 04:07:39 +02:00
|
|
|
|
2024-06-06 02:21:36 +02:00
|
|
|
#[cfg(all(feature = "serve", feature = "worker"))]
|
|
|
|
/// start both api routes and background workers
|
|
|
|
Monolith {
|
|
|
|
#[arg(short, long, default_value="127.0.0.1:3000")]
|
|
|
|
/// addr to bind and serve onto
|
|
|
|
bind: String,
|
|
|
|
|
|
|
|
#[arg(short, long, default_value_t = 4)]
|
|
|
|
/// how many concurrent jobs to process with this worker
|
|
|
|
tasks: usize,
|
|
|
|
|
|
|
|
#[arg(short, long, default_value_t = 20)]
|
|
|
|
/// interval for polling new tasks
|
|
|
|
poll: u64,
|
|
|
|
},
|
|
|
|
|
2024-05-31 04:07:39 +02:00
|
|
|
#[cfg(feature = "serve")]
|
2024-06-06 02:21:36 +02:00
|
|
|
/// start api routes server
|
2024-05-31 04:07:39 +02:00
|
|
|
Serve {
|
|
|
|
#[arg(short, long, default_value="127.0.0.1:3000")]
|
|
|
|
/// addr to bind and serve onto
|
|
|
|
bind: String,
|
|
|
|
},
|
2024-06-06 02:21:36 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "worker")]
|
|
|
|
/// start background job worker
|
|
|
|
Work {
|
|
|
|
/// only run tasks of this type, run all if not given
|
|
|
|
filter: Filter,
|
|
|
|
|
|
|
|
/// how many concurrent jobs to process with this worker
|
|
|
|
#[arg(short, long, default_value_t = 4)]
|
|
|
|
tasks: usize,
|
|
|
|
|
|
|
|
#[arg(short, long, default_value_t = 20)]
|
|
|
|
/// interval for polling new tasks
|
|
|
|
poll: u64,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-14 15:51:55 +02:00
|
|
|
fn main() {
|
2024-05-06 01:09:14 +02:00
|
|
|
let args = Args::parse();
|
2024-03-16 03:29:06 +01:00
|
|
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.compact()
|
|
|
|
.with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO })
|
|
|
|
.init();
|
|
|
|
|
2024-06-14 15:51:55 +02:00
|
|
|
let config = upub::Config::load(args.config.as_ref());
|
2024-05-11 22:47:29 +02:00
|
|
|
|
2024-06-06 05:37:27 +02:00
|
|
|
if matches!(args.command, Mode::Config) {
|
|
|
|
println!("{}", toml::to_string_pretty(&config).expect("failed serializing config"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-14 15:51:55 +02:00
|
|
|
let mut runtime = tokio::runtime::Builder::new_multi_thread();
|
|
|
|
|
|
|
|
if let Some(threads) = args.threads {
|
|
|
|
runtime.worker_threads(threads);
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime
|
|
|
|
.enable_io()
|
|
|
|
.enable_time()
|
2024-06-21 04:07:35 +02:00
|
|
|
.thread_name("upub-worker")
|
2024-06-14 15:51:55 +02:00
|
|
|
.build()
|
|
|
|
.expect("failed creating tokio async runtime")
|
|
|
|
.block_on(async { init(args, config).await })
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn init(args: Args, config: upub::Config) {
|
2024-05-11 22:47:29 +02:00
|
|
|
let database = args.database.unwrap_or(config.datasource.connection_string.clone());
|
|
|
|
let domain = args.domain.unwrap_or(config.instance.domain.clone());
|
|
|
|
|
2024-03-22 17:54:49 +01:00
|
|
|
// TODO can i do connectoptions.into() or .connect() and skip these ugly bindings?
|
2024-05-11 22:47:29 +02:00
|
|
|
let mut opts = ConnectOptions::new(&database);
|
2024-03-22 17:54:49 +01:00
|
|
|
|
2024-03-16 05:45:58 +01:00
|
|
|
opts
|
2024-05-31 01:44:29 +02:00
|
|
|
.sqlx_logging(true)
|
2024-05-11 22:47:29 +02:00
|
|
|
.sqlx_logging_level(tracing::log::LevelFilter::Debug)
|
|
|
|
.max_connections(config.datasource.max_connections)
|
|
|
|
.min_connections(config.datasource.min_connections)
|
|
|
|
.acquire_timeout(std::time::Duration::from_secs(config.datasource.acquire_timeout_seconds))
|
|
|
|
.connect_timeout(std::time::Duration::from_secs(config.datasource.connect_timeout_seconds))
|
|
|
|
.sqlx_slow_statements_logging_settings(
|
2024-05-31 01:34:55 +02:00
|
|
|
if config.datasource.slow_query_warn_enable { tracing::log::LevelFilter::Warn } else { tracing::log::LevelFilter::Debug },
|
2024-05-11 22:47:29 +02:00
|
|
|
std::time::Duration::from_secs(config.datasource.slow_query_warn_seconds)
|
|
|
|
);
|
2024-03-16 05:45:58 +01:00
|
|
|
|
|
|
|
let db = Database::connect(opts)
|
2024-03-16 03:29:06 +01:00
|
|
|
.await.expect("error connecting to db");
|
|
|
|
|
2024-05-31 04:07:39 +02:00
|
|
|
#[cfg(feature = "migrate")]
|
2024-06-06 05:37:27 +02:00
|
|
|
if matches!(args.command, Mode::Migrate) {
|
|
|
|
use migrations::MigratorTrait;
|
2024-05-31 04:07:39 +02:00
|
|
|
|
2024-06-06 05:37:27 +02:00
|
|
|
migrations::Migrator::up(&db, None)
|
|
|
|
.await
|
|
|
|
.expect("error applying migrations");
|
2024-05-31 04:07:39 +02:00
|
|
|
|
2024-06-06 05:37:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-17 22:29:54 +02:00
|
|
|
|
|
|
|
let ctx = upub::Context::new(db, domain, config.clone())
|
|
|
|
.await.expect("failed creating server context");
|
|
|
|
|
|
|
|
#[cfg(feature = "cli")]
|
|
|
|
if let Mode::Cli { command } = args.command {
|
|
|
|
cli::run(ctx, command)
|
|
|
|
.await.expect("failed running cli task");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// register signal handler only for long-lasting modes, such as server or worker
|
2024-06-10 04:13:15 +02:00
|
|
|
let (tx, rx) = tokio::sync::watch::channel(false);
|
2024-06-14 15:51:55 +02:00
|
|
|
let signals = Signals::new([SIGTERM, SIGINT]).expect("failed registering signal handler");
|
2024-06-10 04:13:15 +02:00
|
|
|
let handle = signals.handle();
|
|
|
|
let signals_task = tokio::spawn(handle_signals(signals, tx));
|
|
|
|
let stop = CancellationToken(rx);
|
|
|
|
|
2024-06-06 05:37:27 +02:00
|
|
|
match args.command {
|
2024-05-31 04:07:39 +02:00
|
|
|
#[cfg(feature = "serve")]
|
|
|
|
Mode::Serve { bind } =>
|
2024-06-10 04:13:15 +02:00
|
|
|
routes::serve(ctx, bind, stop)
|
2024-05-31 04:07:39 +02:00
|
|
|
.await.expect("failed serving api routes"),
|
2024-06-06 02:21:36 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "worker")]
|
|
|
|
Mode::Work { filter, tasks, poll } =>
|
2024-06-10 04:13:15 +02:00
|
|
|
worker::spawn(ctx, tasks, poll, filter.into(), stop)
|
2024-06-06 02:21:36 +02:00
|
|
|
.await.expect("failed running worker"),
|
|
|
|
|
|
|
|
#[cfg(all(feature = "serve", feature = "worker"))]
|
|
|
|
Mode::Monolith { bind, tasks, poll } => {
|
2024-06-10 04:13:15 +02:00
|
|
|
worker::spawn(ctx.clone(), tasks, poll, None, stop.clone());
|
2024-06-06 02:21:36 +02:00
|
|
|
|
2024-06-10 04:13:15 +02:00
|
|
|
routes::serve(ctx, bind, stop)
|
2024-06-06 02:21:36 +02:00
|
|
|
.await.expect("failed serving api routes");
|
|
|
|
},
|
2024-06-06 05:37:27 +02:00
|
|
|
|
2024-06-10 04:13:15 +02:00
|
|
|
Mode::Config => unreachable!(),
|
|
|
|
#[cfg(feature = "migrate")]
|
|
|
|
Mode::Migrate => unreachable!(),
|
2024-07-17 22:29:54 +02:00
|
|
|
#[cfg(feature = "cli")]
|
|
|
|
Mode::Cli { .. } => unreachable!(),
|
2024-06-06 02:21:36 +02:00
|
|
|
}
|
2024-06-10 04:13:15 +02:00
|
|
|
|
|
|
|
handle.close();
|
|
|
|
signals_task.await.expect("failed joining signal handler task");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct CancellationToken(tokio::sync::watch::Receiver<bool>);
|
|
|
|
|
|
|
|
impl worker::StopToken for CancellationToken {
|
|
|
|
fn stop(&self) -> bool {
|
|
|
|
*self.0.borrow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sea_orm::prelude::async_trait::async_trait] // ahahaha we avoid this???
|
|
|
|
impl routes::ShutdownToken for CancellationToken {
|
|
|
|
async fn event(mut self) {
|
|
|
|
self.0.changed().await.warn_failed("cancellation token channel closed, stopping...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_signals(
|
|
|
|
mut signals: signal_hook_tokio::Signals,
|
|
|
|
tx: tokio::sync::watch::Sender<bool>,
|
|
|
|
) {
|
|
|
|
while let Some(signal) = signals.next().await {
|
|
|
|
match signal {
|
|
|
|
SIGTERM | SIGINT => {
|
|
|
|
tracing::info!("received stop signal, closing tasks");
|
|
|
|
tx.send(true).info_failed("error sending stop signal to tasks")
|
|
|
|
},
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, clap::ValueEnum)]
|
|
|
|
enum Filter {
|
|
|
|
All,
|
|
|
|
Delivery,
|
|
|
|
Inbound,
|
|
|
|
Outbound,
|
2024-06-06 02:21:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Filter> for Option<upub::model::job::JobType> {
|
|
|
|
fn from(value: Filter) -> Self {
|
|
|
|
match value {
|
|
|
|
Filter::All => None,
|
2024-06-06 19:04:08 +02:00
|
|
|
Filter::Delivery => Some(upub::model::job::JobType::Delivery),
|
2024-06-06 02:21:36 +02:00
|
|
|
Filter::Inbound => Some(upub::model::job::JobType::Inbound),
|
|
|
|
Filter::Outbound => Some(upub::model::job::JobType::Outbound),
|
|
|
|
}
|
2024-03-15 19:43:29 +01:00
|
|
|
}
|
|
|
|
}
|