64
64
from azul .indexer .index_service import (
65
65
IndexService ,
66
66
)
67
+ from azul .indexer .mirror_service import (
68
+ MirrorService ,
69
+ )
67
70
from azul .json import (
68
71
Serializable ,
69
72
)
70
73
from azul .plugins import (
71
74
File ,
75
+ MetadataPlugin ,
72
76
RepositoryPlugin ,
73
77
)
74
78
from azul .queues import (
@@ -113,6 +117,8 @@ class MirrorAction(Action):
113
117
mirror_source = auto ()
114
118
mirror_partition = auto ()
115
119
mirror_file = auto ()
120
+ mirror_part = auto ()
121
+ finalize_file = auto ()
116
122
117
123
118
124
@attrs .frozen (kw_only = True )
@@ -122,6 +128,9 @@ class AzulClient(SignatureHelper, HasCachedHttpClient):
122
128
def repository_plugin (self , catalog : CatalogName ) -> RepositoryPlugin :
123
129
return self .index_service .repository_plugin (catalog )
124
130
131
+ def metadata_plugin (self , catalog : CatalogName ) -> MetadataPlugin :
132
+ return self .index_service .metadata_plugin (catalog )
133
+
125
134
def notification (self , bundle_fqid : SourcedBundleFQID ) -> JSON :
126
135
"""
127
136
Generate an indexer notification for the given bundle.
@@ -197,6 +206,38 @@ def mirror_file_message(self,
197
206
'file' : file .to_json ()
198
207
})
199
208
209
+ def mirror_part_message (self ,
210
+ catalog : CatalogName ,
211
+ source : SourceRef ,
212
+ file : File ,
213
+ part : File .PartRange ,
214
+ upload_id : str
215
+ ) -> SQSFifoMessage :
216
+ key = self .mirror_service .mirror_object_key (file )
217
+ return SQSFifoMessage ({
218
+ 'action' : MirrorAction .mirror_part .to_json (),
219
+ 'catalog' : catalog ,
220
+ 'source' : source .to_json (),
221
+ 'file' : file .to_json (),
222
+ 'part' : part .to_json (),
223
+ 'upload_id' : upload_id
224
+ }, group_id = key )
225
+
226
+ def finalize_file_message (self ,
227
+ catalog : CatalogName ,
228
+ source : SourceRef ,
229
+ file : File ,
230
+ upload_id : str
231
+ ) -> SQSFifoMessage :
232
+ key = self .mirror_service .mirror_object_key (file )
233
+ return SQSFifoMessage ({
234
+ 'action' : MirrorAction .finalize_file .to_json (),
235
+ 'catalog' : catalog ,
236
+ 'source' : source .to_json (),
237
+ 'file' : file .to_json (),
238
+ 'upload_id' : upload_id
239
+ }, group_id = key )
240
+
200
241
def local_reindex (self , catalog : CatalogName , prefix : str ) -> int :
201
242
notifications = [
202
243
self .notification (bundle_fqid )
@@ -475,6 +516,10 @@ def group_key(fqid: SourcedBundleFQID):
475
516
def index_service (self ) -> IndexService :
476
517
return IndexService ()
477
518
519
+ @cached_property
520
+ def mirror_service (self ) -> MirrorService :
521
+ return MirrorService ()
522
+
478
523
def delete_all_indices (self , catalog : CatalogName ):
479
524
self .index_service .delete_indices (catalog )
480
525
@@ -632,6 +677,26 @@ def message(file: File) -> SQSMessage:
632
677
messages = map (message , plugin .list_files (source , prefix ))
633
678
self .queue_mirror_messages (messages )
634
679
680
+ def mirror_file (self ,
681
+ catalog : CatalogName ,
682
+ source_json : JSON ,
683
+ file_json : JSON ,
684
+ part_size : int ):
685
+ source = self .repository_plugin (catalog ).source_ref_cls .from_json (source_json )
686
+ file = self .load_file (catalog , file_json )
687
+ upload_id = self .mirror_service .begin_mirroring_file (file )
688
+
689
+ def message (part : File .PartRange ) -> SQSMessage :
690
+ log .info ('Mirroring part %r of file %r in source %r from catalog %r ' ,
691
+ part .part_number , file .uuid , str (source .spec ), catalog )
692
+ return self .mirror_part_message (catalog , source , file , part , upload_id )
693
+
694
+ messages = map (message , file .partition (part_size ))
695
+ self .queue_mirror_messages (messages )
696
+
697
+ def load_file (self , catalog : CatalogName , file : JSON ) -> File :
698
+ return self .metadata_plugin (catalog ).file_class ().from_json (file )
699
+
635
700
636
701
class AzulClientError (RuntimeError ):
637
702
pass
0 commit comments