@@ -9,6 +9,7 @@ import java.util.concurrent.{Callable, CancellationException, ConcurrentHashMap,
9
9
import scala .annotation .tailrec
10
10
import scala .concurrent .{ExecutionContext , ExecutionException , Future }
11
11
import scala .util .{Failure , Success , Using }
12
+ import java .time .{ZoneId , ZonedDateTime }
12
13
13
14
abstract class WorkerMain [S ](stdin : InputStream = System .in, stdout : PrintStream = System .out) {
14
15
@@ -92,14 +93,20 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
92
93
val verbosity = request.getVerbosity()
93
94
def logVerbose (message : String ) = {
94
95
if (verbosity >= 10 ) {
95
- System .err.println(message)
96
+ val now = ZonedDateTime .now(ZoneId .of(" UTC" ))
97
+ System .err.println(s " ${now}: ${message}" )
96
98
}
97
99
}
98
100
99
101
// If this is a cancel request, we need to cancel a previously sent WorkRequest
100
102
// Arguments and inputs fields on cancel requests "must be empty and should be ignored"
101
103
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
+ )
103
110
104
111
// From the Bazel doc: "The server may send cancel requests for requests that the worker
105
112
// 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
111
118
} else {
112
119
val args = request.getArgumentsList.toArray(Array .empty[String ])
113
120
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}" )
115
122
116
123
// We go through this hullabaloo with output streams being defined out here, so we can
117
124
// 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
145
152
case Success (code) =>
146
153
flushOut()
147
154
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" )
149
156
150
157
case Failure (e : ExecutionException ) =>
151
158
e.getCause() match {
@@ -164,7 +171,7 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
164
171
writeResponse(requestId, maybeOutStream, Some (- 1 ))
165
172
logVerbose(
166
173
" 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 " +
168
175
" error was thrown in the Future." ,
169
176
)
170
177
e.printStackTrace(System .err)
@@ -175,16 +182,18 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
175
182
flushOut()
176
183
writeResponse(requestId, None , None , wasCancelled = true )
177
184
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 ,
180
187
)
181
188
182
189
// Work task threw an uncaught exception
183
190
case Failure (e) =>
184
191
maybeOut.map(e.printStackTrace(_))
185
192
flushOut()
186
193
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\n Type: ${e.getClass.getCanonicalName}" ,
196
+ )
188
197
e.printStackTrace(System .err)
189
198
}(scala.concurrent.ExecutionContext .global)
190
199
.andThen { case _ =>
@@ -197,7 +206,9 @@ abstract class WorkerMain[S](stdin: InputStream = System.in, stdout: PrintStream
197
206
// two active requests with the same ID. Either of which is not good and something we
198
207
// should just crash on.
199
208
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
+ )
201
212
} else {
202
213
workTask.execute(ec)
203
214
}
0 commit comments