Files
pipelines/app/lib/features/pipelines/pending_run_store.dart
T

145 lines
4.3 KiB
Dart

import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'run_pipeline_intent.dart';
abstract interface class PendingRunStore {
Future<RunPipelineIntent?> read(int pipelineId, int version);
Future<void> write(RunPipelineIntent intent);
Future<void> remove(int pipelineId, int version);
}
String pendingRunStorageKey(String endpoint, int pipelineId, int version) =>
'pipelines.web.pendingRun.v1:$endpoint:$pipelineId:$version';
class SharedPreferencesPendingRunStore implements PendingRunStore {
const SharedPreferencesPendingRunStore(
this.endpoint, {
this.legacyEndpoints = const [],
});
final String endpoint;
final List<String> legacyEndpoints;
Future<SharedPreferences?> _preferences() async {
try {
return await SharedPreferences.getInstance();
} catch (_) {
return null;
}
}
String _key(int id, int version) =>
pendingRunStorageKey(endpoint, id, version);
@override
Future<RunPipelineIntent?> read(int pipelineId, int version) async {
try {
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));
if (intent == null || intent.requestKey.trim().isEmpty) return null;
return intent.matches(pipelineId, version) ? intent : null;
} catch (_) {
return null;
}
}
@override
Future<void> write(RunPipelineIntent intent) async {
try {
await (await _preferences())?.setString(
_key(intent.pipelineId, intent.version),
jsonEncode(intent.toJson()),
);
} catch (_) {}
}
@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 {
if (!await preferences.remove(_key(pipelineId, version))) {
await preferences.reload();
}
} catch (_) {
try {
await preferences.reload();
} catch (_) {}
}
}
}
class MemoryPendingRunStore implements PendingRunStore {
final Map<String, String> values = {};
final String endpoint;
MemoryPendingRunStore({this.endpoint = ''});
String _key(int id, int version) =>
pendingRunStorageKey(endpoint, id, version);
@override
Future<RunPipelineIntent?> read(int id, int version) async {
try {
final intent = RunPipelineIntent.fromJson(
jsonDecode(values[_key(id, version)]!),
);
return intent?.matches(id, version) == true ? intent : null;
} catch (_) {
return null;
}
}
@override
Future<void> write(RunPipelineIntent intent) async =>
values[_key(intent.pipelineId, intent.version)] = jsonEncode(
intent.toJson(),
);
@override
Future<void> remove(int id, int version) async {
values.remove(_key(id, version));
}
}