celers-backend-redis 0.2.0

Redis result backend for CeleRS
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
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
//! Redis result backend implementation
//!
//! Contains the [`RedisResultBackend`] struct with all inherent methods for
//! Redis-based task result storage, including builder methods, convenience
//! methods, query operations, transaction support, and archival.

use chrono::{DateTime, Utc};
use futures_util::stream::StreamExt;
use redis::{AsyncCommands, Client};
use std::time::Duration;
use uuid::Uuid;

use crate::query::TaskQuery;
use crate::result_backend_trait::{ResultBackend, ResultStream};
use crate::stats::{BackendStats, BatchOperationResult, PoolStats, StateCount, TaskSummary};
use crate::types::{BackendError, ProgressInfo, Result, TaskMeta, TaskResult, TaskTtlConfig};
use crate::{cache, chunking, compression, encryption, metrics};

/// Redis result backend implementation
#[derive(Clone)]
pub struct RedisResultBackend {
    pub(crate) client: Client,
    pub(crate) key_prefix: String,
    pub(crate) compression_config: compression::CompressionConfig,
    pub(crate) encryption_config: encryption::EncryptionConfig,
    pub(crate) metrics: metrics::BackendMetrics,
    pub(crate) cache: cache::ResultCache,
    pub(crate) ttl_config: TaskTtlConfig,
    pub(crate) compression_stats: celers_protocol::compression::CompressionStats,
    pub(crate) chunking_config: chunking::ChunkingConfig,
    pub(crate) chunker: chunking::ResultChunker,
}

impl RedisResultBackend {
    pub fn new(url: &str) -> Result<Self> {
        let client = Client::open(url).map_err(|e| {
            BackendError::Connection(format!("Failed to create Redis client: {}", e))
        })?;

        Ok(Self {
            client,
            key_prefix: "celery-task-meta-".to_string(),
            compression_config: compression::CompressionConfig::default(),
            encryption_config: encryption::EncryptionConfig::disabled(),
            metrics: metrics::BackendMetrics::new(),
            cache: cache::ResultCache::new(cache::CacheConfig::default()),
            ttl_config: TaskTtlConfig::new(),
            compression_stats: celers_protocol::compression::CompressionStats::default(),
            chunking_config: chunking::ChunkingConfig::default(),
            chunker: chunking::ResultChunker::new(chunking::ChunkingConfig::default()),
        })
    }

    pub fn with_prefix(mut self, prefix: String) -> Self {
        self.key_prefix = prefix;
        self
    }

    /// Configure result compression
    pub fn with_compression(mut self, config: compression::CompressionConfig) -> Self {
        self.compression_config = config;
        self
    }

    /// Disable result compression
    pub fn without_compression(mut self) -> Self {
        self.compression_config = compression::CompressionConfig::disabled();
        self
    }

    /// Get the compression configuration
    pub fn compression_config(&self) -> &compression::CompressionConfig {
        &self.compression_config
    }

    /// Configure metrics collection
    pub fn with_metrics(mut self, metrics: metrics::BackendMetrics) -> Self {
        self.metrics = metrics;
        self
    }

    /// Disable metrics collection
    pub fn without_metrics(mut self) -> Self {
        self.metrics = metrics::BackendMetrics::disabled();
        self
    }

    /// Get the metrics collector
    pub fn metrics(&self) -> &metrics::BackendMetrics {
        &self.metrics
    }

    /// Configure result cache
    pub fn with_cache(mut self, cache: cache::ResultCache) -> Self {
        self.cache = cache;
        self
    }

    /// Disable result cache
    pub fn without_cache(mut self) -> Self {
        self.cache = cache::ResultCache::disabled();
        self
    }

    /// Get the result cache
    pub fn cache(&self) -> &cache::ResultCache {
        &self.cache
    }

    /// Configure result encryption
    pub fn with_encryption(mut self, config: encryption::EncryptionConfig) -> Self {
        self.encryption_config = config;
        self
    }

    /// Disable result encryption
    pub fn without_encryption(mut self) -> Self {
        self.encryption_config = encryption::EncryptionConfig::disabled();
        self
    }

    /// Get the encryption configuration
    pub fn encryption_config(&self) -> &encryption::EncryptionConfig {
        &self.encryption_config
    }

    /// Configure per-task-type TTL
    pub fn with_ttl_config(mut self, config: TaskTtlConfig) -> Self {
        self.ttl_config = config;
        self
    }

    /// Get the TTL configuration
    pub fn ttl_config(&self) -> &TaskTtlConfig {
        &self.ttl_config
    }

    /// Get a mutable reference to the TTL configuration
    pub fn ttl_config_mut(&mut self) -> &mut TaskTtlConfig {
        &mut self.ttl_config
    }

    /// Configure result chunking for large payloads
    pub fn with_chunking(mut self, config: chunking::ChunkingConfig) -> Self {
        self.chunking_config = config.clone();
        self.chunker = chunking::ResultChunker::new(config);
        self
    }

    /// Disable result chunking
    pub fn without_chunking(mut self) -> Self {
        let config = chunking::ChunkingConfig::disabled();
        self.chunking_config = config.clone();
        self.chunker = chunking::ResultChunker::new(config);
        self
    }

    /// Get the chunking configuration
    pub fn chunking_config(&self) -> &chunking::ChunkingConfig {
        &self.chunking_config
    }

    /// Get the result chunker
    pub fn chunker(&self) -> &chunking::ResultChunker {
        &self.chunker
    }

    /// Get the compression statistics
    pub fn compression_stats(&self) -> &celers_protocol::compression::CompressionStats {
        &self.compression_stats
    }

    pub(crate) fn task_key(&self, task_id: Uuid) -> String {
        format!("{}{}", self.key_prefix, task_id)
    }

    pub(crate) fn chord_key(&self, chord_id: Uuid) -> String {
        format!("celery-chord-{}", chord_id)
    }

    pub(crate) fn chord_counter_key(&self, chord_id: Uuid) -> String {
        format!("celery-chord-counter-{}", chord_id)
    }

    /// Create a stream of results for the given task IDs
    ///
    /// This allows async iteration over results without loading them all at once.
    ///
    /// # Arguments
    /// * `task_ids` - Task IDs to stream results for
    /// * `batch_size` - Number of results to fetch per batch (default: 10)
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    /// use futures_util::StreamExt;
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_ids = vec![Uuid::new_v4(), Uuid::new_v4()];
    ///
    /// let mut stream = backend.stream_results(task_ids, 10);
    /// while let Some(result) = stream.next().await {
    ///     match result {
    ///         Ok((task_id, Some(meta))) => println!("Task {}: {:?}", task_id, meta.result),
    ///         Ok((task_id, None)) => println!("Task {} not found", task_id),
    ///         Err(e) => eprintln!("Error: {}", e),
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn stream_results(&mut self, task_ids: Vec<Uuid>, batch_size: usize) -> ResultStream {
        let backend = self.clone();
        let stream = futures_util::stream::iter(task_ids)
            .chunks(batch_size)
            .then(move |chunk| {
                let mut backend = backend.clone();
                async move {
                    let results = backend.get_results_batch(&chunk).await?;
                    Ok::<_, BackendError>(chunk.into_iter().zip(results).collect::<Vec<_>>())
                }
            })
            .flat_map(|batch_result| {
                futures_util::stream::iter(match batch_result {
                    Ok(batch) => batch.into_iter().map(Ok).collect(),
                    Err(e) => vec![Err(e)],
                })
            });

        Box::pin(stream)
    }

    /// Health check: Verify Redis connectivity
    ///
    /// Performs a simple PING command to verify the backend is operational.
    ///
    /// # Returns
    /// * `Ok(true)` - Backend is healthy and responsive
    /// * `Ok(false)` - Backend is not responding correctly
    /// * `Err(_)` - Connection or other error occurred
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    ///
    /// if backend.health_check().await? {
    ///     println!("Backend is healthy");
    /// } else {
    ///     eprintln!("Backend health check failed");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn health_check(&mut self) -> Result<bool> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let result: String = redis::cmd("PING").query_async(&mut conn).await?;
        Ok(result == "PONG")
    }

    /// Scan keys matching a pattern using SCAN (production-safe, non-blocking)
    ///
    /// Uses Redis SCAN command instead of KEYS for safe iteration in production.
    pub(crate) async fn scan_keys(&mut self, pattern: &str) -> Result<Vec<String>> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let mut all_keys = Vec::new();
        let mut cursor = 0u64;

        loop {
            let (next_cursor, keys): (u64, Vec<String>) = redis::cmd("SCAN")
                .arg(cursor)
                .arg("MATCH")
                .arg(pattern)
                .arg("COUNT")
                .arg(100) // Scan 100 keys per iteration
                .query_async(&mut conn)
                .await?;

            all_keys.extend(keys);
            cursor = next_cursor;

            if cursor == 0 {
                break;
            }
        }

        Ok(all_keys)
    }

    /// Get backend statistics
    ///
    /// Returns information about the backend state including key count,
    /// memory usage, and connection info.
    ///
    /// Uses SCAN instead of KEYS for production safety (non-blocking).
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let stats = backend.get_stats().await?;
    ///
    /// println!("Task keys: {}", stats.task_key_count);
    /// println!("Chord keys: {}", stats.chord_key_count);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_stats(&mut self) -> Result<BackendStats> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;

        // Count task result keys using SCAN (production-safe)
        let task_pattern = format!("{}*", self.key_prefix);
        let task_keys = self.scan_keys(&task_pattern).await?;
        let task_key_count = task_keys.len();

        // Count chord state keys using SCAN (production-safe)
        let chord_keys = self.scan_keys("celery-chord-*").await?;
        let chord_key_count = chord_keys.len();

        // Get memory info
        let info: String = redis::cmd("INFO")
            .arg("memory")
            .query_async(&mut conn)
            .await?;

        let used_memory = info
            .lines()
            .find(|line| line.starts_with("used_memory:"))
            .and_then(|line| line.split(':').nth(1))
            .and_then(|s| s.trim().parse::<u64>().ok())
            .unwrap_or(0);

        Ok(BackendStats {
            task_key_count,
            chord_key_count,
            total_keys: task_key_count + chord_key_count,
            used_memory_bytes: used_memory,
        })
    }

    /// Cleanup expired or old task results
    ///
    /// Scans for task result keys and deletes them based on the provided filter.
    /// This is useful for bulk cleanup operations.
    ///
    /// Uses SCAN instead of KEYS for production safety (non-blocking).
    ///
    /// # Arguments
    /// * `older_than` - Delete results older than this duration
    ///
    /// # Returns
    /// Number of keys deleted
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    /// use std::time::Duration;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    ///
    /// // Clean up results older than 7 days
    /// let deleted = backend.cleanup_old_results(Duration::from_secs(7 * 24 * 3600)).await?;
    /// println!("Cleaned up {} old results", deleted);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn cleanup_old_results(&mut self, older_than: Duration) -> Result<usize> {
        let pattern = format!("{}*", self.key_prefix);
        let keys = self.scan_keys(&pattern).await?;

        let cutoff = Utc::now() - chrono::Duration::from_std(older_than).unwrap_or_default();
        let mut deleted = 0;

        for key in keys {
            // Extract task_id from key
            if let Some(task_id_str) = key.strip_prefix(&self.key_prefix) {
                if let Ok(task_id) = Uuid::parse_str(task_id_str) {
                    // Check if result is old enough
                    if let Ok(Some(meta)) = self.get_result(task_id).await {
                        if meta.created_at < cutoff {
                            self.delete_result(task_id).await?;
                            deleted += 1;
                        }
                    }
                }
            }
        }

        Ok(deleted)
    }

    /// Cleanup completed chords
    ///
    /// Removes chord state for chords that have completed or timed out.
    ///
    /// Uses SCAN instead of KEYS for production safety (non-blocking).
    ///
    /// # Returns
    /// Number of chord states deleted
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    ///
    /// let deleted = backend.cleanup_completed_chords().await?;
    /// println!("Cleaned up {} completed chords", deleted);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn cleanup_completed_chords(&mut self) -> Result<usize> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let keys = self.scan_keys("celery-chord-*").await?;

        let mut deleted = 0;

        for key in keys {
            // Skip counter keys
            if key.contains("counter") {
                continue;
            }

            // Extract chord_id from key
            if let Some(chord_id_str) = key.strip_prefix("celery-chord-") {
                if let Ok(chord_id) = Uuid::parse_str(chord_id_str) {
                    if let Ok(Some(state)) = self.chord_get_state(chord_id).await {
                        // Delete if completed, cancelled, or timed out
                        if state.is_terminal() {
                            // Delete both state and counter
                            let state_key = self.chord_key(chord_id);
                            let counter_key = self.chord_counter_key(chord_id);
                            conn.del::<_, ()>(&state_key).await?;
                            conn.del::<_, ()>(&counter_key).await?;
                            deleted += 1;
                        }
                    }
                }
            }
        }

        Ok(deleted)
    }

    /// Store a result and set its expiration atomically
    ///
    /// This is a convenience method that combines `store_result()` and `set_expiration()`
    /// into a single operation, ensuring the TTL is always set.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, ResultBackend, TaskMeta, ttl};
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_id = Uuid::new_v4();
    /// let meta = TaskMeta::new(task_id, "my_task".to_string());
    ///
    /// // Store with automatic expiration
    /// backend.store_result_with_ttl(task_id, &meta, ttl::LONG).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn store_result_with_ttl(
        &mut self,
        task_id: Uuid,
        meta: &TaskMeta,
        ttl: Duration,
    ) -> Result<()> {
        self.store_result(task_id, meta).await?;
        self.set_expiration(task_id, ttl).await?;
        Ok(())
    }

    /// Store multiple results with the same TTL
    ///
    /// This is a convenience method that combines batch store with TTL setting.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, ResultBackend, TaskMeta, ttl};
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let results = vec![
    ///     (Uuid::new_v4(), TaskMeta::new(Uuid::new_v4(), "task1".to_string())),
    ///     (Uuid::new_v4(), TaskMeta::new(Uuid::new_v4(), "task2".to_string())),
    /// ];
    ///
    /// // Store all with same TTL
    /// backend.store_results_with_ttl(&results, ttl::LONG).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn store_results_with_ttl(
        &mut self,
        results: &[(Uuid, TaskMeta)],
        ttl: Duration,
    ) -> Result<()> {
        self.store_results_batch(results).await?;
        self.set_multiple_expirations(&results.iter().map(|(id, _)| *id).collect::<Vec<_>>(), ttl)
            .await?;
        Ok(())
    }

    /// Set expiration for multiple tasks at once
    ///
    /// This uses pipelining for efficient bulk TTL updates.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, ttl};
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_ids = vec![Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4()];
    ///
    /// // Set TTL for all tasks
    /// backend.set_multiple_expirations(&task_ids, ttl::LONG).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn set_multiple_expirations(
        &mut self,
        task_ids: &[Uuid],
        ttl: Duration,
    ) -> Result<()> {
        if task_ids.is_empty() {
            return Ok(());
        }

        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let mut pipe = redis::pipe();

        let ttl_secs = ttl.as_secs() as i64;
        for task_id in task_ids {
            let key = self.task_key(*task_id);
            pipe.expire(&key, ttl_secs);
        }

        pipe.query_async::<()>(&mut conn).await?;
        Ok(())
    }

    /// Wait for a task result with timeout and polling
    ///
    /// Polls the backend until the task reaches a terminal state or timeout is reached.
    ///
    /// # Arguments
    /// * `task_id` - Task to wait for
    /// * `timeout` - Maximum time to wait
    /// * `poll_interval` - Time between polls (default: 500ms)
    ///
    /// # Returns
    /// * `Ok(Some(meta))` - Task completed (terminal state)
    /// * `Ok(None)` - Timeout reached before completion
    /// * `Err(...)` - Backend error
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    /// use uuid::Uuid;
    /// use std::time::Duration;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_id = Uuid::new_v4();
    ///
    /// // Wait up to 30 seconds for result
    /// match backend.wait_for_result(
    ///     task_id,
    ///     Duration::from_secs(30),
    ///     Duration::from_millis(500)
    /// ).await? {
    ///     Some(meta) => println!("Task completed: {:?}", meta.result),
    ///     None => println!("Task timed out"),
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn wait_for_result(
        &mut self,
        task_id: Uuid,
        timeout: Duration,
        poll_interval: Duration,
    ) -> Result<Option<TaskMeta>> {
        let start = std::time::Instant::now();

        loop {
            if let Some(meta) = self.get_result(task_id).await? {
                if meta.is_terminal() {
                    return Ok(Some(meta));
                }
            }

            if start.elapsed() >= timeout {
                return Ok(None);
            }

            tokio::time::sleep(poll_interval).await;
        }
    }

    /// Check if a task is in a terminal state without fetching full metadata
    ///
    /// This is more efficient than fetching the full result when you only need to check completion.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_id = Uuid::new_v4();
    ///
    /// if backend.is_task_complete(task_id).await? {
    ///     println!("Task is done!");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn is_task_complete(&mut self, task_id: Uuid) -> Result<bool> {
        if let Some(meta) = self.get_result(task_id).await? {
            Ok(meta.is_terminal())
        } else {
            Ok(false)
        }
    }

    /// Get the age of a task result (time since creation)
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_id = Uuid::new_v4();
    ///
    /// if let Some(age) = backend.get_task_age(task_id).await? {
    ///     println!("Task created {} seconds ago", age.num_seconds());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_task_age(&mut self, task_id: Uuid) -> Result<Option<chrono::Duration>> {
        if let Some(meta) = self.get_result(task_id).await? {
            Ok(Some(meta.age()))
        } else {
            Ok(None)
        }
    }

    /// Get or create a task result
    ///
    /// If the task exists, returns the existing metadata.
    /// If not, creates it with the provided metadata.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, ResultBackend, TaskMeta, TaskResult};
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_id = Uuid::new_v4();
    ///
    /// // Get existing or create new
    /// let meta = backend.get_or_create(
    ///     task_id,
    ///     TaskMeta::new(task_id, "my_task".to_string())
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_or_create(&mut self, task_id: Uuid, default: TaskMeta) -> Result<TaskMeta> {
        if let Some(existing) = self.get_result(task_id).await? {
            Ok(existing)
        } else {
            self.store_result(task_id, &default).await?;
            Ok(default)
        }
    }

    /// Mark a task as failed with an error message and timestamp
    ///
    /// Convenience method that sets the task result to Failure and updates completion time.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, ResultBackend};
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_id = Uuid::new_v4();
    ///
    /// backend.mark_failed(task_id, "Connection timeout".to_string()).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn mark_failed(&mut self, task_id: Uuid, error: String) -> Result<()> {
        self.mark_completed(task_id, TaskResult::Failure(error))
            .await
    }

    /// Mark a task as successful with a result value and timestamp
    ///
    /// Convenience method that sets the task result to Success and updates completion time.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, ResultBackend};
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_id = Uuid::new_v4();
    ///
    /// backend.mark_success(task_id, serde_json::json!({"result": 42})).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn mark_success(&mut self, task_id: Uuid, value: serde_json::Value) -> Result<()> {
        self.mark_completed(task_id, TaskResult::Success(value))
            .await
    }

    /// Mark a task as revoked (cancelled)
    ///
    /// Convenience method that sets the task result to Revoked and updates completion time.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, ResultBackend};
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_id = Uuid::new_v4();
    ///
    /// backend.mark_revoked(task_id).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn mark_revoked(&mut self, task_id: Uuid) -> Result<()> {
        self.mark_completed(task_id, TaskResult::Revoked).await
    }

    /// Get ages for multiple tasks in a single batch operation
    ///
    /// This is more efficient than calling `get_task_age` multiple times.
    ///
    /// # Returns
    /// A vector of `Option<chrono::Duration>` corresponding to each task ID.
    /// Returns `None` for tasks that don't exist.
    pub async fn get_task_ages_batch(
        &mut self,
        task_ids: &[Uuid],
    ) -> Result<Vec<Option<chrono::Duration>>> {
        let results = self.get_results_batch(task_ids).await?;
        let now = Utc::now();

        Ok(results
            .into_iter()
            .map(|meta_opt| meta_opt.map(|meta| now.signed_duration_since(meta.created_at)))
            .collect())
    }

    /// Get a summary of task states for a list of tasks
    ///
    /// Returns statistics about task completion, failures, etc.
    pub async fn get_task_summary(&mut self, task_ids: &[Uuid]) -> Result<TaskSummary> {
        let results = self.get_results_batch(task_ids).await?;

        let mut summary = TaskSummary {
            total: task_ids.len(),
            found: 0,
            not_found: 0,
            pending: 0,
            started: 0,
            success: 0,
            failure: 0,
            retry: 0,
            revoked: 0,
        };

        for result in results {
            match result {
                Some(meta) => {
                    summary.found += 1;
                    match meta.result {
                        TaskResult::Pending => summary.pending += 1,
                        TaskResult::Started => summary.started += 1,
                        TaskResult::Success(_) => summary.success += 1,
                        TaskResult::Failure(_) => summary.failure += 1,
                        TaskResult::Retry(_) => summary.retry += 1,
                        TaskResult::Revoked => summary.revoked += 1,
                    }
                }
                None => summary.not_found += 1,
            }
        }

        Ok(summary)
    }

    /// Check if a task exists in the backend
    ///
    /// This is more efficient than calling `get_result` if you only need to check existence.
    pub async fn task_exists(&mut self, task_id: Uuid) -> Result<bool> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let key = self.task_key(task_id);
        let exists: bool = conn.exists(&key).await?;
        Ok(exists)
    }

    /// Check existence for multiple tasks in a batch
    ///
    /// Returns a vector of booleans indicating whether each task exists.
    pub async fn tasks_exist_batch(&mut self, task_ids: &[Uuid]) -> Result<Vec<bool>> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let mut pipe = redis::pipe();

        for task_id in task_ids {
            let key = self.task_key(*task_id);
            pipe.exists(&key);
        }

        let results: Vec<bool> = pipe.query_async(&mut conn).await?;
        Ok(results)
    }

    /// Get failed tasks from a list of task IDs
    ///
    /// Returns task IDs and their error messages for all failed tasks.
    pub async fn get_failed_tasks(&mut self, task_ids: &[Uuid]) -> Result<Vec<(Uuid, String)>> {
        let results = self.get_results_batch(task_ids).await?;
        let mut failed = Vec::new();

        for (task_id, meta_opt) in task_ids.iter().zip(results.iter()) {
            if let Some(meta) = meta_opt {
                if let TaskResult::Failure(error) = &meta.result {
                    failed.push((*task_id, error.clone()));
                }
            }
        }

        Ok(failed)
    }

    /// Get successful tasks from a list of task IDs
    ///
    /// Returns task IDs and their result values for all successful tasks.
    pub async fn get_successful_tasks(
        &mut self,
        task_ids: &[Uuid],
    ) -> Result<Vec<(Uuid, serde_json::Value)>> {
        let results = self.get_results_batch(task_ids).await?;
        let mut successful = Vec::new();

        for (task_id, meta_opt) in task_ids.iter().zip(results.iter()) {
            if let Some(meta) = meta_opt {
                if let TaskResult::Success(value) = &meta.result {
                    successful.push((*task_id, value.clone()));
                }
            }
        }

        Ok(successful)
    }

    /// Cleanup tasks by state
    ///
    /// Removes all tasks matching the specified state from a list of task IDs.
    /// Returns the number of tasks deleted.
    pub async fn cleanup_by_state(
        &mut self,
        task_ids: &[Uuid],
        target_state: TaskResult,
    ) -> Result<usize> {
        let results = self.get_results_batch(task_ids).await?;
        let mut to_delete = Vec::new();

        for (task_id, meta_opt) in task_ids.iter().zip(results.iter()) {
            if let Some(meta) = meta_opt {
                // Compare discriminants (state type) only
                if std::mem::discriminant(&meta.result) == std::mem::discriminant(&target_state) {
                    to_delete.push(*task_id);
                }
            }
        }

        if !to_delete.is_empty() {
            self.delete_results_batch(&to_delete).await?;
        }

        Ok(to_delete.len())
    }

    /// Get TTL (time to live) for a task result
    ///
    /// Returns the remaining time before the task expires, or None if no TTL is set.
    pub async fn get_ttl(&mut self, task_id: Uuid) -> Result<Option<Duration>> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let key = self.task_key(task_id);

        let ttl_secs: i64 = conn.ttl(&key).await?;

        // Redis returns -1 if key exists but has no TTL, -2 if key doesn't exist
        if ttl_secs > 0 {
            Ok(Some(Duration::from_secs(ttl_secs as u64)))
        } else {
            Ok(None)
        }
    }

    /// Refresh TTL for a task (reset expiration timer)
    ///
    /// Extends the TTL of a task by the specified duration from now.
    pub async fn refresh_ttl(&mut self, task_id: Uuid, ttl: Duration) -> Result<()> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let key = self.task_key(task_id);
        let _: bool = conn.expire(&key, ttl.as_secs() as i64).await?;
        Ok(())
    }

    /// Refresh TTL for multiple tasks at once
    ///
    /// Efficiently updates TTL for multiple tasks using pipelining.
    /// Returns the number of tasks that had their TTL updated.
    pub async fn refresh_ttl_batch(&mut self, task_ids: &[Uuid], ttl: Duration) -> Result<usize> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let mut pipe = redis::pipe();

        for task_id in task_ids {
            let key = self.task_key(*task_id);
            pipe.expire(&key, ttl.as_secs() as i64);
        }

        let results: Vec<bool> = pipe.query_async(&mut conn).await?;
        Ok(results.iter().filter(|&&r| r).count())
    }

    /// Remove TTL from a task (make it persistent)
    ///
    /// The task will no longer expire automatically.
    pub async fn persist_task(&mut self, task_id: Uuid) -> Result<()> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let key = self.task_key(task_id);
        let _: bool = conn.persist(&key).await?;
        Ok(())
    }

    /// Count tasks by state in a list of task IDs
    ///
    /// Returns a count of how many tasks are in each state.
    pub async fn count_by_state(&mut self, task_ids: &[Uuid]) -> Result<StateCount> {
        let results = self.get_results_batch(task_ids).await?;

        let mut counts = StateCount {
            total: task_ids.len(),
            pending: 0,
            started: 0,
            success: 0,
            failure: 0,
            retry: 0,
            revoked: 0,
            not_found: 0,
        };

        for meta_opt in results {
            match meta_opt {
                Some(meta) => match meta.result {
                    TaskResult::Pending => counts.pending += 1,
                    TaskResult::Started => counts.started += 1,
                    TaskResult::Success(_) => counts.success += 1,
                    TaskResult::Failure(_) => counts.failure += 1,
                    TaskResult::Retry(_) => counts.retry += 1,
                    TaskResult::Revoked => counts.revoked += 1,
                },
                None => counts.not_found += 1,
            }
        }

        Ok(counts)
    }

    // === Transaction Support ===

    /// Atomically store multiple results with optional TTL
    ///
    /// Uses Redis MULTI/EXEC transaction to ensure all operations succeed or fail together.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, TaskMeta, ttl};
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let results = vec![
    ///     (Uuid::new_v4(), TaskMeta::new(Uuid::new_v4(), "task1".to_string())),
    ///     (Uuid::new_v4(), TaskMeta::new(Uuid::new_v4(), "task2".to_string())),
    /// ];
    ///
    /// // Atomically store all results with TTL
    /// backend.atomic_store_multiple(&results, Some(ttl::LONG)).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn atomic_store_multiple(
        &mut self,
        results: &[(Uuid, TaskMeta)],
        ttl: Option<Duration>,
    ) -> Result<()> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let mut pipe = redis::pipe();
        pipe.atomic();

        for (task_id, meta) in results {
            let key = self.task_key(*task_id);
            let json = serde_json::to_string(meta)
                .map_err(|e| BackendError::Serialization(e.to_string()))?;
            pipe.set(&key, json);

            if let Some(ttl_duration) = ttl {
                pipe.expire(&key, ttl_duration.as_secs() as i64);
            }
        }

        let _: () = pipe.query_async(&mut conn).await?;
        Ok(())
    }

    /// Atomically delete multiple task results
    ///
    /// Uses Redis transaction to ensure all deletions succeed or fail together.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_ids = vec![Uuid::new_v4(), Uuid::new_v4()];
    ///
    /// let deleted = backend.atomic_delete_multiple(&task_ids).await?;
    /// println!("Deleted {} tasks", deleted);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn atomic_delete_multiple(&mut self, task_ids: &[Uuid]) -> Result<usize> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let mut pipe = redis::pipe();
        pipe.atomic();

        for task_id in task_ids {
            let key = self.task_key(*task_id);
            pipe.del(&key);
        }

        let results: Vec<i32> = pipe.query_async(&mut conn).await?;
        Ok(results.iter().sum::<i32>() as usize)
    }

    // === Lua Script Support ===

    /// Execute a Lua script for atomic operations
    ///
    /// Lua scripts are executed atomically on the Redis server, ensuring thread-safe
    /// operations without race conditions.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    ///
    /// // Lua script to increment a counter with max value
    /// let script = r#"
    ///     local current = redis.call('GET', KEYS[1])
    ///     if not current then current = 0 else current = tonumber(current) end
    ///     local max = tonumber(ARGV[1])
    ///     if current < max then
    ///         redis.call('INCR', KEYS[1])
    ///         return current + 1
    ///     else
    ///         return current
    ///     end
    /// "#;
    ///
    /// let result: i64 = backend.eval_script(
    ///     script,
    ///     &["counter_key"],
    ///     &["100"]
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[allow(dead_code)]
    pub async fn eval_script<T: redis::FromRedisValue>(
        &mut self,
        script: &str,
        keys: &[&str],
        args: &[&str],
    ) -> Result<T> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let result = redis::Script::new(script)
            .key(keys)
            .arg(args)
            .invoke_async(&mut conn)
            .await?;
        Ok(result)
    }

    /// Compare-and-swap operation using Lua script
    ///
    /// Atomically updates a task result only if the current state matches the expected state.
    ///
    /// # Returns
    /// - `Ok(true)` if the swap was successful
    /// - `Ok(false)` if the current state didn't match
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, TaskMeta, TaskResult};
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_id = Uuid::new_v4();
    ///
    /// let mut expected = TaskMeta::new(task_id, "task".to_string());
    /// expected.result = TaskResult::Started;
    ///
    /// let mut new = expected.clone();
    /// new.result = TaskResult::Success(serde_json::json!({"result": 42}));
    ///
    /// // Only updates if task is currently Started
    /// let swapped = backend.compare_and_swap(task_id, &expected, &new).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn compare_and_swap(
        &mut self,
        task_id: Uuid,
        expected: &TaskMeta,
        new_value: &TaskMeta,
    ) -> Result<bool> {
        let key = self.task_key(task_id);
        let expected_json = serde_json::to_string(expected)
            .map_err(|e| BackendError::Serialization(e.to_string()))?;
        let new_json = serde_json::to_string(new_value)
            .map_err(|e| BackendError::Serialization(e.to_string()))?;

        // Lua script for atomic compare-and-swap
        let script = r#"
            local current = redis.call('GET', KEYS[1])
            if current == ARGV[1] then
                redis.call('SET', KEYS[1], ARGV[2])
                return 1
            else
                return 0
            end
        "#;

        let result: i32 = self
            .eval_script(script, &[&key], &[&expected_json, &new_json])
            .await?;

        Ok(result == 1)
    }

    // === Pattern-Based Operations ===

    /// Find all task IDs matching a pattern
    ///
    /// Uses SCAN for production-safe iteration without blocking.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    ///
    /// // Find all tasks (using wildcard pattern)
    /// let task_ids = backend.find_tasks_by_pattern("*").await?;
    /// println!("Found {} tasks", task_ids.len());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn find_tasks_by_pattern(&mut self, pattern: &str) -> Result<Vec<Uuid>> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let full_pattern = format!("{}{}", self.key_prefix, pattern);

        let mut task_ids = Vec::new();
        let mut cursor = 0u64;

        loop {
            let (new_cursor, keys): (u64, Vec<String>) = redis::cmd("SCAN")
                .arg(cursor)
                .arg("MATCH")
                .arg(&full_pattern)
                .arg("COUNT")
                .arg(100)
                .query_async(&mut conn)
                .await?;

            for key in keys {
                // Extract task ID from key
                if let Some(id_str) = key.strip_prefix(&self.key_prefix) {
                    if let Ok(task_id) = Uuid::parse_str(id_str) {
                        task_ids.push(task_id);
                    }
                }
            }

            cursor = new_cursor;
            if cursor == 0 {
                break;
            }
        }

        Ok(task_ids)
    }

    /// Delete all tasks matching a pattern
    ///
    /// Uses SCAN to find matching keys, then deletes them in batches.
    ///
    /// # Returns
    /// Number of tasks deleted
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    ///
    /// // Delete tasks (example pattern - be careful!)
    /// // let deleted = backend.delete_tasks_by_pattern("test-*").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn delete_tasks_by_pattern(&mut self, pattern: &str) -> Result<usize> {
        let task_ids = self.find_tasks_by_pattern(pattern).await?;
        let count = task_ids.len();

        if count == 0 {
            return Ok(0);
        }

        self.delete_results_batch(&task_ids).await?;
        Ok(count)
    }

    // === Task Dependencies ===

    /// Store task dependencies (parent-child relationships)
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    ///
    /// let parent = Uuid::new_v4();
    /// let children = vec![Uuid::new_v4(), Uuid::new_v4()];
    ///
    /// backend.store_task_dependencies(parent, &children).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn store_task_dependencies(
        &mut self,
        parent_id: Uuid,
        child_ids: &[Uuid],
    ) -> Result<()> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let key = format!("{}deps:{}", self.key_prefix, parent_id);

        let child_strings: Vec<String> = child_ids.iter().map(|id| id.to_string()).collect();

        if !child_strings.is_empty() {
            let _: () = conn.sadd(&key, child_strings).await?;
        }

        Ok(())
    }

    /// Get all tasks that depend on a given task
    ///
    /// # Returns
    /// Vector of task IDs that are children of the given parent task
    pub async fn get_task_dependencies(&mut self, parent_id: Uuid) -> Result<Vec<Uuid>> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let key = format!("{}deps:{}", self.key_prefix, parent_id);

        let child_strings: Vec<String> = conn.smembers(&key).await?;

        let child_ids: Vec<Uuid> = child_strings
            .iter()
            .filter_map(|s| Uuid::parse_str(s).ok())
            .collect();

        Ok(child_ids)
    }

    /// Remove task dependencies
    pub async fn remove_task_dependencies(&mut self, parent_id: Uuid) -> Result<()> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let key = format!("{}deps:{}", self.key_prefix, parent_id);
        let _: () = conn.del(&key).await?;
        Ok(())
    }

    /// Check if all dependencies of a task are complete
    ///
    /// # Returns
    /// - `Ok(true)` if all dependencies are in terminal state
    /// - `Ok(false)` if any dependency is still running
    pub async fn are_dependencies_complete(&mut self, parent_id: Uuid) -> Result<bool> {
        let child_ids = self.get_task_dependencies(parent_id).await?;

        if child_ids.is_empty() {
            return Ok(true);
        }

        let results = self.get_results_batch(&child_ids).await?;

        for meta_opt in results {
            if let Some(meta) = meta_opt {
                if !meta.result.is_terminal() {
                    return Ok(false);
                }
            } else {
                return Ok(false);
            }
        }

        Ok(true)
    }

    // === Connection Monitoring ===

    /// Get detailed Redis connection information
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    ///
    /// let info = backend.get_connection_info().await?;
    /// println!("Redis info:\n{}", info);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_connection_info(&mut self) -> Result<String> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let info: String = redis::cmd("INFO").query_async(&mut conn).await?;
        Ok(info)
    }

    /// Get Redis server memory statistics
    ///
    /// # Returns
    /// Memory statistics as a map
    pub async fn get_memory_stats(&mut self) -> Result<std::collections::HashMap<String, String>> {
        let info = self.get_connection_info().await?;
        let mut stats = std::collections::HashMap::new();

        for line in info.lines() {
            if line.starts_with("used_memory") || line.starts_with("maxmemory") {
                if let Some((key, value)) = line.split_once(':') {
                    stats.insert(key.to_string(), value.to_string());
                }
            }
        }

        Ok(stats)
    }

    /// Test connection latency (ping round-trip time)
    ///
    /// # Returns
    /// Round-trip time in microseconds
    pub async fn ping_latency(&mut self) -> Result<Duration> {
        let start = std::time::Instant::now();
        self.health_check().await?;
        Ok(start.elapsed())
    }

    // === Task Querying ===

    /// Query tasks by state
    ///
    /// Returns task IDs that match the specified state.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, TaskResult};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    ///
    /// // Find all failed tasks
    /// let failed_ids = backend.query_tasks_by_state(TaskResult::Failure("".to_string())).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn query_tasks_by_state(&mut self, target_state: TaskResult) -> Result<Vec<Uuid>> {
        // Get all task keys
        let pattern = format!("{}task-meta-*", self.key_prefix);
        let task_ids = self.find_tasks_by_pattern(&pattern).await?;

        let mut matching_ids = Vec::new();

        // Filter by state
        for task_id in task_ids {
            if let Some(meta) = self.get_result(task_id).await? {
                if meta.result.same_variant(&target_state) {
                    matching_ids.push(task_id);
                }
            }
        }

        Ok(matching_ids)
    }

    /// Query tasks by worker name
    ///
    /// Returns task IDs that were executed by the specified worker.
    pub async fn query_tasks_by_worker(&mut self, worker_name: &str) -> Result<Vec<Uuid>> {
        let pattern = format!("{}task-meta-*", self.key_prefix);
        let task_ids = self.find_tasks_by_pattern(&pattern).await?;

        let mut matching_ids = Vec::new();

        for task_id in task_ids {
            if let Some(meta) = self.get_result(task_id).await? {
                if let Some(ref worker) = meta.worker {
                    if worker == worker_name {
                        matching_ids.push(task_id);
                    }
                }
            }
        }

        Ok(matching_ids)
    }

    /// Query tasks created within a time range
    ///
    /// # Arguments
    /// * `start` - Start time (inclusive)
    /// * `end` - End time (inclusive)
    ///
    /// # Returns
    /// Vector of task IDs created within the specified time range
    pub async fn query_tasks_by_time_range(
        &mut self,
        start: DateTime<Utc>,
        end: DateTime<Utc>,
    ) -> Result<Vec<Uuid>> {
        let pattern = format!("{}task-meta-*", self.key_prefix);
        let task_ids = self.find_tasks_by_pattern(&pattern).await?;

        let mut matching_ids = Vec::new();

        for task_id in task_ids {
            if let Some(meta) = self.get_result(task_id).await? {
                if meta.created_at >= start && meta.created_at <= end {
                    matching_ids.push(task_id);
                }
            }
        }

        Ok(matching_ids)
    }

    /// Query tasks by multiple criteria
    ///
    /// # Arguments
    /// * `criteria` - Query criteria to filter tasks
    ///
    /// # Returns
    /// Vector of task IDs that match all specified criteria
    pub async fn query_tasks(&mut self, criteria: TaskQuery) -> Result<Vec<Uuid>> {
        let pattern = format!("{}task-meta-*", self.key_prefix);
        let task_ids = self.find_tasks_by_pattern(&pattern).await?;

        let mut matching_ids = Vec::new();

        for task_id in task_ids {
            if let Some(meta) = self.get_result(task_id).await? {
                if criteria.matches(&meta) {
                    matching_ids.push(task_id);
                }
            }
        }

        Ok(matching_ids)
    }

    /// Query tasks by tags (task must have all specified tags)
    ///
    /// This is a convenience method that wraps `query_tasks` with a tag filter.
    pub async fn query_tasks_by_tags(&mut self, tags: Vec<String>) -> Result<Vec<Uuid>> {
        let criteria = TaskQuery::new().with_tags(tags);
        self.query_tasks(criteria).await
    }

    /// Count tasks with specific tags
    ///
    /// Returns the number of tasks that have all specified tags.
    pub async fn count_tasks_by_tags(&mut self, tags: Vec<String>) -> Result<usize> {
        let task_ids = self.query_tasks_by_tags(tags).await?;
        Ok(task_ids.len())
    }

    /// Bulk delete tasks with specific tags
    ///
    /// Deletes all tasks that have all specified tags.
    /// Returns the number of tasks deleted.
    pub async fn bulk_delete_by_tags(&mut self, tags: Vec<String>) -> Result<usize> {
        let task_ids = self.query_tasks_by_tags(tags).await?;
        if task_ids.is_empty() {
            return Ok(0);
        }
        self.delete_results_batch(&task_ids).await?;
        Ok(task_ids.len())
    }

    /// Bulk revoke tasks with specific tags
    ///
    /// Revokes all tasks that have all specified tags.
    /// Returns the number of tasks revoked.
    pub async fn bulk_revoke_by_tags(&mut self, tags: Vec<String>) -> Result<usize> {
        let task_ids = self.query_tasks_by_tags(tags).await?;
        if task_ids.is_empty() {
            return Ok(0);
        }
        self.bulk_transition_state(&task_ids, TaskResult::Revoked)
            .await
    }

    /// Bulk set TTL for tasks with specific tags
    ///
    /// Sets expiration time for all tasks that have all specified tags.
    /// Returns the number of tasks updated.
    pub async fn bulk_set_ttl_by_tags(
        &mut self,
        tags: Vec<String>,
        ttl: Duration,
    ) -> Result<usize> {
        let task_ids = self.query_tasks_by_tags(tags).await?;
        if task_ids.is_empty() {
            return Ok(0);
        }
        self.set_multiple_expirations(&task_ids, ttl).await?;
        Ok(task_ids.len())
    }

    /// Store multiple results with detailed error tracking
    ///
    /// Attempts to store all results and returns detailed information about
    /// successes and failures. Unlike `store_results_batch`, this method
    /// continues on errors and reports which specific tasks failed.
    pub async fn store_results_with_details(
        &mut self,
        results: &[(Uuid, TaskMeta)],
    ) -> BatchOperationResult {
        let mut batch_result = BatchOperationResult::new();

        for (task_id, meta) in results {
            match self.store_result(*task_id, meta).await {
                Ok(()) => batch_result.record_success(*task_id),
                Err(e) => batch_result.record_failure(*task_id, e.to_string()),
            }
        }

        batch_result
    }

    /// Delete multiple results with detailed error tracking
    ///
    /// Attempts to delete all results and returns detailed information about
    /// successes and failures.
    pub async fn delete_results_with_details(&mut self, task_ids: &[Uuid]) -> BatchOperationResult {
        let mut batch_result = BatchOperationResult::new();

        for task_id in task_ids {
            match self.delete_result(*task_id).await {
                Ok(()) => batch_result.record_success(*task_id),
                Err(e) => batch_result.record_failure(*task_id, e.to_string()),
            }
        }

        batch_result
    }

    // === Bulk State Transitions ===

    /// Transition multiple tasks to a new state atomically
    ///
    /// This is useful for operational tasks like bulk revocation.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::{RedisResultBackend, TaskResult};
    /// use uuid::Uuid;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_ids = vec![Uuid::new_v4(), Uuid::new_v4()];
    ///
    /// // Revoke all tasks
    /// let count = backend.bulk_transition_state(&task_ids, TaskResult::Revoked).await?;
    /// println!("Transitioned {} tasks", count);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn bulk_transition_state(
        &mut self,
        task_ids: &[Uuid],
        new_state: TaskResult,
    ) -> Result<usize> {
        let mut count = 0;

        for &task_id in task_ids {
            if let Some(mut meta) = self.get_result(task_id).await? {
                meta.result = new_state.clone();

                // Update completion timestamp for terminal states
                if new_state.is_terminal() && meta.completed_at.is_none() {
                    meta.completed_at = Some(Utc::now());
                }

                self.store_result(task_id, &meta).await?;
                count += 1;
            }
        }

        Ok(count)
    }

    /// Bulk revoke tasks by pattern
    ///
    /// Finds all tasks matching the pattern and transitions them to Revoked state.
    ///
    /// # Returns
    /// Number of tasks revoked
    pub async fn bulk_revoke_by_pattern(&mut self, pattern: &str) -> Result<usize> {
        let task_ids = self.find_tasks_by_pattern(pattern).await?;
        self.bulk_transition_state(&task_ids, TaskResult::Revoked)
            .await
    }

    // === Metadata Partial Updates ===

    /// Update only the worker field of task metadata
    ///
    /// This is more efficient than fetching and storing the entire metadata.
    pub async fn update_worker(&mut self, task_id: Uuid, worker: String) -> Result<()> {
        if let Some(mut meta) = self.get_result(task_id).await? {
            meta.worker = Some(worker);
            self.store_result(task_id, &meta).await?;
        }
        Ok(())
    }

    /// Update only the progress field of task metadata
    pub async fn update_progress_field(
        &mut self,
        task_id: Uuid,
        progress: ProgressInfo,
    ) -> Result<()> {
        if let Some(mut meta) = self.get_result(task_id).await? {
            meta.progress = Some(progress);
            self.store_result(task_id, &meta).await?;
        }
        Ok(())
    }

    /// Increment version number of a task
    ///
    /// This is useful for optimistic locking or tracking how many times
    /// a task result has been updated.
    pub async fn increment_version(&mut self, task_id: Uuid) -> Result<u32> {
        if let Some(mut meta) = self.get_result(task_id).await? {
            meta.version += 1;
            let new_version = meta.version;
            self.store_result(task_id, &meta).await?;
            Ok(new_version)
        } else {
            Err(BackendError::NotFound(task_id))
        }
    }

    // === Result Archival ===

    /// Copy task results to an archive key with a longer TTL
    ///
    /// This is useful for preserving important results before they expire.
    ///
    /// # Example
    /// ```no_run
    /// use celers_backend_redis::RedisResultBackend;
    /// use uuid::Uuid;
    /// use std::time::Duration;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut backend = RedisResultBackend::new("redis://localhost")?;
    /// let task_id = Uuid::new_v4();
    ///
    /// // Archive with 90-day retention
    /// backend.archive_result(task_id, Duration::from_secs(90 * 86400)).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn archive_result(&mut self, task_id: Uuid, archive_ttl: Duration) -> Result<()> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;

        let source_key = format!("{}task-meta-{}", self.key_prefix, task_id);
        let archive_key = format!("{}archive:task-meta-{}", self.key_prefix, task_id);

        // Copy to archive key
        let _: () = redis::cmd("COPY")
            .arg(&source_key)
            .arg(&archive_key)
            .arg("REPLACE")
            .query_async(&mut conn)
            .await?;

        // Set TTL on archive
        let ttl_secs = archive_ttl.as_secs() as i64;
        let _: () = conn.expire(&archive_key, ttl_secs).await?;

        Ok(())
    }

    /// Retrieve an archived task result
    pub async fn get_archived_result(&mut self, task_id: Uuid) -> Result<Option<TaskMeta>> {
        let mut conn = self.client.get_multiplexed_async_connection().await?;
        let archive_key = format!("{}archive:task-meta-{}", self.key_prefix, task_id);

        let data: Option<String> = conn.get(&archive_key).await?;

        match data {
            Some(json_str) => {
                let meta: TaskMeta = serde_json::from_str(&json_str)
                    .map_err(|e| BackendError::Serialization(e.to_string()))?;
                Ok(Some(meta))
            }
            None => Ok(None),
        }
    }

    /// Bulk archive multiple task results
    pub async fn archive_results_batch(
        &mut self,
        task_ids: &[Uuid],
        archive_ttl: Duration,
    ) -> Result<usize> {
        let mut count = 0;

        for &task_id in task_ids {
            if self.archive_result(task_id, archive_ttl).await.is_ok() {
                count += 1;
            }
        }

        Ok(count)
    }

    // === Connection Pool Statistics ===

    /// Get connection pool statistics
    ///
    /// Returns statistics about the Redis connection pool,
    /// including active connections and pool capacity.
    ///
    /// # Returns
    /// Connection pool statistics
    pub async fn get_pool_stats(&self) -> PoolStats {
        // Note: redis-rs multiplexed connections don't expose pool stats directly
        // This is a placeholder that returns basic info
        PoolStats {
            backend_type: "redis".to_string(),
            connection_mode: "multiplexed".to_string(),
            is_connected: true,
        }
    }
}