mocra 0.3.0

A distributed, event-driven crawling and data collection framework
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
use crate::errors::{Error, ErrorKind};
use serde::{Deserialize, Serialize};

/// Retry backoff strategy used by a resolved policy.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum BackoffPolicy {
    /// Do not delay between retries.
    None,
    /// Increase delay by a fixed amount each retry until `max_ms`.
    Linear { base_ms: u64, max_ms: u64 },
    /// Increase delay exponentially until `max_ms`.
    Exponential { base_ms: u64, max_ms: u64 },
}

/// Dead-letter queue strategy for failed events.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum DlqPolicy {
    /// Never route to DLQ.
    Never,
    /// Route to DLQ only after retries are exhausted.
    OnExhausted,
    /// Always route to DLQ immediately.
    Always,
}

/// Alert severity emitted for a handled error.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum AlertLevel {
    /// Informational signal.
    Info,
    /// Warning-level alert.
    Warn,
    /// Error-level alert.
    Error,
    /// Critical operational alert.
    Critical,
}

/// Fully materialized policy returned by the resolver.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Policy {
    /// Whether retry is allowed.
    pub retryable: bool,
    /// Backoff strategy for retries.
    pub backoff: BackoffPolicy,
    /// DLQ routing behavior.
    pub dlq: DlqPolicy,
    /// Alerting level.
    pub alert: AlertLevel,
    /// Maximum retry attempts.
    pub max_retries: u32,
    /// Initial backoff value in milliseconds.
    pub backoff_ms: u64,
}

impl Default for Policy {
    fn default() -> Self {
        Self {
            retryable: true,
            backoff: BackoffPolicy::Exponential {
                base_ms: 500,
                max_ms: 30_000,
            },
            dlq: DlqPolicy::OnExhausted,
            alert: AlertLevel::Warn,
            max_retries: 3,
            backoff_ms: 500,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PolicyConfig {
    /// Override rules applied on top of built-in defaults.
    pub overrides: Vec<PolicyOverride>,
}

/// A conditional rule used to override the default policy.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyOverride {
    /// Optional domain match, e.g. `engine`.
    pub domain: Option<String>,
    /// Optional event type match, e.g. `download`.
    pub event_type: Option<String>,
    /// Optional lifecycle phase match, e.g. `failed`.
    pub phase: Option<String>,
    /// Required error kind match.
    pub kind: ErrorKind,
    /// Optional override for retryability.
    pub retryable: Option<bool>,
    /// Optional override for backoff strategy.
    pub backoff: Option<BackoffPolicy>,
    /// Optional override for DLQ strategy.
    pub dlq: Option<DlqPolicy>,
    /// Optional override for alert level.
    pub alert: Option<AlertLevel>,
    /// Optional override for max retries.
    pub max_retries: Option<u32>,
    /// Optional override for initial backoff in milliseconds.
    pub backoff_ms: Option<u64>,
}

/// Resolver output with the selected policy and a normalized reason key.
#[derive(Debug, Clone)]
pub struct Decision {
    /// The resolved policy after defaults + overrides.
    pub policy: Policy,
    /// A normalized trace key in the format `domain/event/phase/kind`.
    pub reason: String,
}

/// Resolves runtime error handling policies using default rules and optional overrides.
#[derive(Debug, Clone)]
pub struct PolicyResolver {
    overrides: Vec<PolicyOverride>,
}

impl PolicyResolver {
    /// Creates a resolver from optional config.
    pub fn new(config: Option<&PolicyConfig>) -> Self {
        let overrides = config.map(|cfg| cfg.overrides.clone()).unwrap_or_default();
        Self { overrides }
    }

    /// Resolves a policy from a concrete [`Error`].
    pub fn resolve_with_error(
        &self,
        domain: &str,
        event_type: Option<&str>,
        phase: Option<&str>,
        err: &Error,
    ) -> Decision {
        self.resolve_with_kind(domain, event_type, phase, err.kind().clone())
    }

    /// Resolves a policy from a known [`ErrorKind`].
    ///
    /// # Examples
    ///
    /// ```
    /// use mocra::common::policy::{DlqPolicy, PolicyResolver};
    /// use mocra::errors::ErrorKind;
    ///
    /// let resolver = PolicyResolver::new(None);
    /// let decision = resolver.resolve_with_kind(
    ///     "engine",
    ///     Some("parser"),
    ///     Some("failed"),
    ///     ErrorKind::Parser,
    /// );
    ///
    /// assert!(!decision.policy.retryable);
    /// assert_eq!(decision.policy.dlq, DlqPolicy::Always);
    /// ```
    pub fn resolve_with_kind(
        &self,
        domain: &str,
        event_type: Option<&str>,
        phase: Option<&str>,
        kind: ErrorKind,
    ) -> Decision {
        let policy = default_policy(domain, event_type, phase, &kind);
        let policy = apply_overrides(policy, &self.overrides, domain, event_type, phase, &kind);
        Decision {
            policy,
            reason: format!(
                "{}/{}/{}/{:?}",
                normalize(domain),
                normalize_opt(event_type).unwrap_or("-"),
                normalize_opt(phase).unwrap_or("-"),
                kind
            ),
        }
    }
}

fn normalize(value: &str) -> &str {
    value.trim()
}

fn normalize_opt(value: Option<&str>) -> Option<&str> {
    value.map(|v| v.trim()).filter(|v| !v.is_empty())
}

fn default_policy(
    domain: &str,
    event_type: Option<&str>,
    phase: Option<&str>,
    kind: &ErrorKind,
) -> Policy {
    let domain = normalize(domain).to_ascii_lowercase();
    let event_type = normalize_opt(event_type).unwrap_or("").to_ascii_lowercase();
    let phase = normalize_opt(phase).unwrap_or("").to_ascii_lowercase();

    let is_failed_or_retry = phase == "failed" || phase == "retry";

    match (
        domain.as_str(),
        event_type.as_str(),
        is_failed_or_retry,
        kind,
    ) {
        ("engine", "task_model", true, ErrorKind::Task | ErrorKind::Module) => Policy {
            backoff: BackoffPolicy::Exponential {
                base_ms: 500,
                max_ms: 30_000,
            },
            dlq: DlqPolicy::OnExhausted,
            alert: AlertLevel::Warn,
            max_retries: 3,
            backoff_ms: 500,
            retryable: true,
        },
        ("engine", "parser_task_model", true, ErrorKind::Parser | ErrorKind::ProcessorChain) => {
            Policy {
                backoff: BackoffPolicy::Exponential {
                    base_ms: 500,
                    max_ms: 30_000,
                },
                dlq: DlqPolicy::OnExhausted,
                alert: AlertLevel::Warn,
                max_retries: 3,
                backoff_ms: 500,
                retryable: true,
            }
        }
        ("engine", "request_publish", true, ErrorKind::Queue) => Policy {
            backoff: BackoffPolicy::Exponential {
                base_ms: 500,
                max_ms: 30_000,
            },
            dlq: DlqPolicy::OnExhausted,
            alert: AlertLevel::Warn,
            max_retries: 3,
            backoff_ms: 500,
            retryable: true,
        },
        ("engine", "request_middleware", _, ErrorKind::Request | ErrorKind::ProcessorChain) => {
            Policy {
                retryable: false,
                backoff: BackoffPolicy::None,
                dlq: DlqPolicy::Always,
                alert: AlertLevel::Error,
                max_retries: 0,
                backoff_ms: 0,
            }
        }
        (
            "engine",
            "download",
            true,
            ErrorKind::Download | ErrorKind::Proxy | ErrorKind::RateLimit,
        ) => Policy {
            backoff: BackoffPolicy::Exponential {
                base_ms: 500,
                max_ms: 60_000,
            },
            dlq: DlqPolicy::OnExhausted,
            alert: AlertLevel::Warn,
            max_retries: 5,
            backoff_ms: 500,
            retryable: true,
        },
        ("engine", "response_middleware", _, ErrorKind::Response | ErrorKind::ProcessorChain) => {
            Policy {
                retryable: false,
                backoff: BackoffPolicy::None,
                dlq: DlqPolicy::Always,
                alert: AlertLevel::Error,
                max_retries: 0,
                backoff_ms: 0,
            }
        }
        ("engine", "response_publish", true, ErrorKind::Queue) => Policy {
            backoff: BackoffPolicy::Exponential {
                base_ms: 500,
                max_ms: 30_000,
            },
            dlq: DlqPolicy::OnExhausted,
            alert: AlertLevel::Warn,
            max_retries: 3,
            backoff_ms: 500,
            retryable: true,
        },
        ("engine", "module_generate", true, ErrorKind::Module | ErrorKind::Task) => Policy {
            backoff: BackoffPolicy::Exponential {
                base_ms: 500,
                max_ms: 30_000,
            },
            dlq: DlqPolicy::OnExhausted,
            alert: AlertLevel::Error,
            max_retries: 3,
            backoff_ms: 500,
            retryable: true,
        },
        ("engine", "parser", true, ErrorKind::Parser) => Policy {
            retryable: false,
            backoff: BackoffPolicy::None,
            dlq: DlqPolicy::Always,
            alert: AlertLevel::Error,
            max_retries: 0,
            backoff_ms: 0,
        },
        ("engine", "middleware_before", _, ErrorKind::DataMiddleware) => Policy {
            backoff: BackoffPolicy::Linear {
                base_ms: 200,
                max_ms: 10_000,
            },
            dlq: DlqPolicy::OnExhausted,
            alert: AlertLevel::Warn,
            max_retries: 3,
            backoff_ms: 200,
            retryable: true,
        },
        ("engine", "data_store", _, ErrorKind::DataStore | ErrorKind::Orm) => Policy {
            backoff: BackoffPolicy::Linear {
                base_ms: 200,
                max_ms: 10_000,
            },
            dlq: DlqPolicy::OnExhausted,
            alert: AlertLevel::Error,
            max_retries: 3,
            backoff_ms: 200,
            retryable: true,
        },
        ("system", "system_error", true, _) => Policy {
            retryable: false,
            backoff: BackoffPolicy::None,
            dlq: DlqPolicy::Never,
            alert: AlertLevel::Error,
            max_retries: 0,
            backoff_ms: 0,
        },
        _ => Policy::default(),
    }
}

fn apply_overrides(
    mut policy: Policy,
    overrides: &[PolicyOverride],
    domain: &str,
    event_type: Option<&str>,
    phase: Option<&str>,
    kind: &ErrorKind,
) -> Policy {
    let domain = normalize(domain).to_ascii_lowercase();
    let event_type = normalize_opt(event_type).unwrap_or("").to_ascii_lowercase();
    let phase = normalize_opt(phase).unwrap_or("").to_ascii_lowercase();

    let mut best_score = -1_i32;
    let mut best_override: Option<&PolicyOverride> = None;

    for override_item in overrides {
        if &override_item.kind != kind {
            continue;
        }

        let domain_match = match &override_item.domain {
            Some(value) => value.trim().eq_ignore_ascii_case(&domain),
            None => true,
        };
        if !domain_match {
            continue;
        }

        let event_match = match &override_item.event_type {
            Some(value) => value.trim().eq_ignore_ascii_case(&event_type),
            None => true,
        };
        if !event_match {
            continue;
        }

        let phase_match = match &override_item.phase {
            Some(value) => value.trim().eq_ignore_ascii_case(&phase),
            None => true,
        };
        if !phase_match {
            continue;
        }

        let score = (override_item.domain.is_some() as i32)
            + (override_item.event_type.is_some() as i32)
            + (override_item.phase.is_some() as i32)
            + 1;

        if score > best_score || (score == best_score && best_override.is_some()) {
            best_score = score;
            best_override = Some(override_item);
        }
    }

    if let Some(override_item) = best_override {
        if let Some(value) = override_item.retryable {
            policy.retryable = value;
        }
        if let Some(value) = override_item.backoff {
            policy.backoff = value;
        }
        if let Some(value) = override_item.dlq {
            policy.dlq = value;
        }
        if let Some(value) = override_item.alert {
            policy.alert = value;
        }
        if let Some(value) = override_item.max_retries {
            policy.max_retries = value;
        }
        if let Some(value) = override_item.backoff_ms {
            policy.backoff_ms = value;
        }
    }

    policy
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_policy_parser_failed_is_not_retryable() {
        let resolver = PolicyResolver::new(None);
        let decision =
            resolver.resolve_with_kind("engine", Some("parser"), Some("failed"), ErrorKind::Parser);
        assert!(!decision.policy.retryable);
        assert_eq!(decision.policy.dlq, DlqPolicy::Always);
    }

    #[test]
    fn overrides_take_precedence() {
        let cfg = PolicyConfig {
            overrides: vec![PolicyOverride {
                domain: Some("engine".to_string()),
                event_type: Some("download".to_string()),
                phase: Some("failed".to_string()),
                kind: ErrorKind::Download,
                retryable: Some(false),
                backoff: Some(BackoffPolicy::None),
                dlq: Some(DlqPolicy::Always),
                alert: Some(AlertLevel::Error),
                max_retries: Some(0),
                backoff_ms: Some(0),
            }],
        };

        let resolver = PolicyResolver::new(Some(&cfg));
        let decision = resolver.resolve_with_kind(
            "engine",
            Some("download"),
            Some("failed"),
            ErrorKind::Download,
        );

        assert!(!decision.policy.retryable);
        assert_eq!(decision.policy.dlq, DlqPolicy::Always);
        assert_eq!(decision.policy.max_retries, 0);
    }
}