@@ -4,14 +4,15 @@ module SplitIoClient
4
4
module SSE
5
5
module Workers
6
6
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 )
8
8
@synchronizer = synchronizer
9
9
@config = config
10
10
@feature_flags_repository = feature_flags_repository
11
11
@queue = Queue . new
12
12
@running = Concurrent ::AtomicBoolean . new ( false )
13
13
@telemetry_runtime_producer = telemetry_runtime_producer
14
14
@segment_fetcher = segment_fetcher
15
+ @rule_based_segment_repository = rule_based_segment_repository
15
16
end
16
17
17
18
def start
@@ -54,7 +55,10 @@ def perform
54
55
case notification . data [ 'type' ]
55
56
when SSE ::EventSource ::EventTypes ::SPLIT_UPDATE
56
57
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
58
62
when SSE ::EventSource ::EventTypes ::SPLIT_KILL
59
63
kill_feature_flag ( notification )
60
64
end
@@ -64,12 +68,14 @@ def perform
64
68
def update_feature_flag ( notification )
65
69
return true if @feature_flags_repository . get_change_number . to_i >= notification . data [ 'changeNumber' ]
66
70
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 )
69
72
SplitIoClient ::Helpers ::RepositoryHelper . update_feature_flag_repository ( @feature_flags_repository ,
70
73
[ new_split ] ,
71
74
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
73
79
74
80
@telemetry_runtime_producer . record_updates_from_sse ( Telemetry ::Domain ::Constants ::SPLITS )
75
81
@@ -80,6 +86,26 @@ def update_feature_flag(notification)
80
86
false
81
87
end
82
88
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
+
83
109
def kill_feature_flag ( notification )
84
110
return if @feature_flags_repository . get_change_number . to_i > notification . data [ 'changeNumber' ]
85
111
@@ -89,21 +115,30 @@ def kill_feature_flag(notification)
89
115
notification . data [ 'splitName' ] ,
90
116
notification . data [ 'defaultTreatment' ]
91
117
)
92
- @synchronizer . fetch_splits ( notification . data [ 'changeNumber' ] )
118
+ @synchronizer . fetch_splits ( notification . data [ 'changeNumber' ] , 0 )
93
119
end
94
120
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 )
98
124
end
99
125
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
+
102
128
return if segment_names . nil?
103
129
104
- @feature_flags_repository . set_segment_names ( segment_names )
130
+ object_repository . set_segment_names ( segment_names )
105
131
@segment_fetcher . fetch_segments_if_not_exists ( segment_names )
106
132
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
107
142
end
108
143
end
109
144
end
0 commit comments