Skip to content

Commit ae06a63

Browse files
authored
Move over to the official bazel_worker implementation (#68)
1 parent 5dd197c commit ae06a63

15 files changed

+338
-892
lines changed

MODULE.bazel

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ bazel_dep(name = "rules_jvm_external", version = "5.3")
1010
bazel_dep(name = "rules_kotlin", version = "2.1.0")
1111
bazel_dep(name = "bazel_skylib", version = "1.7.1")
1212
bazel_dep(name = "platforms", version = "0.0.8")
13+
bazel_dep(name = "bazel_worker_api", version = "0.0.4")
14+
bazel_dep(name = "bazel_worker_java", version = "0.0.4")
1315

1416
bazel_dep(name = "aspect_bazel_lib", version = "2.11.0", dev_dependency = True)
1517
bazel_dep(name = "buildifier_prebuilt", version = "8.0.0", dev_dependency = True)
@@ -25,11 +27,9 @@ maven.install(
2527
"org.assertj:assertj-core:3.24.2",
2628
"junit:junit:4.13.2",
2729
# Worker Dependencies
28-
# TODO(bencodes) Remove these and use the worker impl. that Bazel defines internally
29-
"com.squareup.moshi:moshi:1.15.0",
30-
"com.squareup.moshi:moshi-kotlin:1.15.0",
31-
"com.squareup.okio:okio-jvm:3.6.0",
32-
"io.reactivex.rxjava3:rxjava:3.1.8",
30+
"com.google.protobuf:protobuf-java:4.29.0",
31+
"com.google.protobuf:protobuf-java-util:4.29.0",
32+
# Args Parsing
3333
"com.xenomachina:kotlin-argparser:2.0.7",
3434
# Lint Dependencies
3535
"com.android.tools.lint:lint:31.3.0-alpha09",

maven_install.json

Lines changed: 244 additions & 442 deletions
Large diffs are not rendered by default.

rules/impl.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ def _run_android_lint(
143143
execution_requirements = {
144144
"supports-workers": "1",
145145
"supports-multiplex-workers": "1",
146-
"requires-worker-protocol": "json",
147146
},
148147
env = {
149148
# https://googlesamples.github.io/android-custom-lint-rules/usage/variables.md.html

src/worker/BUILD

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ kt_jvm_library(
77
srcs = glob(["*.kt"]),
88
visibility = ["//visibility:public"],
99
deps = [
10-
"@rules_android_lint_deps//:com_squareup_moshi_moshi",
11-
"@rules_android_lint_deps//:com_squareup_moshi_moshi_kotlin",
12-
"@rules_android_lint_deps//:com_squareup_okio_okio_jvm",
13-
"@rules_android_lint_deps//:io_reactivex_rxjava3_rxjava",
14-
"@rules_android_lint_deps//:org_reactivestreams_reactive_streams",
10+
"@bazel_worker_java//:worker_protocol_java_proto",
11+
"@bazel_worker_java//src/main/java/com/google/devtools/build/lib/worker:work_request_handlers",
12+
"@rules_android_lint_deps//:com_google_protobuf_protobuf_java",
1513
],
1614
)
1715

src/worker/PersistentWorker.kt

Lines changed: 23 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,37 @@
11
package com.rules.android.lint.worker
22

3-
import io.reactivex.rxjava3.core.BackpressureStrategy
4-
import io.reactivex.rxjava3.core.Flowable
5-
import io.reactivex.rxjava3.core.Scheduler
6-
import io.reactivex.rxjava3.schedulers.Schedulers
7-
import java.io.BufferedOutputStream
8-
import java.io.ByteArrayOutputStream
3+
import com.google.devtools.build.lib.worker.ProtoWorkerMessageProcessor
4+
import com.google.devtools.build.lib.worker.WorkRequestHandler
95
import java.io.IOException
106
import java.io.PrintStream
7+
import java.time.Duration
118

129
internal class PersistentWorker(
13-
/**
14-
* WorkerIO instance wrapping the standard output streams
15-
*/
16-
private val workerIO: WorkerIO,
17-
/**
18-
* Rxjava Scheduler to execute work requests on.
19-
*/
20-
private val scheduler: Scheduler,
21-
/**
22-
* Instance of CpuTimeBasedGcScheduler that will run periodically
23-
*/
24-
private val persistentWorkerCpuTimeBasedGcScheduler: PersistentWorkerCpuTimeBasedGcScheduler,
25-
/**
26-
* Instance of CpuTimeBasedGcScheduler that will run periodically
27-
*/
28-
private val workRequestProcessor: Worker.WorkerMessageProcessor,
29-
/**
30-
* Instance of CpuTimeBasedGcScheduler that will run periodically
31-
*/
3210
private val workerWorkRequestCallback: Worker.WorkRequestCallback,
3311
) : Worker {
34-
constructor(
35-
workerMessageProcessor: Worker.WorkRequestCallback,
36-
) : this(
37-
workerIO = WorkerIO(),
38-
scheduler = Schedulers.io(),
39-
persistentWorkerCpuTimeBasedGcScheduler = PersistentWorkerCpuTimeBasedGcScheduler(),
40-
workRequestProcessor =
41-
WorkerJsonMessageProcessor(
42-
System.`in`,
43-
System.out,
44-
),
45-
workerWorkRequestCallback = workerMessageProcessor,
46-
)
47-
48-
/**
49-
* Initiate the worker and begin processing work requests
50-
*/
5112
override fun processRequests(): Int {
52-
return workerIO.use { io ->
53-
// Start by redirecting the system streams so that nothing
54-
// corrupts the streams that the worker uses
55-
io.redirectSystemStreams()
13+
val realStdErr: PrintStream = System.err
5614

57-
// Process requests as they come in using RxJava
58-
Flowable
59-
.create(
60-
{ emitter ->
61-
while (!emitter.isCancelled) {
62-
try {
63-
val request: WorkRequest = workRequestProcessor.readWorkRequest()
64-
emitter.onNext(request)
65-
} catch (e: IOException) {
66-
emitter.onError(e)
67-
}
68-
}
69-
},
70-
BackpressureStrategy.BUFFER,
71-
).subscribeOn(scheduler)
72-
.parallel()
73-
.runOn(scheduler)
74-
// Execute the work and map the result to a work response
75-
.map { request -> return@map this.respondToRequest(request) }
76-
// Run the garbage collector periodically so that we are a good responsible worker
77-
.doOnNext { persistentWorkerCpuTimeBasedGcScheduler.maybePerformGc() }
78-
.doOnError { it.printStackTrace() }
79-
.sequential()
80-
.observeOn(scheduler)
81-
.blockingSubscribe { response ->
82-
workRequestProcessor.writeWorkResponse(response)
83-
}
84-
return@use 0
15+
try {
16+
val workerHandler: WorkRequestHandler =
17+
WorkRequestHandler
18+
.WorkRequestHandlerBuilder(
19+
WorkRequestHandler.WorkRequestCallback { request, pw ->
20+
return@WorkRequestCallback workerWorkRequestCallback.processWorkRequest(
21+
request.argumentsList.toList(),
22+
System.err,
23+
)
24+
},
25+
realStdErr,
26+
ProtoWorkerMessageProcessor(System.`in`, System.out),
27+
).setCpuUsageBeforeGc(Duration.ofSeconds(10))
28+
.build()
29+
workerHandler.processRequests()
30+
} catch (e: IOException) {
31+
e.printStackTrace(realStdErr)
32+
return 1
8533
}
86-
}
8734

88-
private fun respondToRequest(request: WorkRequest): WorkResponse {
89-
ByteArrayOutputStream().use { baos ->
90-
// Create a print stream that the execution can write logs to
91-
val printStream = PrintStream(BufferedOutputStream(ByteArrayOutputStream()))
92-
var exitCode: Int
93-
try {
94-
// Sanity check the work request arguments
95-
val arguments =
96-
requireNotNull(request.arguments) {
97-
"Request with id ${request.requestId} " +
98-
"does not have arguments!"
99-
}
100-
require(arguments.isNotEmpty()) {
101-
"Request with id ${request.requestId} " +
102-
"does not have arguments!"
103-
}
104-
exitCode = workerWorkRequestCallback.processWorkRequest(arguments, printStream)
105-
} catch (e: Exception) {
106-
e.printStackTrace(printStream)
107-
exitCode = 1
108-
} finally {
109-
printStream.flush()
110-
}
111-
112-
val output =
113-
arrayOf(baos.toString())
114-
.asSequence()
115-
.map { it.trim() }
116-
.filter { it.isNotEmpty() }
117-
.joinToString("\n")
118-
return WorkResponse(
119-
exitCode = exitCode,
120-
output = output,
121-
requestId = request.requestId,
122-
)
123-
}
35+
return 0
12436
}
12537
}

src/worker/PersistentWorkerCpuTimeBasedGcScheduler.kt

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/worker/WorkRequest.kt

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/worker/WorkResponse.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/worker/Worker.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.rules.android.lint.worker
22

3-
import java.io.IOException
43
import java.io.PrintStream
54

65
interface Worker {
@@ -16,14 +15,6 @@ interface Worker {
1615
): Int
1716
}
1817

19-
interface WorkerMessageProcessor {
20-
@Throws(IOException::class)
21-
fun readWorkRequest(): WorkRequest
22-
23-
@Throws(IOException::class)
24-
fun writeWorkResponse(workResponse: WorkResponse)
25-
}
26-
2718
companion object {
2819
/**
2920
* Creates the appropriate worker instance using the provided worker arguments.

src/worker/WorkerIO.kt

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)