Skip to content

Commit 14f8129

Browse files
cluster: add internal worker_output_hook for customizing worker output
Add an internal, unstable `worker_output_hook` Ref that, when set, transforms each line of a worker's output into the string that should be printed for it. `Distributed` still owns the actual printing, so a hook may return a `StyledStrings`-styled line and it will render correctly on IOs that do or do not support color. When the hook is unset (the default) the standard prefix is used. A hook that throws falls back to the default prefix so worker output is never swallowed. The default prefix indent is also reduced from six spaces to a single space. The hook is deliberately not exported and carries no compatibility guarantees; it exists so test harnesses can label worker output with richer context (e.g. which test a worker is running). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent da43f39 commit 14f8129

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

src/cluster.jl

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,55 @@ function start_worker(out::IO, cookie::AbstractString=readline(stdin); close_std
294294
end
295295

296296

297+
"""
298+
worker_output_hook
299+
300+
Internal, unstable hook for customizing how a worker's output lines are displayed.
301+
302+
`worker_output_hook[]` may be set to a transform function
303+
`f(ident::AbstractString, line::AbstractString) -> AbstractString` that maps a line of
304+
worker output to the string that should be printed for it. This lets test harnesses or
305+
other callers provide richer context about what each worker is doing (e.g. which test is
306+
running) instead of the default `" From worker \$ident:\\t\$line"` prefix. The returned
307+
string is printed as-is, so it may carry `StyledStrings` styling; `Distributed` still owns
308+
the actual printing.
309+
310+
When `worker_output_hook[]` is `nothing` (the default) the standard prefix is used.
311+
312+
This is an internal implementation detail with no compatibility guarantees; it may be
313+
changed or removed at any time.
314+
315+
# Example
316+
```julia
317+
Distributed.worker_output_hook[] = (ident, line) -> "[worker \$ident] \$line"
318+
```
319+
"""
320+
const worker_output_hook = Ref{Union{Nothing, Function}}(nothing)
321+
322+
const _worker_output_prefix = " From worker "
323+
297324
function redirect_worker_output(ident, stream)
298325
t = @async while !eof(stream)
299326
line = readline(stream)
300-
if startswith(line, " From worker ")
327+
if startswith(line, _worker_output_prefix)
301328
# stdout's of "additional" workers started from an initial worker on a host are not available
302329
# on the master directly - they are routed via the initial worker's stdout.
303330
println(line)
304331
else
305-
println(" From worker $(ident):\t$line")
332+
hook = worker_output_hook[]
333+
out = nothing
334+
if hook !== nothing
335+
try
336+
out = hook(ident, line)::AbstractString
337+
catch
338+
out = nothing # a broken hook must not swallow worker output
339+
end
340+
end
341+
if out === nothing
342+
println("$(_worker_output_prefix)$(ident):\t$line")
343+
else
344+
println(out)
345+
end
306346
end
307347
end
308348
errormonitor(t)

0 commit comments

Comments
 (0)