Skip to content

Commit 4211ec6

Browse files
committed
feat: diff the configs and output them
Side effect: don't set them if they aren't changing... shouldn't really be a side effect since we're single threaded.
1 parent 59affd5 commit 4211ec6

2 files changed

Lines changed: 73 additions & 8 deletions

File tree

app_dart/lib/src/service/config.dart

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,15 +569,15 @@ class DynamicConfigUpdater {
569569

570570
void stopUpdateLoop() {
571571
if (_status != UpdaterStatus.running) return;
572-
log.info('Stopping config update loop...');
572+
log.info('ConfigUpdater: Stopping config update loop...');
573573
_status = UpdaterStatus.stopping;
574574
}
575575

576576
void startUpdateLoop(Config config) async {
577577
if (_status != UpdaterStatus.stopped) return;
578578
_status = UpdaterStatus.running;
579579

580-
log.info('Starting config update loop...');
580+
log.info('ConfigUpdater: Starting config update loop...');
581581

582582
// What we've decided:
583583
// 1. Each instance will **start** with a valid DynamicConfig
@@ -589,18 +589,51 @@ class DynamicConfigUpdater {
589589
_delay + Duration(milliseconds: _random.nextInt(1000)),
590590
);
591591
if (_status != UpdaterStatus.running) {
592-
log.info('Stopped config update loop');
592+
log.info('ConfigUpdater: Stopped config update loop');
593593
_status = UpdaterStatus.stopped;
594594
return;
595595
}
596596
try {
597597
final dynamicConfig = await fetchDynamicConfig();
598-
config._dynamicConfig = dynamicConfig;
598+
final diffs = diffConfigChanges(
599+
config._dynamicConfig.toJson(),
600+
dynamicConfig.toJson(),
601+
);
602+
if (diffs.isNotEmpty) {
603+
log.info('ConfigUpdater: ${diffs.join(',')}');
604+
config._dynamicConfig = dynamicConfig;
605+
}
599606
} catch (e, s) {
600-
log.error('Unable to fetch DynamicConfig!', e, s);
607+
log.error('ConfigUpdater: Unable to fetch DynamicConfig!', e, s);
601608
}
602609
}
603610
}
611+
612+
/// Produce a simple diff of the changing flags.
613+
List<String> diffConfigChanges(
614+
Map<String, Object?> oldFlags,
615+
Map<String, Object?> newFlags, {
616+
List<String>? diffs,
617+
String chain = 'flags',
618+
}) {
619+
diffs ??= <String>[];
620+
621+
for (final MapEntry(:key, :value) in oldFlags.entries) {
622+
if (value is Map) {
623+
diffConfigChanges(
624+
value as Map<String, Object?>,
625+
newFlags[key] as Map<String, Object?>,
626+
diffs: diffs,
627+
chain: '$chain.$key',
628+
);
629+
continue;
630+
}
631+
if (value != newFlags[key]) {
632+
diffs.add('$chain.$key $value -> ${newFlags[key]}');
633+
}
634+
}
635+
return diffs;
636+
}
604637
}
605638

606639
enum UpdaterStatus { stopped, running, stopping }

app_dart/test/service/dynamic_config_updater_test.dart

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void main() {
7575
bufferedLoggerOf(
7676
containsOnce(
7777
logThat(
78-
message: equals('Starting config update loop...'),
78+
message: equals('ConfigUpdater: Starting config update loop...'),
7979
severity: atMostInfo,
8080
),
8181
),
@@ -86,7 +86,7 @@ void main() {
8686
bufferedLoggerOf(
8787
containsOnce(
8888
logThat(
89-
message: equals('Stopping config update loop...'),
89+
message: equals('ConfigUpdater: Stopping config update loop...'),
9090
severity: atMostInfo,
9191
),
9292
),
@@ -97,7 +97,7 @@ void main() {
9797
bufferedLoggerOf(
9898
containsOnce(
9999
logThat(
100-
message: equals('Stopped config update loop'),
100+
message: equals('ConfigUpdater: Stopped config update loop'),
101101
severity: atMostInfo,
102102
),
103103
),
@@ -131,6 +131,29 @@ void main() {
131131
);
132132
expect(config.flags.backfillerCommitLimit, 100);
133133
});
134+
135+
test('handles updating values', () async {
136+
updater.startUpdateLoop(config);
137+
await Future<void>.delayed(const Duration(milliseconds: 50));
138+
mockHttpFile = updatedConfigYaml;
139+
await Future<void>.delayed(const Duration(milliseconds: 50));
140+
updater.stopUpdateLoop();
141+
142+
expect(config.flags.backfillerCommitLimit, 200);
143+
expect(
144+
log,
145+
bufferedLoggerOf(
146+
containsOnce(
147+
logThat(
148+
message: equals(
149+
'ConfigUpdater: flags.backfillerCommitLimit 100 -> 200',
150+
),
151+
severity: atMostInfo,
152+
),
153+
),
154+
),
155+
);
156+
});
134157
}
135158

136159
final class _FakeRandom extends Fake implements Random {
@@ -149,3 +172,12 @@ const goodConfigYaml = '''
149172
150173
backfillerCommitLimit: 100
151174
''';
175+
176+
const updatedConfigYaml = '''
177+
# Defines the config options for Flutter CI (Cocoon)
178+
#
179+
# The schema for this file is defined in DynamicConfig of
180+
# app_dart/lib/src/service/config.dart
181+
182+
backfillerCommitLimit: 200
183+
''';

0 commit comments

Comments
 (0)