Skip to content

Commit a1998c0

Browse files
committed
Move a transaction block into the task db extension methods.
1 parent 67c0600 commit a1998c0

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';
@@ -236,26 +235,11 @@ Future<void> schedule(
236235
// suppose to run on the instance we just failed to create.
237236
// If this doesn't work, we'll eventually retry. Hence, correctness
238237
// does not hinge on this transaction being successful.
239-
await withRetryTransaction(db, (tx) async {
240-
final s = await tx.lookupOrNull<PackageState>(
241-
PackageState.createKey(
242-
db.emptyKey,
243-
runtimeVersion,
244-
selected.package,
245-
),
246-
);
247-
if (s == null) {
248-
return; // Presumably, the package was deleted.
249-
}
250-
251-
s.versions!.addEntries(
252-
s.versions!.entries
253-
.where((e) => e.value.instance == instanceName)
254-
.map((e) => MapEntry(e.key, oldVersionsMap[e.key]!)),
255-
);
256-
s.derivePendingAt();
257-
tx.insert(s);
258-
});
238+
await db.tasks.restorePreviousVersionsState(
239+
selected.package,
240+
instanceName,
241+
oldVersionsMap,
242+
);
259243
}
260244
}
261245
});
@@ -294,9 +278,7 @@ updatePackageStateWithPendingVersions(
294278
String instanceName,
295279
) async {
296280
return await withRetryTransaction(db, (tx) async {
297-
final s = await tx.lookupOrNull<PackageState>(
298-
PackageState.createKey(db.emptyKey, runtimeVersion, package),
299-
);
281+
final s = await tx.tasks.lookupOrNull(package);
300282
if (s == null) {
301283
// presumably the package was deleted.
302284
return null;
@@ -323,7 +305,7 @@ updatePackageStateWithPendingVersions(
323305
),
324306
});
325307
s.derivePendingAt();
326-
tx.insert(s);
308+
await tx.tasks.update(s);
327309

328310
// Create payload
329311
final payload = Payload(

0 commit comments

Comments
 (0)