Skip to content
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

Fix Recover Failed Webhooks response #1349

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion server/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@
},
"type": "object"
},
"BackgroundTaskStatus": {
"enum": [
"running"
],
"type": "string"
},
"BackgroundTaskType": {
"enum": [
"endpoint.recover"
],
"type": "string"
},
"DashboardAccessOut": {
"properties": {
"token": {
Expand Down Expand Up @@ -1980,6 +1992,25 @@
],
"type": "object"
},
"RecoverOut": {
"properties": {
"id": {
"type": "string"
},
"status": {
"$ref": "#/components/schemas/BackgroundTaskStatus"
},
"task": {
"$ref": "#/components/schemas/BackgroundTaskType"
}
},
"required": [
"id",
"status",
"task"
],
"type": "object"
},
"StatusCodeClass": {
"description": "The different classes of HTTP status codes:\n- CodeNone = 0\n- Code1xx = 100\n- Code2xx = 200\n- Code3xx = 300\n- Code4xx = 400\n- Code5xx = 500",
"enum": [
Expand Down Expand Up @@ -4402,7 +4433,14 @@
},
"responses": {
"202": {
"description": "no content"
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/RecoverOut"
}
}
},
"description": ""
},
"401": {
"content": {
Expand Down
1 change: 1 addition & 0 deletions server/svix-server/src/core/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ create_id_type!(
);
create_id_type!(MessageEndpointId, "msgep_");
create_id_type!(EventTypeId, "evtype_");
create_id_type!(QueueBackgroundTaskId, "qtask_");

create_all_id_types!(ApplicationId, ApplicationUid, ApplicationIdOrUid, "app_");
create_all_id_types!(EndpointId, EndpointUid, EndpointIdOrUid, "ep_");
Expand Down
37 changes: 33 additions & 4 deletions server/svix-server/src/v1/endpoints/endpoint/recovery.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
use axum::extract::{Path, State};
use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use sea_orm::{entity::prelude::*, QueryOrder, QuerySelect};
use serde::{Deserialize, Serialize};
use svix_server_derive::aide_annotate;

use super::RecoverIn;
use crate::{
core::{
permissions,
types::{BaseId, MessageAttemptTriggerType, MessageEndpointId, MessageStatus},
types::{
BaseId, MessageAttemptTriggerType, MessageEndpointId, MessageStatus,
QueueBackgroundTaskId,
},
},
db::models::{application, endpoint, messagedestination},
error::{HttpError, Result, ValidationErrorItem},
queue::{MessageTask, TaskQueueProducer},
v1::utils::{ApplicationEndpointPath, NoContentWithCode, ValidatedJson},
v1::utils::{ApplicationEndpointPath, JsonStatus, ValidatedJson},
AppState,
};

Expand Down Expand Up @@ -66,6 +71,26 @@ async fn bulk_recover_failed_messages(
Ok(())
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum BackgroundTaskStatus {
Running,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]
pub enum BackgroundTaskType {
#[serde(rename = "endpoint.recover")]
Recover,
}

#[derive(Debug, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct RecoverOut {
id: QueueBackgroundTaskId,
status: BackgroundTaskStatus,
task: BackgroundTaskType,
}

/// Resend all failed messages since a given time.
#[aide_annotate(op_id = "v1.endpoint.recover")]
pub(super) async fn recover_failed_webhooks(
Expand All @@ -75,7 +100,7 @@ pub(super) async fn recover_failed_webhooks(
Path(ApplicationEndpointPath { endpoint_id, .. }): Path<ApplicationEndpointPath>,
permissions::Application { app }: permissions::Application,
ValidatedJson(data): ValidatedJson<RecoverIn>,
) -> Result<NoContentWithCode<202>> {
) -> Result<JsonStatus<202, RecoverOut>> {
// Add five minutes so that people can easily just do `now() - two_weeks` without having to worry about clock sync
let timeframe = chrono::Duration::days(14);
let timeframe = timeframe + chrono::Duration::minutes(5);
Expand All @@ -100,5 +125,9 @@ pub(super) async fn recover_failed_webhooks(
async move { bulk_recover_failed_messages(db, queue_tx, app, endp, data.since).await },
);

Ok(NoContentWithCode)
Ok(JsonStatus(RecoverOut {
id: QueueBackgroundTaskId::new(None, None),
status: BackgroundTaskStatus::Running,
task: BackgroundTaskType::Recover,
}))
}