celers-beat 0.2.0

Periodic task scheduler for CeleRS (Celery Beat equivalent)
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
# celers-beat

**Version: 0.2.0 | Status: [Stable] | Updated: 2026-03-27**

Periodic task scheduler for CeleRS, equivalent to Celery Beat. Schedule tasks to run at regular intervals or specific times using interval or crontab expressions.

## Overview

Production-ready task scheduler with comprehensive features:

**Core Scheduling:**
- **Interval Schedules**: Execute every N seconds
-**Crontab Schedules**: Unix cron-style scheduling with timezone support
-**Solar Schedules**: Sunrise/sunset/twilight/golden hour events
-**One-Time Schedules**: Run once at specific time (auto-cleanup)
-**Custom Schedules**: User-defined scheduling logic via closures
-**Composite Schedules**: Combine schedules with AND/OR logic

**Task Management:**
- **Persistent State**: Track execution history across restarts
-**Schedule Versioning**: Track and rollback schedule modifications
-**Task Dependencies**: Define execution order with dependency chains
-**Groups & Tags**: Organize tasks with hierarchical groups and tags
-**Batch Operations**: Efficient bulk add/remove operations
-**Priority Support**: High-priority task scheduling
-**Retry Policies**: Exponential backoff and fixed delay strategies

**Reliability & Monitoring:**
- **Crash Recovery**: Automatic recovery from scheduler crashes
-**Schedule Locking**: Prevent duplicate execution across instances
-**Alerting System**: Configurable alerts for failures and performance issues
-**Health Checks**: Task health validation and stuck detection
-**Conflict Detection**: Analyze overlapping schedules
-**Execution History**: Track success/failure/duration statistics

**Advanced Features:**
- **Calendar Integration**: Business hours, holidays, blackout periods
-**Webhook Alerts**: Send alerts to external systems
-**Jitter & Catch-up**: Avoid thundering herd, handle missed schedules
-**Schedule Indexing**: Fast O(log n) due task lookup

## Quick Start

### Installation

```toml
[dependencies]
celers-beat = "0.1"
celers = { version = "0.1", features = ["redis"] }
```

### Basic Example

```rust
use celers_beat::{BeatScheduler, Schedule, ScheduledTask};
use celers_broker_redis::RedisBroker;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create broker
    let broker = RedisBroker::new("redis://localhost:6379", "celery")?;

    // Create scheduler
    let mut scheduler = BeatScheduler::new();

    // Schedule task every 60 seconds
    let task = ScheduledTask::new(
        "send_report".to_string(),
        Schedule::interval(60)
    );
    scheduler.add_task("report", task);

    // Run scheduler
    scheduler.run(&broker).await?;

    Ok(())
}
```

## Schedule Types

### Interval Schedule

Execute tasks at fixed intervals:

```rust
use celers_beat::Schedule;

// Every 10 seconds
let schedule = Schedule::interval(10);

// Every 5 minutes
let schedule = Schedule::interval(300);

// Every hour
let schedule = Schedule::interval(3600);

// Every day
let schedule = Schedule::interval(86400);
```

### Crontab Schedule (Optional)

Unix cron-style scheduling:

```toml
[dependencies]
celers-beat = { version = "0.1", features = ["cron"] }
```

```rust
use celers_beat::Schedule;

// Every minute
let schedule = Schedule::crontab("*", "*", "*", "*", "*");

// Every hour at minute 0
let schedule = Schedule::crontab("0", "*", "*", "*", "*");

// Every day at midnight
let schedule = Schedule::crontab("0", "0", "*", "*", "*");

// Every Monday at 9 AM
let schedule = Schedule::crontab("0", "9", "1", "*", "*");

// Weekdays at 9 AM
let schedule = Schedule::crontab("0", "9", "1-5", "*", "*");
```

**Crontab format:**
```
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of week (0-6, 0=Sunday)
│ │ │ ┌───────────── day of month (1-31)
│ │ │ │ ┌───────────── month (1-12)
│ │ │ │ │
* * * * *
```

**Special characters:**
- `*` - Any value
- `*/n` - Every n units (e.g., `*/15` = every 15 minutes)
- `n-m` - Range (e.g., `9-17` = 9 AM to 5 PM)
- `n,m` - List (e.g., `1,15` = 1st and 15th)

### Solar Schedule (Optional)

Execute tasks at sunrise/sunset:

```toml
[dependencies]
celers-beat = { version = "0.1", features = ["solar"] }
```

```rust
use celers_beat::Schedule;

// Run at sunrise in Tokyo
let schedule = Schedule::solar("sunrise", 35.6762, 139.6503);

// Run at sunset in New York
let schedule = Schedule::solar("sunset", 40.7128, -74.0060);

// Run at sunrise in London
let schedule = Schedule::solar("sunrise", 51.5074, -0.1278);
```

**Supported events:**
- `"sunrise"` - Task runs at sunrise
- `"sunset"` - Task runs at sunset

**Notes:**
- Latitude/longitude must be in decimal degrees
- Times are calculated in UTC
- Next occurrence is searched up to 365 days ahead

## Scheduled Tasks

### Creating Tasks

```rust
use celers_beat::{ScheduledTask, Schedule};
use serde_json::json;

// Basic task
let task = ScheduledTask::new(
    "cleanup_old_files".to_string(),
    Schedule::interval(3600)
);

// Task with arguments
let task = ScheduledTask::new(
    "send_email".to_string(),
    Schedule::interval(300)
)
.with_args(vec![
    json!("admin@example.com"),
    json!("Daily Report"),
]);

// Task with keyword arguments
let mut kwargs = HashMap::new();
kwargs.insert("subject".to_string(), json!("Report"));
kwargs.insert("priority".to_string(), json!("high"));

let task = ScheduledTask::new(
    "send_email".to_string(),
    Schedule::interval(300)
)
.with_kwargs(kwargs);

// Disabled task (won't run)
let task = ScheduledTask::new(
    "maintenance".to_string(),
    Schedule::interval(3600)
)
.disabled();
```

### Task Structure

```rust
pub struct ScheduledTask {
    /// Task name (must match registered task)
    pub name: String,

    /// Execution schedule
    pub schedule: Schedule,

    /// Positional arguments
    pub args: Vec<serde_json::Value>,

    /// Keyword arguments
    pub kwargs: HashMap<String, serde_json::Value>,

    /// Task options (queue, priority, expires)
    pub options: TaskOptions,

    /// Last execution timestamp
    pub last_run_at: Option<DateTime<Utc>>,

    /// Total number of executions
    pub total_run_count: u64,

    /// Enable/disable flag
    pub enabled: bool,
}
```

## Beat Scheduler

### Basic Usage

```rust
use celers_beat::{BeatScheduler, Schedule, ScheduledTask};

let mut scheduler = BeatScheduler::new();

// Add tasks
scheduler.add_task("report", ScheduledTask::new(
    "generate_report".to_string(),
    Schedule::interval(3600)
));

scheduler.add_task("cleanup", ScheduledTask::new(
    "cleanup_temp_files".to_string(),
    Schedule::interval(86400)
));

// Run scheduler (blocks until shutdown)
scheduler.run(&broker).await?;
```

### Managing Tasks

```rust
// Add task
scheduler.add_task("my_task", task);

// Remove task
scheduler.remove_task("my_task");

// Enable/disable task
scheduler.enable_task("my_task");
scheduler.disable_task("my_task");

// Get task info
if let Some(task) = scheduler.get_task("my_task") {
    println!("Last run: {:?}", task.last_run_at);
    println!("Run count: {}", task.total_run_count);
}

// List all tasks
for (name, task) in scheduler.list_tasks() {
    println!("{}: enabled={}", name, task.enabled);
}
```

### Persistent Schedules

Load/save schedule state to persist across restarts:

```rust
use std::fs;

// Save state
let state = scheduler.export_state()?;
fs::write("schedule_state.json", state)?;

// Load state
let state = fs::read_to_string("schedule_state.json")?;
scheduler.import_state(&state)?;
```

## Configuration Examples

### Periodic Reports

```rust
// Daily report at midnight
let daily_report = ScheduledTask::new(
    "daily_report".to_string(),
    Schedule::crontab("0", "0", "*", "*", "*")
);

// Weekly report every Monday at 9 AM
let weekly_report = ScheduledTask::new(
    "weekly_report".to_string(),
    Schedule::crontab("0", "9", "1", "*", "*")
);

// Monthly report on 1st at midnight
let monthly_report = ScheduledTask::new(
    "monthly_report".to_string(),
    Schedule::crontab("0", "0", "*", "1", "*")
);
```

### Maintenance Tasks

```rust
// Cleanup every hour
let cleanup = ScheduledTask::new(
    "cleanup_temp_files".to_string(),
    Schedule::interval(3600)
);

// Database backup daily at 3 AM
let backup = ScheduledTask::new(
    "backup_database".to_string(),
    Schedule::crontab("0", "3", "*", "*", "*")
);

// Log rotation every 6 hours
let rotate_logs = ScheduledTask::new(
    "rotate_logs".to_string(),
    Schedule::interval(21600)
);
```

### Monitoring Tasks

```rust
// Health check every 30 seconds
let health_check = ScheduledTask::new(
    "health_check".to_string(),
    Schedule::interval(30)
);

// Metrics collection every 5 minutes
let collect_metrics = ScheduledTask::new(
    "collect_metrics".to_string(),
    Schedule::interval(300)
);

// Alert check every minute
let check_alerts = ScheduledTask::new(
    "check_alerts".to_string(),
    Schedule::interval(60)
);
```

## Schedule Builders and Templates

### Schedule Builder - Fluent API

Create schedules with a chainable, intuitive API:

```rust
use celers_beat::ScheduleBuilder;

// Simple intervals
let schedule = ScheduleBuilder::new()
    .every_n_minutes(30)
    .build();

// Business hours only (Mon-Fri, 9 AM - 5 PM)
let schedule = ScheduleBuilder::new()
    .every_n_minutes(15)
    .business_hours_only()
    .build();

// Weekends only
let schedule = ScheduleBuilder::new()
    .every_n_hours(2)
    .weekends_only()
    .build();

// Weekdays with timezone
let schedule = ScheduleBuilder::new()
    .every_n_hours(1)
    .weekdays_only()
    .in_timezone("America/New_York")
    .build();

// Business hours in Tokyo
let schedule = ScheduleBuilder::new()
    .every_n_minutes(30)
    .business_hours_only()
    .in_timezone("Asia/Tokyo")
    .build();
```

**Builder Methods:**
- `every_n_seconds(n)` - Every N seconds
- `every_n_minutes(n)` - Every N minutes
- `every_n_hours(n)` - Every N hours
- `every_n_days(n)` - Every N days
- `business_hours_only()` - Mon-Fri, 9 AM - 5 PM
- `weekdays_only()` - Mon-Fri
- `weekends_only()` - Sat-Sun
- `in_timezone(tz)` - Set timezone
- `build()` - Create the schedule

### Schedule Templates

Pre-built schedules for common patterns:

```rust
use celers_beat::ScheduleTemplates;

// Common intervals
ScheduleTemplates::every_minute();
ScheduleTemplates::every_5_minutes();
ScheduleTemplates::every_15_minutes();
ScheduleTemplates::every_30_minutes();
ScheduleTemplates::hourly();
ScheduleTemplates::every_2_hours();
ScheduleTemplates::every_6_hours();
ScheduleTemplates::every_12_hours();

// Daily schedules
ScheduleTemplates::daily_at_midnight();
ScheduleTemplates::daily_at_hour(3); // 3 AM

// Weekly schedules
ScheduleTemplates::weekdays_at(9, 0); // Weekdays at 9:00 AM
ScheduleTemplates::weekly_on_monday(9, 0); // Monday at 9:00 AM
ScheduleTemplates::weekend_mornings(); // Sat/Sun at 8 AM

// Monthly schedules
ScheduleTemplates::monthly_first_day(); // 1st of month
ScheduleTemplates::monthly_last_day(); // 28-31st of month

// Business hours
ScheduleTemplates::business_hours_hourly();
ScheduleTemplates::business_hours_every_15_minutes();

// Quarterly
ScheduleTemplates::quarterly(); // Jan/Apr/Jul/Oct 1st
```

**Practical Examples:**

```rust
// Health check monitoring
let task = ScheduledTask::new(
    "health_check".to_string(),
    ScheduleTemplates::every_minute()
);

// Daily report at 3 AM
let task = ScheduledTask::new(
    "daily_report".to_string(),
    ScheduleTemplates::daily_at_hour(3)
);

// Business hours API sync
let task = ScheduledTask::new(
    "api_sync".to_string(),
    ScheduleTemplates::business_hours_every_15_minutes()
);

// Weekend batch processing
let task = ScheduledTask::new(
    "weekend_batch".to_string(),
    ScheduleBuilder::new()
        .every_n_hours(2)
        .weekends_only()
        .build()
);
```

See `examples/schedule_builders.rs` for comprehensive demonstrations.

## Advanced Features

### Schedule Versioning

Track and rollback schedule modifications:

```rust
use celers_beat::ScheduledTask;

let mut task = ScheduledTask::new("my_task".to_string(), Schedule::interval(60));

// Update schedule (creates version 2)
task.update_schedule(Schedule::interval(120));

// Update again (creates version 3)
task.update_schedule(Schedule::interval(180));

// Rollback to version 2
task.rollback_to_version(1)?;

// View version history
for version in task.get_version_history() {
    println!("Version {} at {}", version.version, version.timestamp);
}
```

### Task Dependencies

Define task execution order with dependency chains:

```rust
let extract = ScheduledTask::new("extract_data".to_string(), Schedule::interval(3600))
    .with_group("etl");

let transform = ScheduledTask::new("transform_data".to_string(), Schedule::interval(3600))
    .with_dependencies(vec!["extract_data".to_string()])
    .with_group("etl");

let load = ScheduledTask::new("load_data".to_string(), Schedule::interval(3600))
    .with_dependencies(vec!["transform_data".to_string()])
    .with_group("etl");

scheduler.add_task(extract)?;
scheduler.add_task(transform)?;
scheduler.add_task(load)?;

// Validate dependencies (detects circular dependencies)
scheduler.validate_dependencies()?;

// Get dependency chain
let chain = scheduler.resolve_dependency_chain("load_data")?;
```

### Schedule Locking

Prevent duplicate task execution across multiple scheduler instances:

```rust
// Set unique instance ID
scheduler.set_instance_id("scheduler-001");

// Try to acquire lock
if scheduler.try_acquire_lock("my_task", 300)? {
    // Lock acquired, safe to execute

    // Renew lock if task runs longer
    scheduler.renew_lock("my_task", 300)?;

    // Release when done
    scheduler.release_lock("my_task")?;
}
```

### Alerting System

Configure alerts for task failures and performance issues:

```rust
use celers_beat::AlertConfig;

let alert_config = AlertConfig {
    enabled: true,
    consecutive_failures_threshold: 3,
    failure_rate_threshold: 0.5,  // 50%
    slow_execution_threshold_ms: Some(5000),
    alert_on_missed_schedule: true,
    alert_on_stuck_task: true,
};

let task = ScheduledTask::new("critical_task".to_string(), Schedule::interval(60))
    .with_alert_config(alert_config);

// Register alert callback
scheduler.on_alert(Arc::new(|alert| {
    eprintln!("[{}] {} - {}", alert.level, alert.task_name, alert.message);
}));

// Check alerts
scheduler.check_all_alerts();
let critical = scheduler.get_critical_alerts();
```

### Crash Recovery

Automatic recovery from scheduler crashes:

```rust
// On restart, detect interrupted tasks
let crashed_tasks = scheduler.detect_crashed_tasks();

// Recover from crash
scheduler.recover_from_crash()?;

// Get tasks ready for retry
let retry_tasks = scheduler.get_tasks_ready_for_crash_retry();
```

### Conflict Detection

Detect and analyze overlapping schedules:

```rust
// Detect conflicts (tasks scheduled too close together)
let conflicts = scheduler.detect_conflicts(60)?; // 60 second window

// Get high-severity conflicts only
let high_severity = scheduler.get_high_severity_conflicts(&conflicts);

for conflict in high_severity {
    println!("Conflict: {} and {} overlap by {}s",
        conflict.task1_name, conflict.task2_name, conflict.overlap_seconds);
}
```

### Groups and Tags

Organize tasks with groups and tags:

```rust
let task = ScheduledTask::new("report".to_string(), Schedule::interval(3600))
    .with_group("reports")
    .with_tags(vec!["daily".to_string(), "email".to_string()]);

// Query by group
let reports = scheduler.get_tasks_by_group("reports");

// Query by tag
let daily = scheduler.get_tasks_by_tag("daily");

// Bulk operations
scheduler.enable_group("reports");
scheduler.disable_tag("cleanup");
```

### Batch Operations

Efficient bulk task management:

```rust
// Add multiple tasks in one operation
let tasks = vec![
    ScheduledTask::new("task1".to_string(), Schedule::interval(60)),
    ScheduledTask::new("task2".to_string(), Schedule::interval(120)),
];
scheduler.add_tasks_batch(tasks)?;

// Remove multiple tasks
scheduler.remove_tasks_batch(&["task1", "task2"])?;
```

### Schedule Composition

Combine multiple schedules with AND/OR logic:

```rust
use celers_beat::CompositeSchedule;

// OR: Run when ANY schedule is due (earliest)
let composite_or = CompositeSchedule::or(vec![
    Schedule::interval(60),
    Schedule::interval(120),
]);

// AND: Run when ALL schedules are due (latest)
let composite_and = CompositeSchedule::and(vec![
    Schedule::interval(60),
    Schedule::interval(120),
]);
```

### Custom Schedules

Define custom scheduling logic:

```rust
use celers_beat::CustomSchedule;

let custom = CustomSchedule::new(
    "business_hours",
    |last_run| {
        let mut next = last_run.unwrap_or_else(Utc::now);
        // Custom logic to find next business hour
        // ...
        Ok(next)
    }
);
```

### Priority Scheduling

```rust
let task = ScheduledTask::new(
    "critical_task".to_string(),
    Schedule::interval(60)
);

task.options.priority = Some(9);  // Highest priority
```

### Custom Queue

```rust
let task = ScheduledTask::new(
    "background_task".to_string(),
    Schedule::interval(300)
);

task.options.queue = Some("low_priority".to_string());
```

### Task Expiration

```rust
let task = ScheduledTask::new(
    "time_sensitive".to_string(),
    Schedule::interval(60)
);

task.options.expires = Some(300);  // Expire after 5 minutes
```

### Conditional Execution

```rust
// Check if task is due before running
if task.is_due()? {
    println!("Task is due, executing...");
    // Execute task
} else {
    println!("Task not due yet");
}
```

## Production Deployment

### Systemd Service

```ini
[Unit]
Description=CeleRS Beat Scheduler
After=network.target redis.service

[Service]
Type=simple
User=celery
WorkingDirectory=/opt/celery
ExecStart=/opt/celery/beat
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target
```

### Docker

```dockerfile
FROM rust:1.70 AS builder
WORKDIR /app
COPY . .
RUN cargo build --release --bin beat

FROM debian:bullseye-slim
COPY --from=builder /app/target/release/beat /usr/local/bin/
CMD ["beat"]
```

### High Availability

**Important:** Only run **one** beat scheduler instance to avoid duplicate task execution!

```rust
// Use leader election or singleton pattern
use tokio::sync::Mutex;
use std::sync::Arc;

let lock = Arc::new(Mutex::new(()));

// Acquire lock before starting
let _guard = lock.lock().await;
scheduler.run(&broker).await?;
```

## Monitoring

### Scheduled Task Metrics

```rust
// Track execution
for (name, task) in scheduler.list_tasks() {
    println!("Task: {}", name);
    println!("  Last run: {:?}", task.last_run_at);
    println!("  Total runs: {}", task.total_run_count);
    println!("  Enabled: {}", task.enabled);
}
```

### Health Checks

```rust
use tokio::time::{interval, Duration};

// Periodic health check
let mut ticker = interval(Duration::from_secs(60));
loop {
    ticker.tick().await;

    // Check scheduler is running
    if !scheduler.is_running() {
        eprintln!("WARNING: Scheduler not running!");
        // Alert or restart
    }
}
```

## Best Practices

### 1. Single Scheduler Instance

```rust
// ❌ Bad: Multiple schedulers (duplicates tasks)
// Worker 1: scheduler.run()
// Worker 2: scheduler.run()

// ✅ Good: Single scheduler instance
// Beat server: scheduler.run()
// Workers: Only execute tasks
```

### 2. Persistent State

```rust
// Save state on shutdown
use tokio::signal;

tokio::select! {
    _ = scheduler.run(&broker) => {}
    _ = signal::ctrl_c() => {
        println!("Saving scheduler state...");
        let state = scheduler.export_state()?;
        fs::write("state.json", state)?;
    }
}
```

### 3. Timezone Handling

```rust
// Always use UTC internally
use chrono::Utc;

let now = Utc::now();
let next_run = schedule.next_run(Some(now))?;

// Convert to local time for display
use chrono_tz::America::New_York;
let local = next_run.with_timezone(&New_York);
println!("Next run: {}", local);
```

#### Comprehensive Timezone Utilities

The crate provides comprehensive timezone utilities for working with schedules across different timezones:

```rust
use celers_beat::TimezoneUtils;

// Automatic system timezone detection
let system_tz = TimezoneUtils::detect_system_timezone();

// Validate timezone names
assert!(TimezoneUtils::is_valid_timezone("America/New_York"));

// List and search timezones
let all_timezones = TimezoneUtils::list_all_timezones(); // 600+ timezones
let us_zones = TimezoneUtils::search_timezones("america");

// Get current time in multiple timezones
let zones = vec!["America/New_York", "Europe/London", "Asia/Tokyo"];
let times = TimezoneUtils::current_time_in_zones(&zones);

// Check DST status
let is_dst = TimezoneUtils::is_dst_active("America/New_York", None)?;

// Get UTC offset
let offset = TimezoneUtils::get_utc_offset("Asia/Tokyo", None)?;

// Get detailed timezone info
let info = TimezoneUtils::get_timezone_info("Europe/London", None)?;
println!("{}", info); // Europe/London (UTC+0.0h, GMT, DST: No): ...

// Convert between timezones
let tokyo_time = TimezoneUtils::convert_between_timezones(
    Utc::now(),
    "America/New_York",
    "Asia/Tokyo"
)?;

// Get common timezone abbreviations
let abbrevs = TimezoneUtils::get_common_timezone_abbreviations();
let ny_tz = abbrevs.get("EST"); // Returns "America/New_York"

// Calculate time until next occurrence in timezone
let duration = TimezoneUtils::time_until_next_occurrence(9, 0, "America/New_York")?;
```

See `examples/timezone_utilities.rs` for a comprehensive demonstration.

### 4. Error Handling

```rust
// Graceful error handling
loop {
    match scheduler.tick(&broker).await {
        Ok(executed) => {
            println!("Executed {} tasks", executed);
        }
        Err(e) => {
            eprintln!("Scheduler error: {}", e);
            // Log but continue
        }
    }

    tokio::time::sleep(Duration::from_secs(1)).await;
}
```

### 5. Task Idempotency

```rust
// Ensure tasks are idempotent (safe to run multiple times)
registry.register("generate_report", |args| async move {
    let report_id = generate_unique_id();

    // Check if already generated
    if report_exists(report_id).await? {
        return Ok("Already generated".to_string());
    }

    // Generate report
    generate_report(report_id).await?;
    Ok(format!("Generated report {}", report_id))
});
```

## Troubleshooting

### Tasks not executing

**Check:**
1. Scheduler is running: `scheduler.is_running()`
2. Task is enabled: `task.enabled == true`
3. Schedule is correct: `task.schedule.next_run()`
4. Broker connection: `broker.ping()`

### Duplicate executions

**Cause:** Multiple scheduler instances running
**Solution:** Ensure only one scheduler instance

### Missed schedules

**Cause:** Scheduler was down during scheduled time
**Solution:** Implement catchup logic or use persistent state

### Timezone issues

**Cause:** Mixing UTC and local time
**Solution:** Always use UTC internally, convert for display only

## Performance

### Scheduling Overhead

| Schedule Type | Overhead | Memory |
|--------------|----------|--------|
| Interval | <1ms | ~100B per task |
| Crontab | <5ms | ~200B per task |
| Solar | <10ms | ~300B per task |

### Scalability

- **Tasks:** 10,000+ scheduled tasks
- **Precision:** 1-second granularity
- **Latency:** <10ms schedule evaluation

## Comparison with Celery Beat

| Feature | Celery Beat | CeleRS Beat |
|---------|-------------|-------------|
| Interval schedules |||
| Crontab schedules || ✅ (with timezone support) |
| Solar schedules || ✅ (with twilight & golden hour) |
| One-time schedules |||
| Custom schedules |||
| Schedule versioning |||
| Task dependencies |||
| Crash recovery |||
| Alerting system |||
| Schedule locking |||
| Conflict detection |||
| Business calendars |||
| Persistent state | File/DB | JSON (auto-save) |
| Performance | ~100 tasks/sec | ~1000 tasks/sec |
| Memory | 50MB+ | <10MB |

## See Also

- **Worker**: `celers-worker` - Task execution runtime
- **Broker**: `celers-broker-redis` - Message broker
- **Core**: `celers-core` - Task registry

## Testing

**312 tests passing** (unit + doc tests across 12 focused modules)

## License

Apache-2.0