Skip to content

Commit

Permalink
Bucket config + initialization for API JSON exporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed Dec 1, 2023
1 parent a963fb3 commit d4f71d5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/lib/package/export_api_to_bucket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:io';
import 'package:basics/basics.dart';
import 'package:clock/clock.dart';
import 'package:crypto/crypto.dart';
import 'package:gcloud/service_scope.dart' as ss;
import 'package:gcloud/storage.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
Expand Down Expand Up @@ -41,6 +42,13 @@ List<String> _apiPkgNameCompletitionDataNames() => [
'current/api/package-name-completion-data',
];

/// Sets the API Exporter service.
void registerApiExporter(ApiExporter value) =>
ss.register(#_apiExporter, value);

/// The active API Exporter service or null if it hasn't been initialized.
ApiExporter? get apiExporter => ss.lookup(#_apiExporter) as ApiExporter?;

class ApiExporter {
final Bucket _bucket;
final int _concurrency;
Expand Down
19 changes: 18 additions & 1 deletion app/lib/service/entrypoint/analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import 'dart:async';
import 'package:args/command_runner.dart';
import 'package:gcloud/service_scope.dart';
import 'package:logging/logging.dart';
import 'package:pub_dev/package/export_api_to_bucket.dart';
import 'package:pub_dev/search/backend.dart';
import 'package:pub_dev/shared/configuration.dart';

import '../../analyzer/handlers.dart';
import '../../service/services.dart';
Expand Down Expand Up @@ -43,6 +45,15 @@ class AnalyzerCommand extends Command {
);
registerScopeExitCallback(indexBuilder.close);

if (activeConfiguration.exportedApiBucketName != null) {
final apiExporterIsolate = await startWorkerIsolate(
logger: logger,
entryPoint: _apiExporterMain,
kind: 'api-exporter',
);
registerScopeExitCallback(apiExporterIsolate.close);
}

await runHandler(logger, analyzerServiceHandler);
});
}
Expand All @@ -51,11 +62,11 @@ class AnalyzerCommand extends Command {
Future _workerMain(EntryMessage message) async {
message.protocolSendPort.send(ReadyMessage());

await popularityStorage.start();
await taskBackend.start();

setupAnalyzerPeriodicTasks();
setupSearchPeriodicTasks();
await popularityStorage.start();

// wait indefinitely
await Completer().future;
Expand All @@ -66,3 +77,9 @@ Future _indexBuilderMain(EntryMessage message) async {
await popularityStorage.start();
await searchBackend.updateSnapshotInForeverLoop();
}

Future _apiExporterMain(EntryMessage message) async {
message.protocolSendPort.send(ReadyMessage());
await popularityStorage.start();
await apiExporter!.uploadInForeverLoop();
}
6 changes: 6 additions & 0 deletions app/lib/service/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:gcloud/service_scope.dart';
import 'package:gcloud/storage.dart';
import 'package:googleapis_auth/auth_io.dart' as auth;
import 'package:logging/logging.dart';
import 'package:pub_dev/package/export_api_to_bucket.dart';
import 'package:pub_dev/service/async_queue/async_queue.dart';
import 'package:pub_dev/service/security_advisories/backend.dart';
import 'package:shelf/shelf.dart' as shelf;
Expand Down Expand Up @@ -228,6 +229,11 @@ Future<R> _withPubServices<R>(FutureOr<R> Function() fn) async {
registerAccountBackend(AccountBackend(dbService));
registerAdminBackend(AdminBackend(dbService));
registerAnnouncementBackend(AnnouncementBackend());
if (activeConfiguration.exportedApiBucketName != null) {
registerApiExporter(ApiExporter(
bucket: storageService
.bucket(activeConfiguration.exportedApiBucketName!)));
}
registerAsyncQueue(AsyncQueue());
registerAuditBackend(AuditBackend(dbService));
registerConsentBackend(ConsentBackend(dbService));
Expand Down
7 changes: 7 additions & 0 deletions app/lib/shared/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class Configuration {
/// The name of the Cloud Storage bucket to use for generated reports.
final String? reportsBucketName;

/// The name of the Cloud Storage bucket to use for exporting JSON API responses.
final String? exportedApiBucketName;

/// The Cloud project Id. This is only required when using Apiary to access
/// Datastore and/or Cloud Storage
final String projectId;
Expand Down Expand Up @@ -275,6 +278,7 @@ class Configuration {
required this.dartdocStorageBucketName,
required this.popularityDumpBucketName,
required this.searchSnapshotBucketName,
required this.exportedApiBucketName,
required this.maxTaskInstances,
required this.maxTaskRunHours,
required this.taskResultBucketName,
Expand Down Expand Up @@ -341,6 +345,7 @@ class Configuration {
dartdocStorageBucketName: 'fake-bucket-dartdoc',
popularityDumpBucketName: 'fake-bucket-popularity',
searchSnapshotBucketName: 'fake-bucket-search',
exportedApiBucketName: 'fake-exported-apis',
maxTaskInstances: 10,
maxTaskRunHours: 2,
taskResultBucketName: 'fake-bucket-task-result',
Expand Down Expand Up @@ -392,6 +397,7 @@ class Configuration {
dartdocStorageBucketName: 'fake-bucket-dartdoc',
popularityDumpBucketName: 'fake-bucket-popularity',
searchSnapshotBucketName: 'fake-bucket-search',
exportedApiBucketName: 'fake-exported-apis',
taskResultBucketName: 'fake-bucket-task-result',
maxTaskInstances: 10,
maxTaskRunHours: 2,
Expand Down Expand Up @@ -442,6 +448,7 @@ class Configuration {
publicPackagesBucketName!,
searchSnapshotBucketName!,
taskResultBucketName!,
if (exportedApiBucketName != null) exportedApiBucketName!,
]);

late final isProduction = projectId == 'dartlang-pub';
Expand Down
4 changes: 4 additions & 0 deletions app/lib/shared/configuration.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions app/lib/tool/neat_task/pub_dev_tasks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:io';
import 'package:gcloud/service_scope.dart' as ss;
import 'package:logging/logging.dart';
import 'package:neat_periodic_task/neat_periodic_task.dart';
import 'package:pub_dev/package/export_api_to_bucket.dart';

import '../../account/backend.dart';
import '../../account/consent_backend.dart';
Expand Down Expand Up @@ -114,6 +115,13 @@ void _setupGenericPeriodicTasks() {
task: updatePublicArchiveBucket,
);

// Exports the package name completetion data to a bucket.
_daily(
name: 'export-package-name-completition-data-to-bucket',
isRuntimeVersioned: true,
task: () async => await apiExporter?.uploadPkgNameCompletionData(),
);

// Deletes task status entities where the status hasn't been updated
// for more than a month.
_weekly(
Expand Down Expand Up @@ -150,6 +158,13 @@ void _setupGenericPeriodicTasks() {
task: taskBackend.garbageCollect,
);

// Deletes exported API data for old runtime versions
_weekly(
name: 'garbage-collect-api-exports',
isRuntimeVersioned: true,
task: () async => apiExporter?.deleteObsoleteRuntimeContent(),
);

// Delete very old instances that have been abandoned
_daily(
name: 'garbage-collect-old-instances',
Expand Down

0 comments on commit d4f71d5

Please sign in to comment.