Skip to content

Commit 20c3b94

Browse files
committed
Migrating task model and backend to use typed_sql.
1 parent 3ac305e commit 20c3b94

File tree

8 files changed

+346
-299
lines changed

8 files changed

+346
-299
lines changed

app/lib/database/database.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import 'package:pub_dev/database/schema.dart';
1414
import 'package:pub_dev/service/secret/backend.dart';
1515
import 'package:pub_dev/shared/configuration.dart';
1616
import 'package:pub_dev/shared/env_config.dart';
17+
import 'package:pub_dev/shared/exceptions.dart';
1718
import 'package:pub_dev/task/clock_control.dart';
19+
import 'package:retry/retry.dart';
1820
import 'package:typed_sql/typed_sql.dart';
1921

2022
final _random = Random.secure();
@@ -180,3 +182,23 @@ Future<void> _dropCustomDatabase(String url, String dbName) async {
180182
await conn.execute('DROP DATABASE "$dbName";');
181183
await conn.close(force: true);
182184
}
185+
186+
extension DatabaseExt on Database {
187+
Future<K> transactWithRetry<K>(Future<K> Function() fn) async {
188+
return await retry(
189+
() async {
190+
try {
191+
return await transact(fn);
192+
} on TransactionAbortedException catch (e) {
193+
if (e.reason is ResponseException) {
194+
// TODO: we should keep and use the original stacktrace in typed_sql's exception
195+
throw e.reason;
196+
}
197+
rethrow;
198+
}
199+
},
200+
maxAttempts: 3,
201+
retryIf: (e) => e is DatabaseException,
202+
);
203+
}
204+
}

app/lib/search/backend.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void registerSearchIndex(SearchIndex index) =>
8585
/// Datastore-related access methods for the search service
8686
class SearchBackend {
8787
final DatastoreDB _db;
88+
8889
final VersionedJsonStorage _snapshotStorage;
8990

9091
SearchBackend(this._db, Bucket snapshotBucket)
@@ -258,7 +259,7 @@ class SearchBackend {
258259
addResult(e.name, e.updated);
259260
}
260261

261-
await for (final e in _db.tasks.listFinishedSince(updatedThreshold)) {
262+
await for (final e in taskBackend.listFinishedSince(updatedThreshold)) {
262263
addResult(e.package, e.finished);
263264
}
264265

app/lib/service/services.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ Future<R> _withPubServices<R>(FutureOr<R> Function() fn) async {
338338
registerTaskBackend(
339339
TaskBackend(
340340
dbService,
341+
primaryDatabase!.db,
341342
storageService.bucket(activeConfiguration.taskResultBucketName!),
342343
),
343344
);

0 commit comments

Comments
 (0)