Skip to content

Commit

Permalink
feat: implement shuttle service
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Dec 21, 2024
1 parent 4012b00 commit ddab42e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 57 deletions.
34 changes: 15 additions & 19 deletions crates/chat-app-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ anyhow.workspace = true
plugin-chat = { workspace = true, features = ["server_only"] }
serde.workspace = true
shuttle-actix-web = { workspace = true, optional = true }
shuttle-runtime = { workspace = true, optional = true }
shuttle-shared-db = { workspace = true, optional = true, features = [
"postgres",
"sqlx",
] }
shuttle-runtime = { workspace = true }
shuttle-shared-db = { workspace = true, optional = true, features = ["postgres", "sqlx"] }
sqlx = { workspace = true, features = ["tls-rustls"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tracing.workspace = true
Expand All @@ -41,24 +38,23 @@ wykies-time = { workspace = true }
default = ["standalone", "mysql"]
disable-cors = ["wykies-server/disable-cors"]
standalone = [
# Runs the app in the standalone mode (also needs mysql or postgres)
"wykies-server/redis-session-rustls",
# Runs the app in the standalone mode (also needs mysql or postgres)
"wykies-server/redis-session-rustls",
]
shuttle = [
# Runs the app in the shuttle mode (Uses postgres and cookie's only for sessions)
"dep:shuttle-actix-web",
"dep:shuttle-runtime",
"dep:shuttle-shared-db",
"postgres",
"wykies-server/cookie-session",
# Runs the app in the shuttle mode (Uses postgres and cookie's only for sessions)
"dep:shuttle-actix-web",
"dep:shuttle-shared-db",
"postgres",
"wykies-server/cookie-session",
]
mysql = [
"plugin-chat/mysql",
"wykies-server-test-helper/mysql",
"wykies-server/mysql",
"plugin-chat/mysql",
"wykies-server-test-helper/mysql",
"wykies-server/mysql",
]
postgres = [
"plugin-chat/postgres",
"wykies-server-test-helper/postgres",
"wykies-server/postgres",
"plugin-chat/postgres",
"wykies-server-test-helper/postgres",
"wykies-server/postgres",
]
41 changes: 4 additions & 37 deletions crates/chat-app-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,41 +58,8 @@ async fn main() -> anyhow::Result<()> {
.application,
)
.context("failed to get socket address")?;
let (mut join_set, cancellation_tracker, _) = start_servers(api_server_builder, addr).await;
let join_outcome = join_set.join_next().await.context("no tasks in join set")?;
report_exit(join_outcome);

// Cancel any remaining tasks
cancel_remaining_tasks(cancellation_tracker).await;

Ok(())
}

fn report_exit(join_set_outcome: Result<(&str, Result<anyhow::Result<()>, JoinError>), JoinError>) {
match join_set_outcome {
Ok((task_name, spawn_join_outcome)) => match spawn_join_outcome {
Ok(Ok(())) => info!("{task_name} has exited from the join set with Ok(())"),
Ok(Err(e)) => {
error!(
error.cause_chain = ?e,
error.message = %e,
"{task_name} resulted in an error: {e}"
);
}
Err(e) => {
error!(
error.cause_chain = ?e,
error.message = %e,
"{task_name} resulted in a join error so it must have panicked"
);
}
},
Err(e) => {
error!( // Not expected to happen as we have a very small anonymous async function that should not panic
error.cause_chain = ?e,
error.message = %e,
"anonymous async function panicked instead of returning the task name. NO TASK name available"
);
}
}
ShuttleService(api_server_builder)
.bind(addr)
.await
.context("service runtime error")
}
47 changes: 46 additions & 1 deletion crates/chat-app-server/src/startup.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::websocket::WsIds;
use actix_web::web::{self, ServiceConfig};
use anyhow::Context;
use plugin_chat::server_only::{
chat_ws_start_client_handler_loop, ChatPlugin, ChatPluginConfig, ChatSettings,
};
use tokio::task::{JoinError, JoinSet};
use tracing::info;
use tracing::{error, info};
use tracked_cancellations::CancellationTracker;
use ws_auth::ws_get_route_add_closures;
use wykies_server::{
cancel_remaining_tasks,
plugin::{ServerPlugin, ServerPluginArtifacts},
ApiServerBuilder, ServerTask as _,
};
Expand Down Expand Up @@ -91,3 +93,46 @@ pub async fn start_servers(

(result, cancellation_tacker, port)
}

struct ShuttleService(ApiServerBuilder<CustomConfiguration>);
impl shuttle_runtime::Service for ShuttleService {
async fn bind(self, addr: std::net::SocketAddr) -> Result<(), shuttle_runtime::Error> {

Check failure on line 99 in crates/chat-app-server/src/startup.rs

View workflow job for this annotation

GitHub Actions / Test

lifetime parameters or bounds on method `bind` do not match the trait declaration
let (mut join_set, cancellation_tracker, _) = start_servers(self.0, addr).await;
let join_outcome = join_set.join_next().await.context("no tasks in join set")?;
report_exit(join_outcome);

// Cancel any remaining tasks
cancel_remaining_tasks(cancellation_tracker).await;

Ok(())
}
}

fn report_exit(join_set_outcome: Result<(&str, Result<anyhow::Result<()>, JoinError>), JoinError>) {
match join_set_outcome {
Ok((task_name, spawn_join_outcome)) => match spawn_join_outcome {
Ok(Ok(())) => info!("{task_name} has exited from the join set with Ok(())"),
Ok(Err(e)) => {
error!(
error.cause_chain = ?e,
error.message = %e,
"{task_name} resulted in an error: {e}"
);
}
Err(e) => {
error!(
error.cause_chain = ?e,
error.message = %e,
"{task_name} resulted in a join error so it must have panicked"
);
}
},
Err(e) => {
error!( // Not expected to happen as we have a very small anonymous async function that should not panic
error.cause_chain = ?e,
error.message = %e,
"anonymous async function panicked instead of returning the task name. NO TASK name available"
);
}
}
}

0 comments on commit ddab42e

Please sign in to comment.