Skip to content

Commit 06a0c5e

Browse files
committed
Move a transaction block into the task db extension methods.
1 parent 704b8b9 commit 06a0c5e

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

app/lib/task/backend.dart

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,9 +1382,7 @@ final class _TaskDataAccess {
13821382
return await withRetryTransaction(_db, (tx) async {
13831383
// Reload [state] within a transaction to avoid overwriting changes
13841384
// made by others trying to update state for another package.
1385-
final s = await tx.lookupOrNull<PackageState>(
1386-
PackageState.createKey(_db.emptyKey, runtimeVersion, package),
1387-
);
1385+
final s = await tx.tasks.lookupOrNull(package);
13881386
if (s == null) {
13891387
// No entry has been created yet, probably because of a new deployment rolling out.
13901388
// We can ignore it for now.
@@ -1404,18 +1402,35 @@ final class _TaskDataAccess {
14041402

14051403
Future<void> bumpPriority(String packageName) async {
14061404
await withRetryTransaction(_db, (tx) async {
1407-
final stateKey = PackageState.createKey(
1408-
_db.emptyKey,
1409-
runtimeVersion,
1410-
packageName,
1411-
);
1412-
final state = await tx.lookupOrNull<PackageState>(stateKey);
1405+
final state = await tx.tasks.lookupOrNull(packageName);
14131406
if (state != null) {
14141407
state.pendingAt = initialTimestamp;
14151408
tx.insert(state);
14161409
}
14171410
});
14181411
}
1412+
1413+
/// Restores the previous versions map state when starting the tasks on [instanceName] failed.
1414+
Future<void> restorePreviousVersionsState(
1415+
String packageName,
1416+
String instanceName,
1417+
Map<String, PackageVersionStateInfo> previousVersionsMap,
1418+
) async {
1419+
await withRetryTransaction(_db, (tx) async {
1420+
final s = await tx.tasks.lookupOrNull(packageName);
1421+
if (s == null) {
1422+
return; // Presumably, the package was deleted.
1423+
}
1424+
1425+
s.versions!.addEntries(
1426+
s.versions!.entries
1427+
.where((e) => e.value.instance == instanceName)
1428+
.map((e) => MapEntry(e.key, previousVersionsMap[e.key]!)),
1429+
);
1430+
s.derivePendingAt();
1431+
await tx.tasks.update(s);
1432+
});
1433+
}
14191434
}
14201435

14211436
class _TaskTransactionDataAcccess {

app/lib/task/scheduler.dart

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'package:pub_dev/package/backend.dart';
1010
import 'package:pub_dev/shared/configuration.dart';
1111
import 'package:pub_dev/shared/datastore.dart';
1212
import 'package:pub_dev/shared/utils.dart';
13-
import 'package:pub_dev/shared/versions.dart' show runtimeVersion;
1413
import 'package:pub_dev/task/backend.dart';
1514
import 'package:pub_dev/task/clock_control.dart';
1615
import 'package:pub_dev/task/cloudcompute/cloudcompute.dart';
@@ -237,26 +236,11 @@ Future<void> schedule(
237236
// suppose to run on the instance we just failed to create.
238237
// If this doesn't work, we'll eventually retry. Hence, correctness
239238
// does not hinge on this transaction being successful.
240-
await withRetryTransaction(db, (tx) async {
241-
final s = await tx.lookupOrNull<PackageState>(
242-
PackageState.createKey(
243-
db.emptyKey,
244-
runtimeVersion,
245-
selected.package,
246-
),
247-
);
248-
if (s == null) {
249-
return; // Presumably, the package was deleted.
250-
}
251-
252-
s.versions!.addEntries(
253-
s.versions!.entries
254-
.where((e) => e.value.instance == instanceName)
255-
.map((e) => MapEntry(e.key, oldVersionsMap[e.key]!)),
256-
);
257-
s.derivePendingAt();
258-
tx.insert(s);
259-
});
239+
await db.tasks.restorePreviousVersionsState(
240+
selected.package,
241+
instanceName,
242+
oldVersionsMap,
243+
);
260244
}
261245
}
262246
});
@@ -295,9 +279,7 @@ updatePackageStateWithPendingVersions(
295279
String instanceName,
296280
) async {
297281
return await withRetryTransaction(db, (tx) async {
298-
final s = await tx.lookupOrNull<PackageState>(
299-
PackageState.createKey(db.emptyKey, runtimeVersion, package),
300-
);
282+
final s = await tx.tasks.lookupOrNull(package);
301283
if (s == null) {
302284
// presumably the package was deleted.
303285
return null;
@@ -360,7 +342,7 @@ updatePackageStateWithPendingVersions(
360342
),
361343
});
362344
s.derivePendingAt();
363-
tx.insert(s);
345+
await tx.tasks.update(s);
364346

365347
// Create payload
366348
final payload = Payload(

0 commit comments

Comments
 (0)