camel-component-cron 0.24.0

Cron component for rust-camel — triggers Routes on Unix cron schedules
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
//! cron component for rust-camel — fires `Exchange` events on Unix cron schedules.
//!
//! Main types: `CronComponent`, `CronConsumer`, `CronConfig`, `CronEndpoint`.
//! URI format: `cron:name?schedule=0 2 * * *&timeZone=UTC`.
//!
//! # Features
//!
//! - **Unix 5-field cron**: `min hour dom month dow`
//! - **Timezone-aware**: UTC default, configurable via `timeZone` param
//! - **Misfire skip**: missed schedules during downtime are not replayed
//! - **SPI-backed**: delegates scheduling to `CronService` (default: `TokioCronService`)

mod tokio_impl;

pub use tokio_impl::TokioCronService;

use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use tracing::debug;

use camel_component_api::{
    BoxProcessor, CamelError, Component, ComponentContext, Consumer, ConsumerContext, CronCallback,
    CronFire, CronSchedule, CronService, Endpoint, ProducerContext, RuntimeObservability,
    UriConfig,
};
use camel_component_api::{Exchange, Message};
use chrono_tz::Tz;

// ---------------------------------------------------------------------------
// CronConfig
// ---------------------------------------------------------------------------

/// Configuration parsed from a cron URI.
///
/// Format: `cron:name?schedule=0 2 * * *&timeZone=UTC&includeMetadata=true`
#[derive(Debug, Clone, UriConfig)]
#[uri_scheme = "cron"]
#[uri_config(skip_impl, crate = "camel_component_api")]
pub struct CronConfig {
    /// Cron trigger name (the path portion of the URI).
    pub name: String,

    /// 5-field Unix cron expression.
    #[uri_param(name = "schedule")]
    pub schedule: String,

    /// IANA timezone. Default: UTC.
    #[uri_param(name = "timeZone", default = "UTC")]
    time_zone_str: String,

    /// Whether to include cron metadata headers in exchanges.
    #[uri_param(name = "includeMetadata", default = "true")]
    include_metadata: bool,
}

impl CronConfig {
    /// Parsed timezone.
    pub fn time_zone(&self) -> Result<Tz, CamelError> {
        self.time_zone_str.parse::<Tz>().map_err(|e| {
            CamelError::EndpointCreationFailed(format!(
                "invalid timeZone '{}': {}",
                self.time_zone_str, e
            ))
        })
    }

    /// Whether metadata headers are included.
    pub fn include_metadata(&self) -> bool {
        self.include_metadata
    }

    /// Normalize a 5-field Unix cron expression to the `cron` crate's
    /// 6-field format (prepend `0` for the seconds field).
    ///
    /// `"0 2 * * *"` → `"0 0 2 * * *"` (sec=0, min=0, hour=2)
    fn normalized_cron_expression(&self) -> String {
        format!("0 {}", self.schedule)
    }

    /// Validate: name non-empty, schedule is valid 5-field cron, timezone parseable.
    pub fn validate(&self) -> Result<(), CamelError> {
        if self.name.is_empty() {
            return Err(CamelError::EndpointCreationFailed(
                "cron name must not be empty".to_string(),
            ));
        }
        if self.schedule.is_empty() {
            return Err(CamelError::EndpointCreationFailed(
                "cron schedule must not be empty".to_string(),
            ));
        }
        // We restrict to 5-field Unix by counting fields.
        let field_count = self.schedule.split_whitespace().count();
        if field_count != 5 {
            return Err(CamelError::EndpointCreationFailed(format!(
                "cron schedule must be 5-field Unix format (min hour dom month dow), got {} field(s): '{}'",
                field_count, self.schedule
            )));
        }
        // The `cron` crate expects 6-7 fields (sec min hour dom month dow [year]).
        // Normalize our 5-field Unix expression before parsing.
        self.normalized_cron_expression()
            .parse::<cron::Schedule>()
            .map_err(|e| {
                CamelError::EndpointCreationFailed(format!(
                    "invalid cron expression '{}': {}",
                    self.schedule, e
                ))
            })?;
        // Validate timezone parses
        self.time_zone()?;
        Ok(())
    }
}

impl UriConfig for CronConfig {
    fn scheme() -> &'static str {
        "cron"
    }

    fn from_uri(uri: &str) -> Result<Self, CamelError> {
        let parts = camel_component_api::parse_uri(uri)?;
        Self::from_components(parts)
    }

    fn from_components(parts: camel_component_api::UriComponents) -> Result<Self, CamelError> {
        let mut config = Self::parse_uri_components(parts)?;
        // Accept `+` as space in the schedule expression (Apache Camel convention).
        // The global URI parser treats `+` as a literal plus; cron expressions
        // never use `+`, so this component-local normalization is safe and
        // greatly improves URI readability:
        //   cron:t?schedule=0+2+*+*+*  instead of  cron:t?schedule=0%202%20*%20*%20*
        config.schedule = config.schedule.replace('+', " ");
        CronConfig::validate(&config)?;
        Ok(config)
    }

    fn validate(self) -> Result<Self, CamelError> {
        CronConfig::validate(&self)?;
        Ok(self)
    }
}

// ---------------------------------------------------------------------------
// CronEndpoint
// ---------------------------------------------------------------------------

/// Endpoint for the `cron:` scheme.
pub struct CronEndpoint {
    uri: String,
    config: CronConfig,
    service: Arc<dyn CronService>,
}

impl CronEndpoint {
    pub fn new(uri: String, config: CronConfig, service: Arc<dyn CronService>) -> Self {
        Self {
            uri,
            config,
            service,
        }
    }
}

impl Endpoint for CronEndpoint {
    fn uri(&self) -> &str {
        &self.uri
    }

    fn create_consumer(
        &self,
        _rt: Arc<dyn RuntimeObservability>,
    ) -> Result<Box<dyn Consumer>, CamelError> {
        Ok(Box::new(CronConsumer::new(
            self.config.clone(),
            self.service.clone(),
        )))
    }

    fn create_producer(
        &self,
        _rt: Arc<dyn RuntimeObservability>,
        _ctx: &ProducerContext,
    ) -> Result<BoxProcessor, CamelError> {
        Err(CamelError::EndpointCreationFailed(
            "cron: component is consumer-only".to_string(),
        ))
    }
}

// ---------------------------------------------------------------------------
// CronConsumer
// ---------------------------------------------------------------------------

/// Consumer that fires Exchanges on a cron schedule.
pub struct CronConsumer {
    config: CronConfig,
    service: Arc<dyn CronService>,
    started: AtomicBool,
}

impl CronConsumer {
    /// Create a new CronConsumer with the given config and scheduling service.
    pub fn new(config: CronConfig, service: Arc<dyn CronService>) -> Self {
        Self {
            config,
            service,
            started: AtomicBool::new(false),
        }
    }

    /// Test helper: force the started flag to true.
    #[cfg(test)]
    pub(crate) fn mark_started_for_test(&self) {
        self.started.store(true, Ordering::SeqCst);
    }
}

#[async_trait::async_trait]
impl Consumer for CronConsumer {
    async fn start(&mut self, context: ConsumerContext) -> Result<(), CamelError> {
        self.started
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .map_err(|_| {
                CamelError::EndpointCreationFailed("cron consumer already started".to_string())
            })?;

        CronConfig::validate(&self.config)?;
        let config = self.config.clone();

        let schedule = CronSchedule {
            expression: config.normalized_cron_expression(),
            time_zone: config.time_zone()?,
            trigger_id: config.name.clone(),
            route_id: Some(context.route_id().to_string()),
        };

        let include_metadata = config.include_metadata();
        let trigger_name = config.name.clone();
        let trigger_schedule = config.schedule.clone();
        let time_zone_str = config.time_zone_str.clone();

        // Build the callback that submits Exchanges to the pipeline.
        let ctx = context.clone();
        let callback: CronCallback = Arc::new(move |fire: CronFire| {
            let ctx = ctx.clone();
            let name = trigger_name.clone();
            let sched = trigger_schedule.clone();
            let tz = time_zone_str.clone();
            Box::pin(async move {
                let mut exchange = Exchange::new(Message::default());
                if include_metadata {
                    exchange.input.set_header("CamelCronName", name);
                    exchange.input.set_header("CamelCronSchedule", sched);
                    exchange.input.set_header("CamelCronTimezone", tz);
                    exchange
                        .input
                        .set_header("CamelCronScheduledTime", fire.scheduled_at.to_rfc3339());
                    exchange
                        .input
                        .set_header("CamelCronFiredTime", fire.fired_at.to_rfc3339());
                    exchange
                        .input
                        .set_header("CamelCronCounter", fire.counter.to_string());
                }
                ctx.send(exchange).await
            })
        });

        let cancel_token = context.cancel_token();
        let trigger_id = schedule.trigger_id.clone();

        // Delegate to the CronService. On error, propagate to Route supervision.
        let result = self.service.run(&schedule, callback, cancel_token).await;

        self.started.store(false, Ordering::SeqCst);

        debug!(trigger = trigger_id, "cron consumer stopped");
        result
    }

    async fn stop(&mut self) -> Result<(), CamelError> {
        // The cancel token (held by ConsumerContext) triggers service::run to return.
        // started flag is reset in start() after run returns.
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// CronComponent
// ---------------------------------------------------------------------------

/// Component for the `cron:` scheme. Factory for `CronEndpoint`s.
///
/// By default uses `TokioCronService`. Inject a custom `CronService` via
/// [`CronComponent::with_service`] for alternative backends.
pub struct CronComponent {
    service: Arc<dyn CronService>,
}

impl Default for CronComponent {
    fn default() -> Self {
        Self::new()
    }
}

impl CronComponent {
    /// Create with the default `TokioCronService`.
    pub fn new() -> Self {
        Self {
            service: Arc::new(TokioCronService),
        }
    }

    /// Create with a custom `CronService` implementation.
    pub fn with_service(service: Arc<dyn CronService>) -> Self {
        Self { service }
    }
}

impl Component for CronComponent {
    fn scheme(&self) -> &str {
        "cron"
    }

    fn create_endpoint(
        &self,
        uri: &str,
        _ctx: &dyn ComponentContext,
    ) -> Result<Box<dyn Endpoint>, CamelError> {
        let config = CronConfig::from_uri(uri)?;
        Ok(Box::new(CronEndpoint::new(
            uri.to_string(),
            config,
            self.service.clone(),
        )))
    }
}

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

    #[test]
    fn test_cron_config_basic() {
        let config = CronConfig::from_uri("cron:nightly?schedule=0%202%20*%20*%20*").unwrap();
        assert_eq!(config.name, "nightly");
        assert_eq!(config.schedule, "0 2 * * *");
        assert_eq!(config.time_zone_str, "UTC");
        assert!(config.include_metadata);
    }

    #[test]
    fn test_cron_config_with_timezone() {
        let config =
            CronConfig::from_uri("cron:morning?schedule=0%209%20*%20*%201&timeZone=America/Lima")
                .unwrap();
        assert_eq!(config.name, "morning");
        assert_eq!(config.time_zone().unwrap(), chrono_tz::Tz::America__Lima);
    }

    #[test]
    fn test_cron_config_percent_encoded_spaces() {
        // URI parser should decode %20 to space
        let config = CronConfig::from_uri("cron:test?schedule=0%202%20*%20*%20*").unwrap();
        assert_eq!(config.schedule, "0 2 * * *");
        config.validate().unwrap();
    }

    #[test]
    fn test_cron_config_plus_as_space() {
        // Apache Camel convention: `+` as space separator in cron expressions
        let config = CronConfig::from_uri("cron:test?schedule=0+2+*+*+*").unwrap();
        assert_eq!(config.schedule, "0 2 * * *");
        config.validate().unwrap();
    }

    #[test]
    fn test_rejects_empty_name() {
        let err = CronConfig::from_uri("cron:?schedule=0%202%20*%20*%20*").unwrap_err();
        assert!(
            err.to_string().contains("must not be empty"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn test_rejects_missing_schedule() {
        let result = CronConfig::from_uri("cron:test");
        // schedule is required — should fail at URI parse or validate
        assert!(result.is_err() || result.unwrap().validate().is_err());
    }

    #[test]
    fn test_rejects_six_field_cron() {
        let err = CronConfig::from_uri("cron:test?schedule=0%200%202%20*%20*%20*").unwrap_err();
        assert!(err.to_string().contains("5-field"));
    }

    #[test]
    fn test_rejects_invalid_cron() {
        let err = CronConfig::from_uri("cron:test?schedule=99%2099%2099%2099%2099").unwrap_err();
        assert!(
            err.to_string().contains("invalid cron"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn test_rejects_invalid_timezone() {
        let err = CronConfig::from_uri("cron:test?schedule=0%202%20*%20*%20*&timeZone=NotAZone")
            .unwrap_err();
        assert!(
            err.to_string().contains("invalid timeZone"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn test_valid_expressions() {
        // Each URI must encode a valid 5-field Unix cron expression.
        // 1. "0 2 * * *"        → daily at 02:00
        // 2. "*/5 * * * *"      → every 5 minutes
        // 3. "0 9 * * 1"        → Mondays at 09:00
        for expr in &[
            "0%202%20*%20*%20*",
            "*%2F5%20*%20*%20*%20*",
            "0%209%20*%20*%201",
        ] {
            let config = CronConfig::from_uri(&format!("cron:t?schedule={}", expr)).unwrap();
            config
                .validate()
                .unwrap_or_else(|e| panic!("'{}' should be valid: {}", expr, e));
        }
    }

    // --- Mock CronService for consumer tests (fires every 10ms) ---

    struct FastCronService;
    #[async_trait::async_trait]
    impl CronService for FastCronService {
        async fn run(
            &self,
            _schedule: &CronSchedule,
            callback: CronCallback,
            cancel: tokio_util::sync::CancellationToken,
        ) -> Result<(), CamelError> {
            let mut counter = 0u64;
            loop {
                tokio::select! {
                    _ = cancel.cancelled() => return Ok(()),
                    _ = tokio::time::sleep(tokio::time::Duration::from_millis(10)) => {
                        counter += 1;
                        let now = chrono::Utc::now();
                        let fire = CronFire {
                            scheduled_at: now,
                            fired_at: now,
                            counter,
                        };
                        callback(fire).await?;
                    }
                }
            }
        }
    }

    // --- CronConsumer tests ---

    #[tokio::test]
    async fn test_double_start_returns_error() {
        let config = CronConfig::from_uri("cron:test?schedule=0 2 * * *").unwrap();
        let mut consumer = CronConsumer::new(config, Arc::new(FastCronService));
        consumer.mark_started_for_test();

        let (tx, _rx) = tokio::sync::mpsc::channel(16);
        let cancel = tokio_util::sync::CancellationToken::new();
        let ctx = ConsumerContext::new(tx, cancel, "test-route".to_string());
        let result = consumer.start(ctx).await;
        assert!(result.is_err(), "double-start should error");
    }

    #[tokio::test]
    async fn test_consumer_fires_exchange() {
        let config = CronConfig::from_uri("cron:test?schedule=* * * * *").unwrap();
        let (tx, mut rx) = tokio::sync::mpsc::channel(16);
        let cancel = tokio_util::sync::CancellationToken::new();
        let ctx = ConsumerContext::new(tx, cancel.clone(), "test-route".to_string());
        let mut consumer = CronConsumer::new(config, Arc::new(FastCronService));

        let cancel2 = cancel.clone();
        let handle = tokio::spawn(async move { consumer.start(ctx).await });

        // FastCronService fires every 10ms — should get an exchange quickly
        let exchange = tokio::time::timeout(tokio::time::Duration::from_secs(2), rx.recv()).await;
        assert!(exchange.is_ok(), "should have received an exchange");

        cancel2.cancel();
        let _ = handle.await;
    }

    #[tokio::test]
    async fn test_consumer_metadata_headers() {
        let config = CronConfig::from_uri("cron:test?schedule=* * * * *").unwrap();
        let (tx, mut rx) = tokio::sync::mpsc::channel(16);
        let cancel = tokio_util::sync::CancellationToken::new();
        let ctx = ConsumerContext::new(tx, cancel.clone(), "test-route".to_string());
        let mut consumer = CronConsumer::new(config, Arc::new(FastCronService));

        let cancel2 = cancel.clone();
        tokio::spawn(async move { consumer.start(ctx).await });

        let envelope = tokio::time::timeout(tokio::time::Duration::from_secs(2), rx.recv())
            .await
            .unwrap()
            .unwrap();

        let headers = &envelope.exchange.input.headers;
        assert!(headers.contains_key("CamelCronName"));
        assert!(headers.contains_key("CamelCronSchedule"));
        assert!(headers.contains_key("CamelCronTimezone"));
        assert!(headers.contains_key("CamelCronScheduledTime"));
        assert!(headers.contains_key("CamelCronFiredTime"));
        assert!(headers.contains_key("CamelCronCounter"));

        cancel2.cancel();
    }

    #[tokio::test]
    async fn test_include_metadata_false_omits_headers() {
        let config =
            CronConfig::from_uri("cron:test?schedule=* * * * *&includeMetadata=false").unwrap();
        let (tx, mut rx) = tokio::sync::mpsc::channel(16);
        let cancel = tokio_util::sync::CancellationToken::new();
        let ctx = ConsumerContext::new(tx, cancel.clone(), "test-route".to_string());
        let mut consumer = CronConsumer::new(config, Arc::new(FastCronService));

        let cancel2 = cancel.clone();
        tokio::spawn(async move { consumer.start(ctx).await });

        let envelope = tokio::time::timeout(tokio::time::Duration::from_secs(2), rx.recv())
            .await
            .unwrap()
            .unwrap();

        assert!(
            envelope.exchange.input.headers.is_empty(),
            "no headers when includeMetadata=false"
        );

        cancel2.cancel();
    }

    #[tokio::test]
    async fn test_callback_error_propagates_to_start() {
        // If send fails (channel closed), start() should return Err.
        let config = CronConfig::from_uri("cron:test?schedule=* * * * *").unwrap();
        let (tx, rx) = tokio::sync::mpsc::channel(16);
        // Drop receiver immediately so send fails
        drop(rx);
        let cancel = tokio_util::sync::CancellationToken::new();
        let ctx = ConsumerContext::new(tx, cancel, "test-route".to_string());
        let mut consumer = CronConsumer::new(config, Arc::new(FastCronService));

        let result =
            tokio::time::timeout(tokio::time::Duration::from_secs(2), consumer.start(ctx)).await;

        // Should complete (not timeout) with an error
        let inner = result.expect("should not timeout");
        assert!(
            inner.is_err(),
            "closed channel should propagate error to start()"
        );
    }

    // --- CronComponent tests ---

    #[test]
    fn test_cron_component_scheme() {
        let comp = CronComponent::new();
        assert_eq!(comp.scheme(), "cron");
    }

    #[test]
    fn test_cron_component_creates_endpoint() {
        let comp = CronComponent::new();
        let ctx = camel_component_api::NoOpComponentContext;
        let endpoint = comp
            .create_endpoint("cron:nightly?schedule=0 2 * * *", &ctx)
            .unwrap();
        assert_eq!(endpoint.uri(), "cron:nightly?schedule=0 2 * * *");
    }

    #[test]
    fn test_cron_component_rejects_invalid_schedule() {
        let comp = CronComponent::new();
        let ctx = camel_component_api::NoOpComponentContext;
        let result = comp.create_endpoint("cron:test?schedule=invalid", &ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_cron_component_with_custom_service() {
        let custom_service: Arc<dyn CronService> = Arc::new(TokioCronService);
        let comp = CronComponent::with_service(custom_service);
        assert_eq!(comp.scheme(), "cron");
    }
}