Repository navigation
feat: added ConfigChanged webhook event for config mutation notifications #877
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,13 @@ use actix_web::{ | |
| }; | ||
| use chrono::Utc; | ||
| use diesel::{ | ||
| Connection, ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper, | ||
| TextExpressionMethods, | ||
| Connection, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl, | ||
| SelectableHelper, TextExpressionMethods, | ||
| }; | ||
| use jsonschema::ValidationError; | ||
| use serde_json::Value; | ||
| use service_utils::{ | ||
| helpers::parse_config_tags, | ||
| helpers::{WebhookData, execute_webhook_call, parse_config_tags}, | ||
| service::types::{ | ||
| AppHeader, AppState, CustomHeaders, DbConnection, EncryptionKey, SchemaName, | ||
| WorkspaceContext, | ||
|
|
@@ -22,19 +22,21 @@ use superposition_macros::{ | |
| bad_argument, db_error, not_found, unexpected_error, validation_error, | ||
| }; | ||
| use superposition_types::{ | ||
| DBConnection, PaginatedResponse, User, | ||
| DBConnection, PaginatedResponse, Resource, User, | ||
| api::{ | ||
| default_config::{ | ||
| DefaultConfigCreateRequest, DefaultConfigFilters, DefaultConfigKey, | ||
| DefaultConfigUpdateRequest, | ||
| }, | ||
| functions::{FunctionEnvironment, FunctionExecutionRequest, KeyType}, | ||
| webhook::Action, | ||
| }, | ||
| custom_query::PaginationParams, | ||
| database::{ | ||
| models::{ | ||
| Description, | ||
| cac::{self as models, Context, DefaultConfig, FunctionType}, | ||
| others::WebhookEvent, | ||
| }, | ||
| schema::{self, contexts::dsl::contexts, default_configs::dsl}, | ||
| }, | ||
|
|
@@ -165,9 +167,33 @@ async fn create_handler( | |
| })?; | ||
|
|
||
| #[cfg(feature = "high-performance-mode")] | ||
| put_config_in_redis(version_id, state, &workspace_context.schema_name, &mut conn) | ||
| .await?; | ||
| let mut http_resp = HttpResponse::Ok(); | ||
| put_config_in_redis( | ||
| version_id, | ||
| &state, | ||
| &workspace_context.schema_name, | ||
| &mut conn, | ||
| ) | ||
| .await?; | ||
|
|
||
| let data = WebhookData { | ||
| payload: &default_config, | ||
| resource: Resource::DefaultConfig, | ||
| event: WebhookEvent::ConfigChanged, | ||
| config_version_opt: Some(version_id.to_string()), | ||
| action: Action::Create, | ||
| }; | ||
|
|
||
| let webhook_status = | ||
| execute_webhook_call(data, &workspace_context, &state, &mut conn).await; | ||
|
Collaborator
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. here as well |
||
|
|
||
| let mut http_resp = if webhook_status { | ||
| HttpResponse::Ok() | ||
| } else { | ||
| HttpResponse::build( | ||
| actix_web::http::StatusCode::from_u16(512) | ||
| .unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR), | ||
| ) | ||
| }; | ||
|
|
||
| http_resp.insert_header(( | ||
| AppHeader::XConfigVersion.to_string(), | ||
|
|
@@ -297,10 +323,33 @@ async fn update_handler( | |
| })?; | ||
|
|
||
| #[cfg(feature = "high-performance-mode")] | ||
| put_config_in_redis(version_id, state, &workspace_context.schema_name, &mut conn) | ||
| .await?; | ||
| put_config_in_redis( | ||
| version_id, | ||
| &state, | ||
| &workspace_context.schema_name, | ||
| &mut conn, | ||
| ) | ||
| .await?; | ||
|
|
||
| let data = WebhookData { | ||
| payload: &db_row, | ||
| resource: Resource::DefaultConfig, | ||
| event: WebhookEvent::ConfigChanged, | ||
| config_version_opt: Some(version_id.to_string()), | ||
| action: Action::Update, | ||
| }; | ||
|
|
||
| let webhook_status = | ||
| execute_webhook_call(data, &workspace_context, &state, &mut conn).await; | ||
|
Collaborator
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. here as well |
||
|
|
||
| let mut http_resp = HttpResponse::Ok(); | ||
| let mut http_resp = if webhook_status { | ||
| HttpResponse::Ok() | ||
| } else { | ||
| HttpResponse::build( | ||
| actix_web::http::StatusCode::from_u16(512) | ||
| .unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR), | ||
| ) | ||
| }; | ||
| http_resp.insert_header(( | ||
| AppHeader::XConfigVersion.to_string(), | ||
| version_id.to_string(), | ||
|
|
@@ -461,14 +510,13 @@ async fn delete_handler( | |
| let tags = parse_config_tags(custom_headers.config_tags)?; | ||
|
|
||
| let key: String = path.into_inner().into(); | ||
| let mut version_id = 0; | ||
|
|
||
| let context_ids = | ||
| get_key_usage_context_ids(&key, &mut conn, &workspace_context.schema_name) | ||
| .map_err(|_| unexpected_error!("Something went wrong"))?; | ||
| if context_ids.is_empty() { | ||
| let resp: Result<HttpResponse, superposition::AppError> = | ||
| conn.transaction::<_, superposition::AppError, _>(|transaction_conn| { | ||
| let (version_id, default_config) = conn | ||
| .transaction::<_, superposition::AppError, _>(|transaction_conn| { | ||
| diesel::update(dsl::default_configs) | ||
| .filter(dsl::key.eq(&key)) | ||
| .set(( | ||
|
|
@@ -481,18 +529,19 @@ async fn delete_handler( | |
| let deleted_row = | ||
| diesel::delete(dsl::default_configs.filter(dsl::key.eq(&key))) | ||
| .schema_name(&workspace_context.schema_name) | ||
| .execute(transaction_conn); | ||
| .get_result::<DefaultConfig>(transaction_conn) | ||
| .optional()?; | ||
| match deleted_row { | ||
| Ok(0) => { | ||
| Err(not_found!("default config key `{}` doesn't exists", key)) | ||
| None => { | ||
| Err(not_found!("default config key `{}` doesn't exists", key))? | ||
| } | ||
| Ok(_) => { | ||
| Some(default_config) => { | ||
| let config_version_desc = Description::try_from(format!( | ||
| "Context Deleted by {}", | ||
| user.get_email() | ||
| )) | ||
| .map_err(|e| unexpected_error!(e))?; | ||
| version_id = add_config_version( | ||
| let version_id = add_config_version( | ||
| &state, | ||
| tags, | ||
| config_version_desc, | ||
|
|
@@ -503,31 +552,45 @@ async fn delete_handler( | |
| "default config key: {key} deleted by {}", | ||
| user.get_email() | ||
| ); | ||
| Ok(HttpResponse::NoContent() | ||
| .insert_header(( | ||
| AppHeader::XConfigVersion.to_string(), | ||
| version_id.to_string(), | ||
| )) | ||
| .finish()) | ||
| } | ||
| Err(e) => { | ||
| log::error!("default config delete query failed with error: {e}"); | ||
| Err(unexpected_error!("Something went wrong.")) | ||
| Ok((version_id, default_config)) | ||
|
Collaborator
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. this is a very weird code, version_id is a mutable value which is being updated as well as returned
Contributor
Author
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. Again , this also existed before. We can do the cleanup though. |
||
| } | ||
| } | ||
| }); | ||
| })?; | ||
|
|
||
| if resp.is_ok() { | ||
| #[cfg(feature = "high-performance-mode")] | ||
| put_config_in_redis( | ||
| version_id, | ||
| state, | ||
| &workspace_context.schema_name, | ||
| &mut conn, | ||
| #[cfg(feature = "high-performance-mode")] | ||
| put_config_in_redis( | ||
| version_id, | ||
| &state, | ||
| &workspace_context.schema_name, | ||
| &mut conn, | ||
| ) | ||
| .await?; | ||
|
|
||
| let data = WebhookData { | ||
| payload: &default_config, | ||
| resource: Resource::DefaultConfig, | ||
| event: WebhookEvent::ConfigChanged, | ||
| config_version_opt: Some(version_id.to_string()), | ||
| action: Action::Delete, | ||
| }; | ||
|
|
||
| let webhook_status = | ||
| execute_webhook_call(data, &workspace_context, &state, &mut conn).await; | ||
|
Collaborator
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. here as well |
||
|
|
||
| let mut http_resp = if webhook_status { | ||
| HttpResponse::Ok() | ||
| } else { | ||
| HttpResponse::build( | ||
| actix_web::http::StatusCode::from_u16(512) | ||
| .unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR), | ||
| ) | ||
| .await?; | ||
| } | ||
| resp | ||
| }; | ||
| http_resp.insert_header(( | ||
| AppHeader::XConfigVersion.to_string(), | ||
| version_id.to_string(), | ||
| )); | ||
|
|
||
| Ok(http_resp.finish()) | ||
| } else { | ||
| Err(bad_argument!( | ||
| "Given key already in use in contexts: {}", | ||
|
|
||
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.
superposition/crates/context_aware_config/src/api/context/handlers.rs
Lines 131 to 139 in 381648f
Can you add the trigger for the webhook in
add_config_version?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.
No , add_config_version is synchronous and the trigger is async , we cannot call async function inside transaction.
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.
But you can return future which can be awaited on outside the synchronous block of the transaction
Anyways, on failure of webhook we dont even need to revert the queries so response awaiting outside transaction is safe as well
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.
There is a slight problem with this approach - I would be using execute_webhook_call which needs &mut DBConnection, but the transaction still holds the mutable borrow when we create the future. The future can't capture db_conn — the borrow checker won't allow it.
You can't return a future that captures &mut DBConnection from inside a transaction because the connection's lifetime is scoped to the transaction closure.
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.
Explanation:
Modified add_config_version:
Sample Usage:
You will get an error saying :
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.
The problem is that the future borrows transaction_conn, which only lives for the duration of the transaction closure — but we're trying to return the future out of the closure and .await it after the transaction completes.
We can't borrow transaction_conn in a future that outlives the transaction.
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.
Why do you need db conn with async stuff ?
The only async part is API call, which does not even require db conn