-
-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: clean up parts of the codebase #981
Open
de-sh
wants to merge
15
commits into
parseablehq:main
Choose a base branch
from
de-sh:refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5945cc4
refactor: mod files
de-sh c33ff03
Merge remote-tracking branch 'origin/main' into refactor
de-sh 1f0ed35
refactor: DRY `ParseableServer::start()`
de-sh 8847eaa
Merge branch 'main' into refactor
de-sh d38b88c
doc: explain `None`
de-sh 4d3ef14
style: use box pointers
de-sh 1c70224
refactor: avoid unwrap
de-sh df1fdb5
refactor: drop `validate` from `ParseableServer` trait
de-sh 7a1aeda
doc: move comment
de-sh 474a5d5
refactor: merge init and initialize
de-sh 0a30994
refactor: separate out metadata loading stage
de-sh 2da676e
doc: improve error message
de-sh dd841ef
style: warning/suggestion
de-sh 3149383
refactor: `ObjectStorage: Send`
de-sh 608c0a2
Merge branch 'main' into refactor
de-sh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,31 +26,145 @@ pub mod utils; | |
|
||
use std::sync::Arc; | ||
|
||
use actix_web::middleware::from_fn; | ||
use actix_web::web::ServiceConfig; | ||
use actix_web::App; | ||
use actix_web::HttpServer; | ||
use actix_web_prometheus::PrometheusMetrics; | ||
use async_trait::async_trait; | ||
use openid::Discovered; | ||
|
||
use crate::oidc; | ||
use base64::Engine; | ||
use openid::Discovered; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use ssl_acceptor::get_ssl_acceptor; | ||
use tokio::sync::{oneshot, Mutex}; | ||
|
||
use super::cross_origin_config; | ||
use super::API_BASE_PATH; | ||
use super::API_VERSION; | ||
use crate::handlers::http::health_check; | ||
use crate::oidc; | ||
use crate::option::CONFIG; | ||
|
||
pub type OpenIdClient = Arc<openid::Client<Discovered, oidc::Claims>>; | ||
|
||
// to be decided on what the Default version should be | ||
pub const DEFAULT_VERSION: &str = "v3"; | ||
|
||
include!(concat!(env!("OUT_DIR"), "/generated.rs")); | ||
|
||
#[async_trait(?Send)] | ||
#[async_trait] | ||
pub trait ParseableServer { | ||
// async fn validate(&self) -> Result<(), ObjectStorageError>; | ||
/// configure the router | ||
fn configure_routes(config: &mut ServiceConfig, oidc_client: Option<OpenIdClient>) | ||
where | ||
Self: Sized; | ||
|
||
/// configure the server | ||
async fn start( | ||
&self, | ||
prometheus: PrometheusMetrics, | ||
oidc_client: Option<crate::oidc::OpenidConfig>, | ||
) -> anyhow::Result<()>; | ||
) -> anyhow::Result<()> | ||
where | ||
Self: Sized, | ||
{ | ||
let oidc_client = match oidc_client { | ||
Some(config) => { | ||
let client = config | ||
.connect(&format!("{API_BASE_PATH}/{API_VERSION}/o/code")) | ||
.await?; | ||
Some(Arc::new(client)) | ||
} | ||
|
||
None => None, | ||
}; | ||
|
||
// get the ssl stuff | ||
let ssl = get_ssl_acceptor( | ||
&CONFIG.parseable.tls_cert_path, | ||
&CONFIG.parseable.tls_key_path, | ||
&CONFIG.parseable.trusted_ca_certs_path, | ||
)?; | ||
|
||
// fn that creates the app | ||
let create_app_fn = move || { | ||
App::new() | ||
.wrap(prometheus.clone()) | ||
.configure(|config| Self::configure_routes(config, oidc_client.clone())) | ||
.wrap(from_fn(health_check::check_shutdown_middleware)) | ||
.wrap(actix_web::middleware::Logger::default()) | ||
.wrap(actix_web::middleware::Compress::default()) | ||
.wrap(cross_origin_config()) | ||
}; | ||
|
||
// Create a channel to trigger server shutdown | ||
let (shutdown_trigger, shutdown_rx) = oneshot::channel::<()>(); | ||
let server_shutdown_signal = Arc::new(Mutex::new(Some(shutdown_trigger))); | ||
|
||
// Clone the shutdown signal for the signal handler | ||
let shutdown_signal = server_shutdown_signal.clone(); | ||
|
||
// Spawn the signal handler task | ||
let signal_task = tokio::spawn(async move { | ||
health_check::handle_signals(shutdown_signal).await; | ||
println!("Received shutdown signal, notifying server to shut down..."); | ||
}); | ||
|
||
// Create the HTTP server | ||
let http_server = HttpServer::new(create_app_fn) | ||
.workers(num_cpus::get()) | ||
.shutdown_timeout(60); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what should be shutdown timeout? |
||
|
||
// Start the server with or without TLS | ||
let srv = if let Some(config) = ssl { | ||
http_server | ||
.bind_rustls_0_22(&CONFIG.parseable.address, config)? | ||
.run() | ||
} else { | ||
http_server.bind(&CONFIG.parseable.address)?.run() | ||
}; | ||
|
||
// Graceful shutdown handling | ||
let srv_handle = srv.handle(); | ||
|
||
let sync_task = tokio::spawn(async move { | ||
// Wait for the shutdown signal | ||
let _ = shutdown_rx.await; | ||
|
||
// Perform S3 sync and wait for completion | ||
log::info!("Starting data sync to S3..."); | ||
if let Err(e) = CONFIG.storage().get_object_store().sync(true).await { | ||
log::warn!("Failed to sync local data with object store. {:?}", e); | ||
} else { | ||
log::info!("Successfully synced all data to S3."); | ||
} | ||
|
||
// Initiate graceful shutdown | ||
log::info!("Graceful shutdown of HTTP server triggered"); | ||
srv_handle.stop(true).await; | ||
}); | ||
|
||
// Await the HTTP server to run | ||
let server_result = srv.await; | ||
|
||
// Await the signal handler to ensure proper cleanup | ||
if let Err(e) = signal_task.await { | ||
log::error!("Error in signal handler: {:?}", e); | ||
} | ||
|
||
// Wait for the sync task to complete before exiting | ||
if let Err(e) = sync_task.await { | ||
log::error!("Error in sync task: {:?}", e); | ||
} else { | ||
log::info!("Sync task completed successfully."); | ||
} | ||
|
||
// Return the result of the server | ||
server_result?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn init(&self) -> anyhow::Result<()>; | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
60 here