Skip to content

Commit 7421a78

Browse files
committed
Move a transaction block into the task db extension methods.
1 parent fe2dee1 commit 7421a78

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
@@ -1397,9 +1397,7 @@ final class _TaskDataAccess {
13971397
return await withRetryTransaction(_db, (tx) async {
13981398
// Reload [state] within a transaction to avoid overwriting changes
13991399
// made by others trying to update state for another package.
1400-
final s = await tx.lookupOrNull<PackageState>(
1401-
PackageState.createKey(_db.emptyKey, runtimeVersion, package),
1402-
);
1400+
final s = await tx.tasks.lookupOrNull(package);
14031401
if (s == null) {
14041402
// No entry has been created yet, probably because of a new deployment rolling out.
14051403
// We can ignore it for now.
@@ -1419,18 +1417,35 @@ final class _TaskDataAccess {
14191417

14201418
Future<void> bumpPriority(String packageName) async {
14211419
await withRetryTransaction(_db, (tx) async {
1422-
final stateKey = PackageState.createKey(
1423-
_db.emptyKey,
1424-
runtimeVersion,
1425-
packageName,
1426-
);
1427-
final state = await tx.lookupOrNull<PackageState>(stateKey);
1420+
final state = await tx.tasks.lookupOrNull(packageName);
14281421
if (state != null) {
14291422
state.pendingAt = initialTimestamp;
14301423
tx.insert(state);
14311424
}
14321425
});
14331426
}
1427+
1428+
/// Restores the previous versions map state when starting the tasks on [instanceName] failed.
1429+
Future<void> restorePreviousVersionsState(
1430+
String packageName,
1431+
String instanceName,
1432+
Map<String, PackageVersionStateInfo> previousVersionsMap,
1433+
) async {
1434+
await withRetryTransaction(_db, (tx) async {
1435+
final s = await tx.tasks.lookupOrNull(packageName);
1436+
if (s == null) {
1437+
return; // Presumably, the package was deleted.
1438+
}
1439+
1440+
s.versions!.addEntries(
1441+
s.versions!.entries
1442+
.where((e) => e.value.instance == instanceName)
1443+
.map((e) => MapEntry(e.key, previousVersionsMap[e.key]!)),
1444+
);
1445+
s.derivePendingAt();
1446+
await tx.tasks.update(s);
1447+
});
1448+
}
14341449
}
14351450

14361451
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)