@@ -14,13 +14,15 @@ use crate::{
1414 event:: { filter_event_data_by_conditions, EventMessage } ,
1515 indexer:: native_transfer:: EVENT_NAME ,
1616 manifest:: stream:: {
17- KafkaStreamConfig , KafkaStreamQueueConfig , RabbitMQStreamConfig , RabbitMQStreamQueueConfig ,
18- RedisStreamConfig , RedisStreamStreamConfig , SNSStreamTopicConfig , StreamEvent ,
19- StreamsConfig , WebhookStreamConfig ,
17+ CloudflareQueuesStreamConfig , CloudflareQueuesStreamQueueConfig , KafkaStreamConfig ,
18+ KafkaStreamQueueConfig , RabbitMQStreamConfig , RabbitMQStreamQueueConfig , RedisStreamConfig ,
19+ RedisStreamStreamConfig , SNSStreamTopicConfig , StreamEvent , StreamsConfig ,
20+ WebhookStreamConfig ,
2021 } ,
2122 streams:: {
2223 kafka:: { Kafka , KafkaError } ,
23- RabbitMQ , RabbitMQError , Redis , RedisError , Webhook , WebhookError , SNS ,
24+ CloudflareQueues , CloudflareQueuesError , RabbitMQ , RabbitMQError , Redis , RedisError ,
25+ Webhook , WebhookError , SNS ,
2426 } ,
2527} ;
2628
@@ -54,6 +56,9 @@ pub enum StreamError {
5456 #[ error( "Redis could not publish: {0}" ) ]
5557 RedisCouldNotPublish ( #[ from] RedisError ) ,
5658
59+ #[ error( "Cloudflare Queues could not publish: {0}" ) ]
60+ CloudflareQueuesCouldNotPublish ( #[ from] CloudflareQueuesError ) ,
61+
5762 #[ error( "Task failed: {0}" ) ]
5863 JoinError ( JoinError ) ,
5964}
@@ -82,17 +87,25 @@ pub struct RedisStream {
8287 client : Arc < Redis > ,
8388}
8489
90+ #[ derive( Debug ) ]
91+ pub struct CloudflareQueuesStream {
92+ config : CloudflareQueuesStreamConfig ,
93+ client : Arc < CloudflareQueues > ,
94+ }
95+
8596#[ derive( Debug ) ]
8697pub struct StreamsClients {
8798 sns : Option < SNSStream > ,
8899 webhook : Option < WebhookStream > ,
89100 rabbitmq : Option < RabbitMQStream > ,
90101 kafka : Option < KafkaStream > ,
91102 redis : Option < RedisStream > ,
103+ cloudflare_queues : Option < CloudflareQueuesStream > ,
92104}
93105
94106impl StreamsClients {
95107 pub async fn new ( stream_config : StreamsConfig ) -> Self {
108+ #[ allow( clippy:: manual_map) ]
96109 let sns = if let Some ( config) = & stream_config. sns {
97110 Some ( SNSStream {
98111 config : config. topics . clone ( ) ,
@@ -102,11 +115,13 @@ impl StreamsClients {
102115 None
103116 } ;
104117
118+ #[ allow( clippy:: manual_map) ]
105119 let webhook = stream_config. webhooks . as_ref ( ) . map ( |config| WebhookStream {
106120 config : config. clone ( ) ,
107121 client : Arc :: new ( Webhook :: new ( ) ) ,
108122 } ) ;
109123
124+ #[ allow( clippy:: manual_map) ]
110125 let rabbitmq = if let Some ( config) = stream_config. rabbitmq . as_ref ( ) {
111126 Some ( RabbitMQStream {
112127 config : config. clone ( ) ,
@@ -116,6 +131,7 @@ impl StreamsClients {
116131 None
117132 } ;
118133
134+ #[ allow( clippy:: manual_map) ]
119135 let kafka = if let Some ( config) = stream_config. kafka . as_ref ( ) {
120136 Some ( KafkaStream {
121137 config : config. clone ( ) ,
@@ -129,6 +145,7 @@ impl StreamsClients {
129145 None
130146 } ;
131147
148+ #[ allow( clippy:: manual_map) ]
132149 let redis = if let Some ( config) = stream_config. redis . as_ref ( ) {
133150 Some ( RedisStream {
134151 config : config. clone ( ) ,
@@ -142,7 +159,20 @@ impl StreamsClients {
142159 None
143160 } ;
144161
145- Self { sns, webhook, rabbitmq, kafka, redis }
162+ #[ allow( clippy:: manual_map) ]
163+ let cloudflare_queues = if let Some ( config) = stream_config. cloudflare_queues . as_ref ( ) {
164+ Some ( CloudflareQueuesStream {
165+ config : config. clone ( ) ,
166+ client : Arc :: new ( CloudflareQueues :: new (
167+ config. api_token . clone ( ) ,
168+ config. account_id . clone ( ) ,
169+ ) ) ,
170+ } )
171+ } else {
172+ None
173+ } ;
174+
175+ Self { sns, webhook, rabbitmq, kafka, redis, cloudflare_queues }
146176 }
147177
148178 fn has_any_streams ( & self ) -> bool {
@@ -151,6 +181,7 @@ impl StreamsClients {
151181 || self . rabbitmq . is_some ( )
152182 || self . kafka . is_some ( )
153183 || self . redis . is_some ( )
184+ || self . cloudflare_queues . is_some ( )
154185 }
155186
156187 fn chunk_data ( & self , data_array : & Vec < Value > ) -> Vec < Vec < Value > > {
@@ -450,6 +481,39 @@ impl StreamsClients {
450481 tasks
451482 }
452483
484+ fn cloudflare_queues_stream_tasks (
485+ & self ,
486+ config : & CloudflareQueuesStreamQueueConfig ,
487+ client : Arc < CloudflareQueues > ,
488+ id : & str ,
489+ event_message : & EventMessage ,
490+ chunks : Arc < Vec < Vec < Value > > > ,
491+ ) -> StreamPublishes {
492+ let tasks: Vec < _ > = chunks
493+ . iter ( )
494+ . enumerate ( )
495+ . map ( |( index, chunk) | {
496+ let filtered_chunk: Vec < Value > = self . filter_chunk_event_data_by_conditions (
497+ & config. events ,
498+ event_message,
499+ chunk,
500+ ) ;
501+
502+ let publish_message_id = self . generate_publish_message_id ( id, index, & None ) ;
503+ let client = Arc :: clone ( & client) ;
504+ let queue_id = config. queue_id . clone ( ) ;
505+ let publish_message =
506+ self . create_chunk_message_json ( & config. events , event_message, & filtered_chunk) ;
507+
508+ task:: spawn ( async move {
509+ client. publish ( & publish_message_id, & queue_id, & publish_message) . await ?;
510+ Ok ( filtered_chunk. len ( ) )
511+ } )
512+ } )
513+ . collect ( ) ;
514+ tasks
515+ }
516+
453517 pub async fn stream (
454518 & self ,
455519 id : String ,
@@ -549,6 +613,22 @@ impl StreamsClients {
549613 }
550614 }
551615
616+ if let Some ( cloudflare_queues) = & self . cloudflare_queues {
617+ for config in & cloudflare_queues. config . queues {
618+ if config. events . iter ( ) . any ( |e| e. event_name == event_message. event_name )
619+ && config. networks . contains ( & event_message. network )
620+ {
621+ streams. push ( self . cloudflare_queues_stream_tasks (
622+ config,
623+ Arc :: clone ( & cloudflare_queues. client ) ,
624+ & id,
625+ event_message,
626+ Arc :: clone ( & chunks) ,
627+ ) ) ;
628+ }
629+ }
630+ }
631+
552632 let mut streamed_total = 0 ;
553633
554634 if index_event_in_order {
0 commit comments