Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Important changes to data models, configuration, and migrations between each
AppEngine version, listed here to ease deployment and troubleshooting.

## Next Release (replace with git tag when deployed)
* Note: `search` isolate renewal is randomized.

## `20241205t082000-all`
* Bump runtimeVersion to `2024.12.04`.
Expand Down
26 changes: 22 additions & 4 deletions app/lib/service/entrypoint/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:math';

import 'package:args/command_runner.dart';
import 'package:gcloud/service_scope.dart';
Expand All @@ -18,6 +19,7 @@ import '../../shared/handler_helpers.dart';
import '_isolate.dart';

final Logger _logger = Logger('pub.search');
final _random = Random.secure();

class SearchCommand extends Command {
@override
Expand All @@ -28,6 +30,10 @@ class SearchCommand extends Command {

@override
Future<void> run() async {
// An instance-specific additional drift, to make sure that the index updates
// are stretched out over time.
final delayDrift = _random.nextInt(60);

envConfig.checkServiceEnvironment(name);
await withServices(() async {
final index = await startQueryIsolate(
Expand All @@ -39,10 +45,22 @@ class SearchCommand extends Command {

registerSearchIndex(IsolateSearchIndex(index));

final renewTimer = Timer.periodic(Duration(minutes: 15), (_) async {
await index.renew(count: 1, wait: Duration(minutes: 2));
});
registerScopeExitCallback(() => renewTimer.cancel());
void scheduleRenew() {
scheduleMicrotask(() async {
// 12 - 17 minutes delay
final delay =
Duration(minutes: 12, seconds: delayDrift + _random.nextInt(240));
await Future.delayed(delay);

// create a new index and handover with a 2-minute maximum wait
await index.renew(count: 1, wait: Duration(minutes: 2));

// schedule the renewal again
scheduleRenew();
});
}

scheduleRenew();

await runHandler(_logger, searchServiceHandler);
});
Expand Down
Loading