merge: preserve legacy server binding and pending launch identities
This commit is contained in:
@@ -14,8 +14,12 @@ String pendingRunStorageKey(String endpoint, int pipelineId, int version) =>
|
||||
'pipelines.web.pendingRun.v1:$endpoint:$pipelineId:$version';
|
||||
|
||||
class SharedPreferencesPendingRunStore implements PendingRunStore {
|
||||
const SharedPreferencesPendingRunStore(this.endpoint);
|
||||
const SharedPreferencesPendingRunStore(
|
||||
this.endpoint, {
|
||||
this.legacyEndpoints = const [],
|
||||
});
|
||||
final String endpoint;
|
||||
final List<String> legacyEndpoints;
|
||||
|
||||
Future<SharedPreferences?> _preferences() async {
|
||||
try {
|
||||
@@ -31,10 +35,37 @@ class SharedPreferencesPendingRunStore implements PendingRunStore {
|
||||
@override
|
||||
Future<RunPipelineIntent?> read(int pipelineId, int version) async {
|
||||
try {
|
||||
final raw = (await _preferences())?.getString(_key(pipelineId, version));
|
||||
final preferences = await _preferences();
|
||||
if (preferences == null) return null;
|
||||
final stableKey = _key(pipelineId, version);
|
||||
if (preferences.containsKey(stableKey)) {
|
||||
return _decode(preferences.getString(stableKey), pipelineId, version);
|
||||
}
|
||||
for (final legacy in legacyEndpoints.toSet()) {
|
||||
final value = preferences.get(
|
||||
pendingRunStorageKey(legacy, pipelineId, version),
|
||||
);
|
||||
final raw = value is String ? value : null;
|
||||
final intent = _decode(raw, pipelineId, version);
|
||||
if (intent == null) continue;
|
||||
// Best effort copy, never move: preserve the operation on write failure.
|
||||
try {
|
||||
await preferences.setString(stableKey, raw!);
|
||||
} catch (_) {}
|
||||
return intent;
|
||||
}
|
||||
return null;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
RunPipelineIntent? _decode(String? raw, int pipelineId, int version) {
|
||||
try {
|
||||
if (raw == null) return null;
|
||||
final intent = RunPipelineIntent.fromJson(jsonDecode(raw));
|
||||
return intent?.matches(pipelineId, version) == true ? intent : null;
|
||||
if (intent == null || intent.requestKey.trim().isEmpty) return null;
|
||||
return intent.matches(pipelineId, version) ? intent : null;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
@@ -52,9 +83,34 @@ class SharedPreferencesPendingRunStore implements PendingRunStore {
|
||||
|
||||
@override
|
||||
Future<void> remove(int pipelineId, int version) async {
|
||||
final preferences = await _preferences();
|
||||
if (preferences == null) return;
|
||||
// Remove legacy copies first. Keep the stable recovery copy if cleanup
|
||||
// fails, rather than allowing an older legacy operation to resurrect.
|
||||
for (final legacy in legacyEndpoints.toSet()) {
|
||||
final key = pendingRunStorageKey(legacy, pipelineId, version);
|
||||
if (key == _key(pipelineId, version)) continue;
|
||||
try {
|
||||
if (preferences.containsKey(key) && !await preferences.remove(key)) {
|
||||
await preferences.reload();
|
||||
return;
|
||||
}
|
||||
} catch (_) {
|
||||
try {
|
||||
await preferences.reload();
|
||||
} catch (_) {}
|
||||
return;
|
||||
}
|
||||
}
|
||||
try {
|
||||
await (await _preferences())?.remove(_key(pipelineId, version));
|
||||
} catch (_) {}
|
||||
if (!await preferences.remove(_key(pipelineId, version))) {
|
||||
await preferences.reload();
|
||||
}
|
||||
} catch (_) {
|
||||
try {
|
||||
await preferences.reload();
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user