autumn-web 0.5.0

An opinionated, convention-over-configuration web framework for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
#![allow(
    clippy::significant_drop_tightening,
    clippy::missing_panics_doc,
    clippy::missing_errors_doc
)]
//! Outbound signed webhook delivery with retries, DLQ, and subscription management.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, RwLock};

use crate::http_client::Client;
use crate::{AppState, AutumnError, AutumnResult};

const MAX_LOGGED_RESPONSE_BODY_BYTES: usize = 16 * 1024;
const TRUNCATED_RESPONSE_BODY_SUFFIX: &str = "\n[truncated]";

/// The status of a webhook subscription.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WebhookSubscriptionStatus {
    Active,
    Disabled,
    Failed,
}

impl WebhookSubscriptionStatus {
    /// Return the lower-case status label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Active => "active",
            Self::Disabled => "disabled",
            Self::Failed => "failed",
        }
    }
}

impl std::fmt::Display for WebhookSubscriptionStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A registered webhook subscription targeting a consumer endpoint.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookSubscription {
    pub id: String,
    pub target_url: String,
    pub event_topics: Vec<String>,
    pub secret: String,
    pub status: WebhookSubscriptionStatus,
    pub consecutive_failures: u32,
}

/// A structured log of an outbound webhook delivery attempt.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookDeliveryLog {
    pub id: String,
    pub subscription_id: String,
    pub topic: String,
    pub payload: String,
    pub request_headers: HashMap<String, String>,
    pub response_status: Option<u16>,
    pub response_body: Option<String>,
    pub elapsed_ms: u64,
    pub attempt: u32,
    pub max_attempts: u32,
    pub is_dlq: bool,
    pub last_error: Option<String>,
    pub timestamp: DateTime<Utc>,
}

/// Pluggable handler interface for outbound webhook subscriptions and delivery logs.
pub trait OutboundWebhookHandler: Send + Sync + 'static {
    /// Retrieve active subscriptions registered for a specific event topic.
    fn get_subscriptions(
        &self,
        topic: &str,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<Vec<WebhookSubscription>>> + Send>>;

    /// Log a webhook delivery attempt and handle failure counters/statuses.
    fn log_delivery(
        &self,
        log: WebhookDeliveryLog,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>>;

    /// Replace a stored delivery log without treating it as a new delivery outcome.
    ///
    /// Implementations must perform a plain record replacement. This must not
    /// update subscription failure counters or auto-failure state.
    fn replace_delivery_log(
        &self,
        log: WebhookDeliveryLog,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>>;

    /// Retrieve a specific webhook subscription by ID (regardless of status/active state).
    fn get_subscription(
        &self,
        id: &str,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<Option<WebhookSubscription>>> + Send>>;

    /// Optional: List only permanently failed delivery attempts archived in the Dead Letter Queue.
    fn get_dlq_logs(
        &self,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<Vec<WebhookDeliveryLog>>> + Send>> {
        Box::pin(async { Ok(Vec::new()) })
    }

    /// Get a specific delivery log by ID.
    fn get_delivery_log(
        &self,
        id: &str,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<Option<WebhookDeliveryLog>>> + Send>>;

    /// Optional: Reset consecutive failures for a subscription.
    fn reset_subscription_failures(
        &self,
        _id: &str,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
        Box::pin(async { Ok(()) })
    }

    /// Optional: Reactivate a subscription that was auto-marked as failed.
    ///
    /// Manual DLQ replays need to bypass the automatic failure guard without
    /// re-enabling subscriptions that an operator explicitly disabled.
    fn reactivate_failed_subscription(
        &self,
        id: &str,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
        self.reset_subscription_failures(id)
    }
}

/// Legacy alias for backward compatibility.
pub use OutboundWebhookHandler as OutboundWebhookStore;

/// Bounded, thread-safe, process-local in-memory implementation of the outbound webhook handler.
#[derive(Debug, Default)]
pub struct InMemoryOutboundWebhookHandler {
    subscriptions: RwLock<HashMap<String, WebhookSubscription>>,
    logs: RwLock<HashMap<String, WebhookDeliveryLog>>,
}

/// Legacy alias for backward compatibility.
pub type InMemoryOutboundWebhookStore = InMemoryOutboundWebhookHandler;

impl InMemoryOutboundWebhookHandler {
    /// Create a new, empty in-memory handler.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Helper to register a subscription in memory for testing/dev.
    #[allow(clippy::unused_async)]
    pub async fn create_subscription(
        &self,
        sub: WebhookSubscription,
    ) -> AutumnResult<WebhookSubscription> {
        let mut subs = self
            .subscriptions
            .write()
            .expect("subscriptions write lock poisoned");
        subs.insert(sub.id.clone(), sub.clone());
        Ok(sub)
    }

    /// Helper to retrieve logged deliveries for testing/dev.
    #[allow(clippy::unused_async)]
    pub async fn get_delivery_logs(&self) -> AutumnResult<Vec<WebhookDeliveryLog>> {
        let logs = self.logs.read().expect("logs read lock poisoned");
        let mut list: Vec<WebhookDeliveryLog> = logs.values().cloned().collect();
        list.sort_by_key(|l| l.timestamp);
        list.reverse();
        Ok(list)
    }

    /// Helper to fetch a single subscription.
    #[allow(clippy::unused_async)]
    pub async fn get_subscription(&self, id: &str) -> AutumnResult<Option<WebhookSubscription>> {
        let subs = self
            .subscriptions
            .read()
            .expect("subscriptions read lock poisoned");
        Ok(subs.get(id).cloned())
    }
}

impl OutboundWebhookHandler for InMemoryOutboundWebhookHandler {
    fn get_subscriptions(
        &self,
        topic: &str,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<Vec<WebhookSubscription>>> + Send>> {
        let subs = self
            .subscriptions
            .read()
            .expect("subscriptions read lock poisoned");
        let topic = topic.to_owned();
        let list: Vec<WebhookSubscription> = subs
            .values()
            .filter(|sub| {
                sub.event_topics.iter().any(|t| t == &topic)
                    && sub.status == WebhookSubscriptionStatus::Active
            })
            .cloned()
            .collect();
        Box::pin(async move { Ok(list) })
    }

    fn get_subscription(
        &self,
        id: &str,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<Option<WebhookSubscription>>> + Send>> {
        let subs = self
            .subscriptions
            .read()
            .expect("subscriptions read lock poisoned");
        let sub = subs.get(id).cloned();
        Box::pin(async move { Ok(sub) })
    }

    fn log_delivery(
        &self,
        log: WebhookDeliveryLog,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
        let mut logs = self.logs.write().expect("logs write lock poisoned");
        logs.insert(log.id.clone(), log.clone());

        // Manage subscription consecutive failures and auto-disabling state
        let mut subs = self
            .subscriptions
            .write()
            .expect("subscriptions write lock poisoned");
        if let Some(sub) = subs.get_mut(&log.subscription_id) {
            let is_active = sub.status == WebhookSubscriptionStatus::Active;
            if is_active {
                if let Some(status) = log.response_status {
                    if (200..300).contains(&status) {
                        sub.consecutive_failures = 0;
                    } else {
                        sub.consecutive_failures = sub.consecutive_failures.saturating_add(1);
                        if sub.consecutive_failures >= 50 {
                            sub.status = WebhookSubscriptionStatus::Failed;
                            tracing::warn!(subscription_id = %sub.id, "Webhook subscription auto-disabled due to 50 consecutive failures");
                        }
                    }
                } else if log.last_error.is_some() {
                    sub.consecutive_failures = sub.consecutive_failures.saturating_add(1);
                    if sub.consecutive_failures >= 50 {
                        sub.status = WebhookSubscriptionStatus::Failed;
                        tracing::warn!(subscription_id = %sub.id, "Webhook subscription auto-disabled due to 50 consecutive failures");
                    }
                }
            }
        }

        Box::pin(async move { Ok(()) })
    }

    fn replace_delivery_log(
        &self,
        log: WebhookDeliveryLog,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
        let mut logs = self.logs.write().expect("logs write lock poisoned");
        logs.insert(log.id.clone(), log);
        Box::pin(async move { Ok(()) })
    }

    fn get_dlq_logs(
        &self,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<Vec<WebhookDeliveryLog>>> + Send>> {
        let list = {
            let logs = self.logs.read().expect("logs read lock poisoned");
            let mut list: Vec<WebhookDeliveryLog> =
                logs.values().filter(|l| l.is_dlq).cloned().collect();
            list.sort_by_key(|l| l.timestamp);
            list.reverse();
            list
        };
        Box::pin(async move { Ok(list) })
    }

    fn get_delivery_log(
        &self,
        id: &str,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<Option<WebhookDeliveryLog>>> + Send>> {
        let log = self
            .logs
            .read()
            .expect("logs read lock poisoned")
            .get(id)
            .cloned();
        Box::pin(async move { Ok(log) })
    }

    fn reset_subscription_failures(
        &self,
        id: &str,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
        {
            let mut subs = self
                .subscriptions
                .write()
                .expect("subscriptions write lock poisoned");
            if let Some(sub) = subs.get_mut(id) {
                sub.consecutive_failures = 0;
            }
        }
        Box::pin(async move { Ok(()) })
    }

    fn reactivate_failed_subscription(
        &self,
        id: &str,
    ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
        {
            let mut subs = self
                .subscriptions
                .write()
                .expect("subscriptions write lock poisoned");
            if let Some(sub) = subs.get_mut(id) {
                sub.consecutive_failures = 0;
                if sub.status == WebhookSubscriptionStatus::Failed {
                    sub.status = WebhookSubscriptionStatus::Active;
                }
            }
        }
        Box::pin(async move { Ok(()) })
    }
}

/// A runtime delegation callback type to bridge core autumn to autumn-harvest dynamically.
pub type WebhookDelegate = Arc<
    dyn Fn(
            &AppState,
            WebhookSubscription,
            WebhookDeliveryLog,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>>
        + Send
        + Sync,
>;

/// `AppState` extension for the runtime delegation hook.
#[derive(Clone)]
pub struct WebhookDelegateExt(pub WebhookDelegate);

/// The runtime manager for outbound webhooks.
#[derive(Clone)]
pub struct WebhookOutboundManager {
    handler: Arc<dyn OutboundWebhookHandler>,
    client: Client,
    initial_backoff_ms: u64,
}

impl WebhookOutboundManager {
    /// Create a new webhook manager with a handler.
    pub fn new(handler: Arc<dyn OutboundWebhookHandler>) -> Self {
        Self {
            handler,
            client: Client::new(),
            initial_backoff_ms: 1000,
        }
    }

    /// Set a custom initial backoff for retries.
    #[must_use]
    pub const fn with_initial_backoff_ms(mut self, ms: u64) -> Self {
        self.initial_backoff_ms = ms;
        self
    }

    fn with_client_from_state(mut self, state: &AppState) -> Self {
        self.client = Client::from_state(state);
        self
    }

    /// Access the underlying webhook handler (compatibility/actuator support).
    #[must_use]
    pub fn store(&self) -> &Arc<dyn OutboundWebhookHandler> {
        &self.handler
    }

    /// Access the underlying http client.
    #[must_use]
    pub const fn client(&self) -> &Client {
        &self.client
    }

    /// Dispatch a signed webhook payload to all subscriptions interested in `topic`.
    ///
    /// # Errors
    ///
    /// Returns [`AutumnError`] if payload serialization or queueing fails.
    pub async fn dispatch<T: Serialize + Sync>(
        &self,
        state: &AppState,
        topic: &str,
        payload: &T,
    ) -> AutumnResult<()> {
        let serialized = serde_json::to_string(payload).map_err(|e| {
            AutumnError::internal_server_error_msg(format!("failed to serialize payload: {e}"))
        })?;

        let mut errors = Vec::new();
        let subs = self.handler.get_subscriptions(topic).await?;
        for sub in subs {
            if sub.status == WebhookSubscriptionStatus::Disabled {
                continue;
            }

            let log_id = uuid::Uuid::new_v4().to_string();
            let log = WebhookDeliveryLog {
                id: log_id.clone(),
                subscription_id: sub.id.clone(),
                topic: topic.to_owned(),
                payload: serialized.clone(),
                request_headers: HashMap::new(),
                response_status: None,
                response_body: None,
                elapsed_ms: 0,
                attempt: 1,
                max_attempts: 5,
                is_dlq: false,
                last_error: None,
                timestamp: Utc::now(),
            };

            // Register the initial attempt in local storage
            if let Err(e) = self.handler.log_delivery(log.clone()).await {
                errors.push(e);
                continue;
            }

            // If a delegate extension is registered, run it (delegates to Harvest workflow)
            if let Some(delegate_ext) = state.extension::<WebhookDelegateExt>() {
                tracing::info!(subscription_id = %sub.id, "WebhookOutboundManager::dispatch: delegating webhook delivery via runtime hook");
                if let Err(e) = (delegate_ext.0)(state, sub, log).await {
                    errors.push(e);
                }
            } else {
                // Fallback: enqueue a standard background job
                tracing::debug!(subscription_id = %sub.id, "WebhookOutboundManager::dispatch: enqueuing fallback webhook delivery job");
                if let Some(job_client) = crate::job::global_job_client() {
                    let job_payload = serde_json::json!({
                        "log_id": log.id.clone(),
                    });
                    if let Err(e) = job_client
                        .enqueue("autumn_webhook_delivery", job_payload)
                        .await
                    {
                        errors.push(
                            self.record_delivery_enqueue_failure(log, e.to_string())
                                .await,
                        );
                    }
                } else {
                    errors.push(
                        self.record_delivery_enqueue_failure(
                            log,
                            "Global job client is unavailable; fallback webhook delivery job not enqueued"
                                .to_owned(),
                        )
                        .await,
                    );
                }
            }
        }

        if !errors.is_empty() {
            return Err(errors.remove(0));
        }

        Ok(())
    }

    async fn record_delivery_enqueue_failure(
        &self,
        mut log: WebhookDeliveryLog,
        message: String,
    ) -> AutumnError {
        log.is_dlq = true;
        log.last_error = Some(message.clone());
        log.timestamp = Utc::now();

        if let Err(e) = self.handler.replace_delivery_log(log).await {
            tracing::error!(
                error = %e,
                "Failed to mark webhook delivery log as DLQ after enqueue failure"
            );
            return e;
        }

        AutumnError::internal_server_error_msg(message)
    }
}

fn install_outbound_webhook_manager(
    state: &AppState,
    store: Arc<dyn OutboundWebhookHandler>,
    initial_backoff_ms: u64,
) {
    let manager = WebhookOutboundManager::new(store)
        .with_initial_backoff_ms(initial_backoff_ms)
        .with_client_from_state(state);
    state.insert_extension(manager);
}

/// Asynchronous background job that delivers a webhook payload (legacy fallback).
#[must_use]
#[allow(clippy::redundant_closure_for_method_calls, clippy::too_many_lines)]
pub fn deliver_webhook_job(
    state: AppState,
    payload: serde_json::Value,
) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send + 'static>> {
    Box::pin(async move {
        let is_replay = payload
            .get("replay")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        let manager = state.extension::<WebhookOutboundManager>().ok_or_else(|| {
            AutumnError::internal_server_error_msg("WebhookOutboundManager not found in extensions")
        })?;

        // Support both self-contained payload structure and legacy log_id lookup (for replays)
        let (sub, mut log) = if let Some(sub_val) = payload.get("subscription") {
            let _payload_sub: WebhookSubscription = serde_json::from_value(sub_val.clone())
                .map_err(|e| {
                    AutumnError::bad_request_msg(format!("failed to parse subscription: {e}"))
                })?;
            let mut log: WebhookDeliveryLog = serde_json::from_value(
                payload
                    .get("log")
                    .cloned()
                    .ok_or_else(|| AutumnError::bad_request_msg("missing log in job payload"))?,
            )
            .map_err(|e| AutumnError::bad_request_msg(format!("failed to parse log: {e}")))?;

            // If this log has already been attempted (i.e. is running a retry from the job runner),
            // increment the attempt counter and write the pre-send log.
            if log.response_status.is_some() || log.last_error.is_some() {
                log.attempt = log.attempt.saturating_add(1);
                log.response_status = None;
                log.response_body = None;
                log.last_error = None;
                manager.store().log_delivery(log.clone()).await?;
            }

            let sub = load_current_subscription(&manager, &log).await?;
            (sub, log)
        } else {
            let log_id = payload
                .get("log_id")
                .and_then(|v| v.as_str())
                .ok_or_else(|| AutumnError::bad_request_msg("missing log_id in job payload"))?;

            tracing::debug!(log_id = %log_id, "deliver_webhook_job: starting webhook delivery via log lookup");

            let log_opt = manager.store().get_delivery_log(log_id).await?;
            let mut log = log_opt.ok_or_else(|| {
                AutumnError::not_found_msg(format!("delivery log {log_id} not found"))
            })?;

            // If this log has already been attempted (i.e. is running a retry from the job runner),
            // increment the attempt counter and write the pre-send log.
            if log.response_status.is_some() || log.last_error.is_some() {
                log.attempt = log.attempt.saturating_add(1);
                log.response_status = None;
                log.response_body = None;
                log.last_error = None;
                manager.store().log_delivery(log.clone()).await?;
            }

            // Load latest subscription state to respect emergency rotations/disable
            let sub = load_current_subscription(&manager, &log).await?;
            (sub, log)
        };

        if sub.status == WebhookSubscriptionStatus::Disabled {
            tracing::info!(subscription_id = %sub.id, "Webhook subscription is disabled; skipping delivery");
            log.last_error = Some("Subscription is disabled".to_owned());
            log.timestamp = Utc::now();
            if is_replay {
                log.is_dlq = true;
            }
            manager.store().log_delivery(log).await?;
            return Ok(());
        }

        if sub.status == WebhookSubscriptionStatus::Failed && !is_replay {
            tracing::info!(subscription_id = %sub.id, "Webhook subscription has failed; skipping delivery");
            log.last_error = Some("Subscription has failed due to consecutive errors".to_owned());
            log.timestamp = Utc::now();
            manager.store().log_delivery(log).await?;
            return Ok(());
        }
        if sub.status == WebhookSubscriptionStatus::Failed {
            tracing::info!(subscription_id = %sub.id, "Replaying webhook delivery for failed subscription");
        }

        // Stripe-style payload signing: t=<timestamp>,v1=<signature>
        let timestamp = Utc::now().timestamp();
        let signing_payload = format!("{timestamp}.{}", log.payload);
        let signature = crate::security::config::hmac_sha256_hex(
            sub.secret.as_bytes(),
            signing_payload.as_bytes(),
        );
        let signature_header = format!("t={timestamp},v1={signature}");

        let mut request_headers = HashMap::new();
        request_headers.insert("Content-Type".to_owned(), "application/json".to_owned());
        request_headers.insert("Autumn-Signature".to_owned(), signature_header.clone());

        let start = std::time::Instant::now();
        let req = manager
            .client
            .named(&sub.target_url)
            .post(&sub.target_url)
            .header("Content-Type", "application/json")
            .header("Autumn-Signature", signature_header)
            .text_body(log.payload.clone());

        let response = req.send().await;
        let elapsed = u64::try_from(start.elapsed().as_millis()).unwrap_or(u64::MAX);

        tracing::debug!(
            log_id = %log.id,
            status = ?response.as_ref().map(|r| r.status()),
            "deliver_webhook_job: webhook HTTP request finished"
        );

        log.elapsed_ms = elapsed;
        log.timestamp = Utc::now();
        log.request_headers = request_headers;

        match response {
            Ok(res) => {
                let status = res.status();
                log.response_status = Some(status.as_u16());
                let is_success = res.is_success();
                let body_str = cap_logged_response_body(res.text());
                log.response_body = Some(body_str);

                if is_success {
                    log.last_error = None;
                    manager.store().log_delivery(log).await?;
                    reset_subscription_after_success(&manager, &sub).await;
                    Ok(())
                } else {
                    let status_err = format!("server returned status: {status}");
                    log.last_error = Some(status_err.clone());
                    if log.attempt < log.max_attempts {
                        manager.store().log_delivery(log.clone()).await?;
                    }
                    handle_delivery_failure(&manager, &sub, log, status_err).await
                }
            }
            Err(e) => {
                let error_str = e.to_string();
                log.last_error = Some(error_str.clone());
                if log.attempt < log.max_attempts {
                    manager.store().log_delivery(log.clone()).await?;
                }
                handle_delivery_failure(&manager, &sub, log, error_str).await
            }
        }
    })
}

async fn load_current_subscription(
    manager: &WebhookOutboundManager,
    log: &WebhookDeliveryLog,
) -> AutumnResult<WebhookSubscription> {
    manager
        .store()
        .get_subscription(&log.subscription_id)
        .await?
        .ok_or_else(|| {
            AutumnError::not_found_msg(format!("subscription {} not found", log.subscription_id))
        })
}

fn cap_logged_response_body(mut body: String) -> String {
    if body.len() <= MAX_LOGGED_RESPONSE_BODY_BYTES {
        return body;
    }

    let body_budget =
        MAX_LOGGED_RESPONSE_BODY_BYTES.saturating_sub(TRUNCATED_RESPONSE_BODY_SUFFIX.len());
    let mut cutoff = body_budget.min(body.len());
    while cutoff > 0 && !body.is_char_boundary(cutoff) {
        cutoff -= 1;
    }
    body.truncate(cutoff);
    body.push_str(TRUNCATED_RESPONSE_BODY_SUFFIX);
    body
}

async fn reset_subscription_after_success(
    manager: &WebhookOutboundManager,
    sub: &WebhookSubscription,
) {
    if let Err(e) = manager
        .store()
        .reactivate_failed_subscription(&sub.id)
        .await
    {
        tracing::warn!(
            subscription_id = %sub.id,
            "Webhook delivery succeeded but subscription failure state could not be reset: {}",
            e
        );
    }
}

async fn handle_delivery_failure(
    manager: &WebhookOutboundManager,
    sub: &WebhookSubscription,
    mut log: WebhookDeliveryLog,
    error_msg: String,
) -> AutumnResult<()> {
    if log.attempt < log.max_attempts {
        // Return an error to signal the background job runner to retry this job
        Err(AutumnError::internal_server_error_msg(format!(
            "delivery attempt {} failed, scheduled retry: {error_msg}",
            log.attempt
        )))
    } else {
        log.is_dlq = true;
        manager.store().log_delivery(log).await?;
        // Return Ok(()) to mark the permanently failed job as complete and send to DLQ
        tracing::warn!(subscription_id = %sub.id, "Webhook delivery failed permanently; sent to DLQ: {}", error_msg);
        Ok(())
    }
}

/// `AppBuilder` plugin for outbound signed webhook delivery infrastructure.
pub struct OutboundWebhookPlugin {
    store: Arc<dyn OutboundWebhookHandler>,
    initial_backoff_ms: u64,
}

impl OutboundWebhookPlugin {
    /// Create a new outbound webhook plugin using the specified store.
    #[must_use]
    pub fn new(store: Arc<dyn OutboundWebhookHandler>) -> Self {
        Self {
            store,
            initial_backoff_ms: 1000,
        }
    }

    /// Override the initial backoff retry delay.
    #[must_use]
    pub const fn with_initial_backoff_ms(mut self, ms: u64) -> Self {
        self.initial_backoff_ms = ms;
        self
    }
}

impl crate::plugin::Plugin for OutboundWebhookPlugin {
    fn build(self, app: crate::app::AppBuilder) -> crate::app::AppBuilder {
        let store = self.store;
        let initial_backoff_ms = self.initial_backoff_ms;

        app.state_initializer(move |state| {
            install_outbound_webhook_manager(state, store.clone(), initial_backoff_ms);
        })
        .jobs(vec![crate::job::JobInfo {
            name: "autumn_webhook_delivery".to_string(),
            max_attempts: 10, // Retries are handled durably via the background job engine
            initial_backoff_ms,
            uniqueness: None,
            concurrency: None,
            handler: deliver_webhook_job,
        }])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::http_client::{HttpMockRegistryExt, MockRegistry, MockSetupBuilder};
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn mock_builder(registry: Arc<MockRegistry>, alias: &str) -> MockSetupBuilder {
        MockSetupBuilder {
            registry,
            alias: alias.to_owned(),
            method: None,
            path: None,
        }
    }

    fn sample_subscription(
        id: &str,
        target_url: &str,
        status: WebhookSubscriptionStatus,
    ) -> WebhookSubscription {
        WebhookSubscription {
            id: id.to_owned(),
            target_url: target_url.to_owned(),
            event_topics: vec!["orders.created".to_owned()],
            secret: "my_webhook_signing_secret_32_bytes!!".to_owned(),
            status,
            consecutive_failures: if status == WebhookSubscriptionStatus::Failed {
                50
            } else {
                0
            },
        }
    }

    fn sample_log(id: &str, subscription_id: &str) -> WebhookDeliveryLog {
        WebhookDeliveryLog {
            id: id.to_owned(),
            subscription_id: subscription_id.to_owned(),
            topic: "orders.created".to_owned(),
            payload: serde_json::json!({ "order_id": "ord_123" }).to_string(),
            request_headers: HashMap::new(),
            response_status: None,
            response_body: None,
            elapsed_ms: 0,
            attempt: 1,
            max_attempts: 5,
            is_dlq: false,
            last_error: None,
            timestamp: Utc::now(),
        }
    }

    #[test]
    fn outbound_webhook_plugin_installs_manager_without_startup_hook() {
        let store = Arc::new(InMemoryOutboundWebhookHandler::new());
        let builder = crate::app().plugin(OutboundWebhookPlugin::new(store));

        assert!(
            builder.startup_hooks.is_empty(),
            "webhook manager must be installed before job workers start, not from a startup hook"
        );
        assert_eq!(builder.state_initializers.len(), 1);
    }

    #[tokio::test]
    async fn replay_job_sends_failed_subscription_instead_of_skipping() {
        let state = AppState::for_test();
        let store = Arc::new(InMemoryOutboundWebhookHandler::new());
        let registry = Arc::new(MockRegistry::new());
        let mock = mock_builder(registry.clone(), "http://mock-receiver/webhooks/replay")
            .post("/webhooks/replay")
            .respond_with(200, serde_json::json!({ "received": true }));
        state.insert_extension(HttpMockRegistryExt(registry));
        install_outbound_webhook_manager(&state, store.clone(), 1);

        let sub = sample_subscription(
            "sub_failed",
            "http://mock-receiver/webhooks/replay",
            WebhookSubscriptionStatus::Failed,
        );
        store.create_subscription(sub).await.unwrap();
        store
            .replace_delivery_log(sample_log("log_replay", "sub_failed"))
            .await
            .unwrap();

        deliver_webhook_job(
            state,
            serde_json::json!({
                "log_id": "log_replay",
                "replay": true,
            }),
        )
        .await
        .unwrap();

        mock.expect_called(1);
        let log = store
            .get_delivery_log("log_replay")
            .await
            .unwrap()
            .expect("log should remain stored");
        assert_eq!(log.response_status, Some(200));
        assert!(!log.is_dlq);
        assert!(log.last_error.is_none());

        let updated_sub = store
            .get_subscription("sub_failed")
            .await
            .unwrap()
            .expect("subscription should remain stored");
        assert_eq!(updated_sub.status, WebhookSubscriptionStatus::Active);
        assert_eq!(updated_sub.consecutive_failures, 0);
    }

    #[tokio::test]
    async fn replay_job_keeps_disabled_subscription_log_in_dlq() {
        let state = AppState::for_test();
        let store = Arc::new(InMemoryOutboundWebhookHandler::new());
        let registry = Arc::new(MockRegistry::new());
        let mock = mock_builder(registry.clone(), "http://mock-receiver/webhooks/disabled")
            .post("/webhooks/disabled")
            .respond_with(200, serde_json::json!({ "received": true }));
        state.insert_extension(HttpMockRegistryExt(registry));
        install_outbound_webhook_manager(&state, store.clone(), 1);

        let sub = sample_subscription(
            "sub_disabled",
            "http://mock-receiver/webhooks/disabled",
            WebhookSubscriptionStatus::Disabled,
        );
        store.create_subscription(sub).await.unwrap();
        store
            .replace_delivery_log(sample_log("log_disabled_replay", "sub_disabled"))
            .await
            .unwrap();

        deliver_webhook_job(
            state,
            serde_json::json!({
                "log_id": "log_disabled_replay",
                "replay": true,
            }),
        )
        .await
        .unwrap();

        mock.expect_called(0);
        let log = store
            .get_delivery_log("log_disabled_replay")
            .await
            .unwrap()
            .expect("log should remain stored");
        assert!(log.is_dlq, "disabled replay must remain visible in DLQ");
        assert_eq!(log.last_error.as_deref(), Some("Subscription is disabled"));
        assert_eq!(log.response_status, None);
    }

    #[tokio::test]
    async fn self_contained_delivery_uses_latest_subscription_state() {
        let state = AppState::for_test();
        let store = Arc::new(InMemoryOutboundWebhookHandler::new());
        let registry = Arc::new(MockRegistry::new());
        let stale_mock = mock_builder(registry.clone(), "http://mock-receiver/webhooks/stale")
            .post("/webhooks/stale")
            .respond_with(200, serde_json::json!({ "received": true }));
        state.insert_extension(HttpMockRegistryExt(registry));
        install_outbound_webhook_manager(&state, store.clone(), 1);

        let stored_sub = sample_subscription(
            "sub_refresh",
            "http://mock-receiver/webhooks/current-disabled",
            WebhookSubscriptionStatus::Disabled,
        );
        store.create_subscription(stored_sub).await.unwrap();
        let stale_sub = sample_subscription(
            "sub_refresh",
            "http://mock-receiver/webhooks/stale",
            WebhookSubscriptionStatus::Active,
        );
        let log = sample_log("log_refresh", "sub_refresh");

        deliver_webhook_job(
            state,
            serde_json::json!({
                "subscription": stale_sub,
                "log": log,
            }),
        )
        .await
        .unwrap();

        stale_mock.expect_called(0);
        let stored = store
            .get_delivery_log("log_refresh")
            .await
            .unwrap()
            .expect("delivery log should exist");
        assert_eq!(stored.response_status, None);
        assert_eq!(
            stored.last_error.as_deref(),
            Some("Subscription is disabled")
        );
    }

    #[tokio::test]
    async fn dispatch_marks_log_dlq_when_fallback_enqueue_fails() {
        let _guard = crate::job::global_job_runtime_test_lock().lock().await;
        crate::job::clear_global_job_client();

        let state = AppState::for_test();
        let store = Arc::new(InMemoryOutboundWebhookHandler::new());
        let manager = WebhookOutboundManager::new(store.clone()).with_initial_backoff_ms(1);
        let sub = sample_subscription(
            "sub_enqueue_missing",
            "http://mock-receiver/webhooks/enqueue-missing",
            WebhookSubscriptionStatus::Active,
        );
        store.create_subscription(sub).await.unwrap();

        let err = manager
            .dispatch(&state, "orders.created", &serde_json::json!({ "id": 42 }))
            .await
            .expect_err("dispatch should report the missing fallback job runtime");
        assert!(
            err.to_string().contains("not enqueued"),
            "error should describe the enqueue failure: {err}"
        );

        let logs = store.get_delivery_logs().await.unwrap();
        assert_eq!(logs.len(), 1);
        let log = &logs[0];
        assert!(
            log.is_dlq,
            "enqueue failure must leave a replayable DLQ record"
        );
        assert!(
            log.last_error
                .as_deref()
                .is_some_and(|msg| msg.contains("not enqueued")),
            "DLQ log should record enqueue failure: {:?}",
            log.last_error
        );
        assert_eq!(log.response_status, None);

        let sub = store
            .get_subscription("sub_enqueue_missing")
            .await
            .unwrap()
            .expect("subscription should remain stored");
        assert_eq!(sub.consecutive_failures, 0);
    }

    #[tokio::test]
    async fn delivery_log_response_body_is_capped() {
        let state = AppState::for_test();
        let store = Arc::new(InMemoryOutboundWebhookHandler::new());
        let registry = Arc::new(MockRegistry::new());
        let large_body = "x".repeat(MAX_LOGGED_RESPONSE_BODY_BYTES + 1024);
        let _mock = mock_builder(
            registry.clone(),
            "http://mock-receiver/webhooks/large-error",
        )
        .post("/webhooks/large-error")
        .respond_with(500, serde_json::json!({ "error": large_body }));
        state.insert_extension(HttpMockRegistryExt(registry));
        install_outbound_webhook_manager(&state, store.clone(), 1);

        let sub = sample_subscription(
            "sub_large_error",
            "http://mock-receiver/webhooks/large-error",
            WebhookSubscriptionStatus::Active,
        );
        store.create_subscription(sub.clone()).await.unwrap();
        let mut log = sample_log("log_large_error", "sub_large_error");
        log.max_attempts = 1;

        deliver_webhook_job(
            state,
            serde_json::json!({
                "subscription": sub,
                "log": log,
            }),
        )
        .await
        .unwrap();

        let stored = store
            .get_delivery_log("log_large_error")
            .await
            .unwrap()
            .expect("delivery log should exist");
        let body = stored
            .response_body
            .expect("response body should be logged");
        assert!(
            body.len() <= MAX_LOGGED_RESPONSE_BODY_BYTES,
            "stored response body should be capped, got {} bytes",
            body.len()
        );
        assert!(body.ends_with("[truncated]"));
    }

    struct CountingReplacementStore {
        log_delivery_calls: AtomicUsize,
    }

    impl CountingReplacementStore {
        fn new() -> Self {
            Self {
                log_delivery_calls: AtomicUsize::new(0),
            }
        }

        fn log_delivery_count(&self) -> usize {
            self.log_delivery_calls.load(Ordering::SeqCst)
        }
    }

    impl OutboundWebhookHandler for CountingReplacementStore {
        fn get_subscriptions(
            &self,
            _topic: &str,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<Vec<WebhookSubscription>>> + Send>> {
            Box::pin(async { Ok(Vec::new()) })
        }

        fn log_delivery(
            &self,
            _log: WebhookDeliveryLog,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
            self.log_delivery_calls.fetch_add(1, Ordering::SeqCst);
            Box::pin(async { Ok(()) })
        }

        fn replace_delivery_log(
            &self,
            _log: WebhookDeliveryLog,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
            Box::pin(async { Ok(()) })
        }

        fn get_subscription(
            &self,
            _id: &str,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<Option<WebhookSubscription>>> + Send>>
        {
            Box::pin(async { Ok(None) })
        }

        fn get_delivery_log(
            &self,
            _id: &str,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<Option<WebhookDeliveryLog>>> + Send>>
        {
            Box::pin(async { Ok(None) })
        }
    }

    #[tokio::test]
    async fn replace_delivery_log_is_not_a_delivery_outcome() {
        let store = CountingReplacementStore::new();
        let mut log = sample_log("log_replace", "sub_replace");
        log.response_status = Some(500);
        log.last_error = Some("server returned status: 500 Internal Server Error".to_owned());
        log.is_dlq = true;

        store.replace_delivery_log(log).await.unwrap();

        assert_eq!(
            store.log_delivery_count(),
            0,
            "plain delivery-log replacement must not call log_delivery"
        );
    }

    struct ResetFailingStore {
        inner: InMemoryOutboundWebhookHandler,
    }

    impl ResetFailingStore {
        fn new() -> Self {
            Self {
                inner: InMemoryOutboundWebhookHandler::new(),
            }
        }

        async fn create_subscription(&self, sub: WebhookSubscription) {
            self.inner.create_subscription(sub).await.unwrap();
        }

        async fn delivery_log(&self, id: &str) -> WebhookDeliveryLog {
            self.inner
                .get_delivery_log(id)
                .await
                .unwrap()
                .expect("delivery log should exist")
        }
    }

    impl OutboundWebhookHandler for ResetFailingStore {
        fn get_subscriptions(
            &self,
            topic: &str,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<Vec<WebhookSubscription>>> + Send>> {
            <InMemoryOutboundWebhookHandler as OutboundWebhookHandler>::get_subscriptions(
                &self.inner,
                topic,
            )
        }

        fn log_delivery(
            &self,
            log: WebhookDeliveryLog,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
            <InMemoryOutboundWebhookHandler as OutboundWebhookHandler>::log_delivery(
                &self.inner,
                log,
            )
        }

        fn replace_delivery_log(
            &self,
            log: WebhookDeliveryLog,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
            <InMemoryOutboundWebhookHandler as OutboundWebhookHandler>::replace_delivery_log(
                &self.inner,
                log,
            )
        }

        fn get_subscription(
            &self,
            id: &str,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<Option<WebhookSubscription>>> + Send>>
        {
            <InMemoryOutboundWebhookHandler as OutboundWebhookHandler>::get_subscription(
                &self.inner,
                id,
            )
        }

        fn get_delivery_log(
            &self,
            id: &str,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<Option<WebhookDeliveryLog>>> + Send>>
        {
            <InMemoryOutboundWebhookHandler as OutboundWebhookHandler>::get_delivery_log(
                &self.inner,
                id,
            )
        }

        fn reset_subscription_failures(
            &self,
            _id: &str,
        ) -> Pin<Box<dyn Future<Output = AutumnResult<()>> + Send>> {
            Box::pin(async {
                Err(AutumnError::internal_server_error_msg(
                    "reset backend unavailable",
                ))
            })
        }
    }

    #[tokio::test]
    async fn successful_delivery_does_not_retry_when_failure_reset_fails() {
        let state = AppState::for_test();
        let store = Arc::new(ResetFailingStore::new());
        let registry = Arc::new(MockRegistry::new());
        let mock = mock_builder(registry.clone(), "http://mock-receiver/webhooks/success")
            .post("/webhooks/success")
            .respond_with(200, serde_json::json!({ "received": true }));
        state.insert_extension(HttpMockRegistryExt(registry));
        install_outbound_webhook_manager(&state, store.clone(), 1);

        let sub = sample_subscription(
            "sub_success",
            "http://mock-receiver/webhooks/success",
            WebhookSubscriptionStatus::Active,
        );
        store.create_subscription(sub.clone()).await;
        let log = sample_log("log_success", "sub_success");

        deliver_webhook_job(
            state,
            serde_json::json!({
                "subscription": sub,
                "log": log,
            }),
        )
        .await
        .expect("accepted webhook delivery must not be retried because counter reset failed");

        mock.expect_called(1);
        let persisted = store.delivery_log("log_success").await;
        assert_eq!(persisted.response_status, Some(200));
        assert!(persisted.last_error.is_none());
    }

    #[tokio::test]
    async fn webhook_manager_uses_http_client_config_base_urls() {
        let _guard = crate::job::global_job_runtime_test_lock().lock().await;
        crate::job::clear_global_job_client();

        let store = Arc::new(InMemoryOutboundWebhookHandler::new());
        let plugin = OutboundWebhookPlugin::new(store.clone()).with_initial_backoff_ms(1);
        let mut config = crate::config::AutumnConfig::default();
        config.http.client.base_urls.insert(
            "hook-service".to_owned(),
            "http://mock-receiver/base".to_owned(),
        );

        let mut app_builder = crate::test::TestApp::new().config(config).plugin(plugin);
        let mock = app_builder
            .http_mock("hook-service")
            .post("/base/hook-service")
            .respond_with(200, serde_json::json!({ "received": true }));
        let app = app_builder.build();
        let state = app.state();

        let sub = sample_subscription(
            "sub_config",
            "hook-service",
            WebhookSubscriptionStatus::Active,
        );
        store.create_subscription(sub.clone()).await.unwrap();
        let log = sample_log("log_config", "sub_config");

        deliver_webhook_job(
            state.clone(),
            serde_json::json!({
                "subscription": sub,
                "log": log,
            }),
        )
        .await
        .unwrap();

        mock.expect_called(1);
        crate::job::clear_global_job_client();
    }
}