@@ -4,14 +4,15 @@ module SplitIoClient
44 module SSE
55 module Workers
66 class SplitsWorker
7- def initialize ( synchronizer , config , feature_flags_repository , telemetry_runtime_producer , segment_fetcher )
7+ def initialize ( synchronizer , config , feature_flags_repository , telemetry_runtime_producer , segment_fetcher , rule_based_segment_repository )
88 @synchronizer = synchronizer
99 @config = config
1010 @feature_flags_repository = feature_flags_repository
1111 @queue = Queue . new
1212 @running = Concurrent ::AtomicBoolean . new ( false )
1313 @telemetry_runtime_producer = telemetry_runtime_producer
1414 @segment_fetcher = segment_fetcher
15+ @rule_based_segment_repository = rule_based_segment_repository
1516 end
1617
1718 def start
@@ -54,7 +55,10 @@ def perform
5455 case notification . data [ 'type' ]
5556 when SSE ::EventSource ::EventTypes ::SPLIT_UPDATE
5657 success = update_feature_flag ( notification )
57- @synchronizer . fetch_splits ( notification . data [ 'changeNumber' ] ) unless success
58+ @synchronizer . fetch_splits ( notification . data [ 'changeNumber' ] , 0 ) unless success
59+ when SSE ::EventSource ::EventTypes ::RB_SEGMENT_UPDATE
60+ success = update_rule_based_segment ( notification )
61+ @synchronizer . fetch_splits ( 0 , notification . data [ 'changeNumber' ] ) unless success
5862 when SSE ::EventSource ::EventTypes ::SPLIT_KILL
5963 kill_feature_flag ( notification )
6064 end
@@ -64,12 +68,14 @@ def perform
6468 def update_feature_flag ( notification )
6569 return true if @feature_flags_repository . get_change_number . to_i >= notification . data [ 'changeNumber' ]
6670 return false unless !notification . data [ 'd' ] . nil? && @feature_flags_repository . get_change_number == notification . data [ 'pcn' ]
67-
68- new_split = return_split_from_json ( notification )
71+ new_split = return_object_from_json ( notification )
6972 SplitIoClient ::Helpers ::RepositoryHelper . update_feature_flag_repository ( @feature_flags_repository ,
7073 [ new_split ] ,
7174 notification . data [ 'changeNumber' ] , @config )
72- fetch_segments_if_not_exists ( new_split )
75+ fetch_segments_if_not_exists ( Helpers ::Util . segment_names_by_object ( new_split , "IN_SEGMENT" ) , @feature_flags_repository )
76+ if fetch_rule_based_segments_if_not_exists ( Helpers ::Util . segment_names_by_object ( new_split , "IN_RULE_BASED_SEGMENT" ) , notification . data [ 'changeNumber' ] )
77+ return true
78+ end
7379
7480 @telemetry_runtime_producer . record_updates_from_sse ( Telemetry ::Domain ::Constants ::SPLITS )
7581
@@ -80,6 +86,26 @@ def update_feature_flag(notification)
8086 false
8187 end
8288
89+ def update_rule_based_segment ( notification )
90+ return true if @rule_based_segment_repository . get_change_number . to_i >= notification . data [ 'changeNumber' ]
91+ return false unless !notification . data [ 'd' ] . nil? && @rule_based_segment_repository . get_change_number == notification . data [ 'pcn' ]
92+
93+ new_rb_segment = return_object_from_json ( notification )
94+ SplitIoClient ::Helpers ::RepositoryHelper . update_rule_based_segment_repository ( @rule_based_segment_repository ,
95+ [ new_rb_segment ] ,
96+ notification . data [ 'changeNumber' ] , @config )
97+ fetch_segments_if_not_exists ( Helpers ::Util . segment_names_by_object ( new_rb_segment , "IN_SEGMENT" ) , @rule_based_segment_repository )
98+
99+ # TODO: enable when telemetry spec is added
100+ # @telemetry_runtime_producer.record_updates_from_sse(Telemetry::Domain::Constants::SPLITS)
101+
102+ true
103+ rescue StandardError => e
104+ @config . logger . debug ( "Failed to update Split: #{ e . inspect } " ) if @config . debug_enabled
105+
106+ false
107+ end
108+
83109 def kill_feature_flag ( notification )
84110 return if @feature_flags_repository . get_change_number . to_i > notification . data [ 'changeNumber' ]
85111
@@ -89,21 +115,30 @@ def kill_feature_flag(notification)
89115 notification . data [ 'splitName' ] ,
90116 notification . data [ 'defaultTreatment' ]
91117 )
92- @synchronizer . fetch_splits ( notification . data [ 'changeNumber' ] )
118+ @synchronizer . fetch_splits ( notification . data [ 'changeNumber' ] , 0 )
93119 end
94120
95- def return_split_from_json ( notification )
96- split_json = Helpers ::DecryptionHelper . get_encoded_definition ( notification . data [ 'c' ] , notification . data [ 'd' ] )
97- JSON . parse ( split_json , symbolize_names : true )
121+ def return_object_from_json ( notification )
122+ object_json = Helpers ::DecryptionHelper . get_encoded_definition ( notification . data [ 'c' ] , notification . data [ 'd' ] )
123+ JSON . parse ( object_json , symbolize_names : true )
98124 end
99125
100- def fetch_segments_if_not_exists ( feature_flag )
101- segment_names = Helpers :: Util . segment_names_by_feature_flag ( feature_flag )
126+ def fetch_segments_if_not_exists ( segment_names , object_repository )
127+
102128 return if segment_names . nil?
103129
104- @feature_flags_repository . set_segment_names ( segment_names )
130+ object_repository . set_segment_names ( segment_names )
105131 @segment_fetcher . fetch_segments_if_not_exists ( segment_names )
106132 end
133+
134+ def fetch_rule_based_segments_if_not_exists ( segment_names , change_number )
135+ if segment_names . nil? or segment_names . empty? or @rule_based_segment_repository . contains? ( segment_names . to_a )
136+ return false
137+ end
138+ @synchronizer . fetch_splits ( 0 , change_number )
139+
140+ true
141+ end
107142 end
108143 end
109144 end
0 commit comments