@@ -9,7 +9,9 @@ import 'dart:math';
99import 'package:_pub_shared/dartdoc/dartdoc_page.dart' ;
1010import 'package:_pub_shared/data/package_api.dart' ;
1111import 'package:_pub_shared/data/task_payload.dart' ;
12+ import 'package:_pub_shared/worker/docker_utils.dart' ;
1213import 'package:clock/clock.dart' ;
14+ import 'package:gcloud/service_scope.dart' ;
1315import 'package:http/http.dart' ;
1416import 'package:http_parser/http_parser.dart' ;
1517import 'package:indexed_blob/indexed_blob.dart' ;
@@ -18,6 +20,7 @@ import 'package:pana/pana.dart';
1820import 'package:path/path.dart' as p;
1921import 'package:pub_dev/fake/backend/fake_pana_runner.dart' ;
2022import 'package:pub_dev/frontend/handlers/pubapi.client.dart' ;
23+ import 'package:pub_dev/frontend/static_files.dart' ;
2124import 'package:pub_dev/scorecard/backend.dart' ;
2225import 'package:pub_dev/service/async_queue/async_queue.dart' ;
2326import 'package:pub_dev/shared/utils.dart' ;
@@ -65,13 +68,31 @@ Future<void> processTasksLocallyWithPubWorker() async {
6568 ]);
6669}
6770
71+ /// Process analysis tasks locally, using either:
72+ /// - `fake` : the semi-randomized fake analysis that is fast
73+ /// - `local` : running pkg/pub_worker using the same context as the app
74+ /// - `worker` : running pkg/pub_worker using the dockerized container
75+ Future <void > processTaskFakeLocalOrWorker (String analysis) async {
76+ if (analysis == 'none' ) {
77+ return ;
78+ } else if (analysis == 'local' ) {
79+ await _analyzeLocal ();
80+ } else if (analysis == 'worker' ) {
81+ await _analyzeWorker ();
82+ } else if (analysis == 'fake' ) {
83+ await processTasksWithFakePanaAndDartdoc ();
84+ } else {
85+ throw ArgumentError ('Unknown analysis: `$analysis `' );
86+ }
87+ }
88+
6889/// Updates the task status for all packages and imitates
6990/// pub_worker using fake pana and dartdoc results.
7091Future <void > processTasksWithFakePanaAndDartdoc () async {
71- await taskBackend.backfillAndProcessAllPackages (_processPayload );
92+ await taskBackend.backfillAndProcessAllPackages (_fakeAnalysis );
7293}
7394
74- Future <void > _processPayload (Payload payload) async {
95+ Future <void > _fakeAnalysis (Payload payload) async {
7596 for (final v in payload.versions) {
7697 final client = httpClientWithAuthorization (
7798 tokenProvider: () async => v.token,
@@ -154,10 +175,40 @@ Future<void> _processPayload(Payload payload) async {
154175 }
155176}
156177
178+ Future <void > _analyzeLocal () async {
179+ await fork (() async {
180+ await taskBackend.backfillAndProcessAllPackages ((Payload payload) async {
181+ final arguments = [json.encode (payload.toJson ())];
182+ final pr = await Process .run (
183+ Platform .resolvedExecutable,
184+ ['run' , 'pub_worker' , ...arguments],
185+ workingDirectory: p.join (resolveAppDir (), '..' , 'pkg' , 'pub_worker' ),
186+ );
187+ if (pr.exitCode != 0 ) {
188+ throw Exception ('Unexpected status code: ${pr .exitCode } ${pr .stdout }' );
189+ }
190+ });
191+ });
192+ }
193+
194+ Future <void > _analyzeWorker () async {
195+ await buildDockerImage ();
196+ await fork (() async {
197+ await taskBackend.backfillAndProcessAllPackages ((Payload payload) async {
198+ final p = await startDockerAnalysis (payload);
199+ final exitCode = await p.exitCode;
200+ if (exitCode != 0 ) {
201+ throw Exception (
202+ 'Failed to analyze ${payload .package } with exitCode $exitCode ' );
203+ }
204+ });
205+ });
206+ }
207+
157208Future <void > fakeCloudComputeInstanceRunner (FakeCloudInstance instance) async {
158209 final payload = Payload .fromJson (
159210 json.decode (instance.arguments.first) as Map <String , dynamic >);
160- await _processPayload (payload);
211+ await _fakeAnalysis (payload);
161212}
162213
163214Map <String , String > _fakeDartdocFiles (
0 commit comments