Skip to content

Commit 7b6eec6

Browse files
committed
Improve worker logging by adding a timestamp and making format more consistent
1 parent 5dbcff9 commit 7b6eec6

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/main/scala/higherkindness/rules_scala/common/worker/WorkerMain.scala

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.util.concurrent.{Callable, CancellationException, ConcurrentHashMap,
99
import scala.annotation.tailrec
1010
import scala.concurrent.{ExecutionContext, ExecutionException, Future}
1111
import scala.util.{Failure, Success, Using}
12+
import java.time.{ZoneId, ZonedDateTime}
1213

1314
abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream = System.out) {
1415

@@ -92,14 +93,20 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
9293
val verbosity = request.getVerbosity()
9394
def logVerbose(message: String) = {
9495
if (verbosity >= 10) {
95-
System.err.println(message)
96+
val now = ZonedDateTime.now(ZoneId.of("UTC"))
97+
System.err.println(s"${now}: ${message}")
9698
}
9799
}
98100

99101
// If this is a cancel request, we need to cancel a previously sent WorkRequest
100102
// Arguments and inputs fields on cancel requests "must be empty and should be ignored"
101103
if (request.getCancel()) {
102-
logVerbose(s"Cancellation WorkRequest received for request id: $requestId")
104+
// TODO: Cancellation requests when worker_verbose is set don't set verbosity = 10, so
105+
// this is unlikely to ever log. See this issue for more info:
106+
// https://github.com/bazelbuild/bazel/issues/25803
107+
logVerbose(
108+
s"Cancellation WorkRequest received for request id: $requestId",
109+
)
103110

104111
// From the Bazel doc: "The server may send cancel requests for requests that the worker
105112
// has already responded to, in which case the cancel request must be ignored."
@@ -111,7 +118,7 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
111118
} else {
112119
val args = request.getArgumentsList.toArray(Array.empty[String])
113120
val sandboxDir = Path.of(request.getSandboxDir())
114-
logVerbose(s"WorkRequest $requestId received with args: ${request.getArgumentsList}")
121+
logVerbose(s"WorkRequest received with id: $requestId and args: ${request.getArgumentsList}")
115122

116123
// We go through this hullabaloo with output streams being defined out here, so we can
117124
// close them after the async work in the Future is all done.
@@ -145,7 +152,7 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
145152
case Success(code) =>
146153
flushOut()
147154
writeResponse(requestId, maybeOutStream, Some(code))
148-
logVerbose(s"WorkResponse $requestId sent with code $code")
155+
logVerbose(s"WorkResponse for request id: $requestId sent with code $code")
149156

150157
case Failure(e: ExecutionException) =>
151158
e.getCause() match {
@@ -164,7 +171,7 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
164171
writeResponse(requestId, maybeOutStream, Some(-1))
165172
logVerbose(
166173
"Encountered an uncaught exception that was wrapped in an ExecutionException while" +
167-
s" proccessing the Future for WorkRequest $requestId. This usually means a non-fatal" +
174+
s" proccessing the Future for WorkRequest id: $requestId. This usually means a non-fatal" +
168175
" error was thrown in the Future.",
169176
)
170177
e.printStackTrace(System.err)
@@ -175,16 +182,18 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
175182
flushOut()
176183
writeResponse(requestId, None, None, wasCancelled = true)
177184
logVerbose(
178-
s"Cancellation WorkResponse sent for request id: $requestId in response to a" +
179-
" CancellationException",
185+
s"Cancellation WorkResponse sent for request id: $requestId in response to a " +
186+
e.getClass.getCanonicalName,
180187
)
181188

182189
// Work task threw an uncaught exception
183190
case Failure(e) =>
184191
maybeOut.map(e.printStackTrace(_))
185192
flushOut()
186193
writeResponse(requestId, maybeOutStream, Some(-1))
187-
logVerbose(s"Uncaught exception in Future while proccessing WorkRequest $requestId:")
194+
logVerbose(
195+
s"Uncaught exception in Future while proccessing WorkRequest id: $requestId\nType: ${e.getClass.getCanonicalName}",
196+
)
188197
e.printStackTrace(System.err)
189198
}(scala.concurrent.ExecutionContext.global)
190199
.andThen { case _ =>
@@ -197,7 +206,9 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
197206
// two active requests with the same ID. Either of which is not good and something we
198207
// should just crash on.
199208
if (activeRequests.putIfAbsent(requestId, workTask) != null) {
200-
throw new AnnexDuplicateActiveRequestException("Received a WorkRequest with an already active requestId.")
209+
throw new AnnexDuplicateActiveRequestException(
210+
s"Received a WorkRequest with an already active request id: ${requestId}",
211+
)
201212
} else {
202213
workTask.execute(ec)
203214
}

0 commit comments

Comments
 (0)