worker_tool
name
def name(
*,
name: str,
default_target_platform: None | str = ...,
target_compatible_with: list[str] = ...,
compatible_with: list[str] = ...,
exec_compatible_with: list[str] = ...,
visibility: list[str] = ...,
within_view: list[str] = ...,
metadata: OpaqueMetadata = ...,
tests: list[str] = ...,
modifiers: OpaqueMetadata = ...,
_apple_platforms: dict[str, str] = ...,
_worker_tool_runner: str = ...,
args: str | list[str] = ...,
contacts: list[str] = ...,
default_host_platform: None | str = ...,
env: dict[str, str] = ...,
exe: None | str = ...,
labels: list[str] = ...,
licenses: list[str] = ...,
max_workers: None | int = ...,
max_workers_per_thread_percent: None | int = ...,
persistent: None | bool = ...,
) -> None
Some external tools have high startup costs. To amortize those costs over the whole build rather than paying them for each rule invocation, use the worker_tool()
rule in conjunction with genrule()
. Buck then starts the external tool once and reuses it by communicating with it over stdin
and stdout
using a simple JSON protocol.
Parameters
name
: name of the targetdefault_target_platform
: specifies the default target platform, used when no platforms are specified on the command linetarget_compatible_with
: a list of constraints that are required to be satisfied for this target to be compatible with a configurationcompatible_with
: a list of constraints that are required to be satisfied for this target to be compatible with a configurationexec_compatible_with
: a list of constraints that are required to be satisfied for this target to be compatible with an execution platformvisibility
: a list of visibility patterns restricting what targets can depend on this onewithin_view
: a list of visibility patterns restricting what this target can depend onmetadata
: a key-value map of metadata associated with this targettests
: a list of targets that provide tests for this onemodifiers
: an array of modifiers associated with this targetargs
: A string of args that is passed to the executable represented byexe
on initial startup.env
: A map of environment variables that is passed to the executable represented byexe
on initial startup.exe
: Abuild target
for a rule that outputs an executable, such as ansh_binary()
. Buck runs this executable only once per build.max_workers
: The maximum number of workers of this type that Buck starts. Use-1
to allow the creation of as many workers as necessary.max_workers_per_thread_percent
: The maximum ratio of workers of this type that Buck starts per thread, specified as a positive integer percentage (1-100). Must be greater than or equal to1
and less than or equal to100
. Only one ofmax_workers
andmax_workers_per_thread_percent
may be specified.persistent
: If set to true, Buck does not restart the tool unless the tool itself changes. This means the tool persists across multiple Buck commands without being shut down and may see the same rule being built more than once. Be careful not to use this setting with tools that don't expect to process the same input—with different contents—twice!
Details
A worker_tool
rule can be referenced in the cmd
parameter of
a genrule
by using the macro:
$(exe //path/to:target)
Examples:
Consider the following build rules
:
#
# Buck
#
worker_tool(
name = 'ExternalToolWorker',
exe = ':ExternalTool',
args = '--arg1 --arg2'
)
sh_binary(
name = 'ExternalTool',
main = 'external_tool.sh',
)
genrule(
name = 'TransformA',
out = 'OutputA.txt',
cmd = '$(exe :ExternalToolWorker) argA',
)
genrule(
name = 'TransformB',
out = 'OutputB.txt',
cmd = '$(exe :ExternalToolWorker) argB',
)
genrule(
name = 'TransformC',
out = 'OutputC.txt',
cmd = '$(exe :ExternalToolWorker) argC',
)
When doing a buck build
on all three of the above genrules
, Buck
first creates the worker process by invoking:
./external_tool.sh --arg1 --arg2
Buck then communicates with this process using JSON over stdin
,
starting with a handshake:
[
{
"id": 0,
"type": "handshake",
"protocol_version": "0",
"capabilities": []
}
Buck then waits for the tool to reply on stdout
:
[
{
"id": 0,
"type": "handshake",
"protocol_version": "0",
"capabilities": []
}
Then, when building the first genrule
, Buck writes to stdin
:
,{
"id": 1,
"type": "command",
"args_path": "/tmp/1.args",
"stdout_path": "/tmp/1.out",
"stderr_path": "/tmp/1.err"
}
The file /tmp/1.args
contains argA
. The tool should
perform the necessary work for this job and then write the job's output to the files
supplied by Buck—in this case, /tmp/1.out
and /tmp/1.err
.
Once the job is done, the tool should reply to Buck on stdout
with:
,{
"id": 1,
"type": "result",
"exit_code": 0
}
Once Buck hears back from the first genrule's job, it submits the second genrule's job in the
same fashion and awaits the response. When the build is all finished,
Buck closes the JSON by writing to stdin
:
]
which signals the tool that it should exit after replying on stdout
with:
]
In this example, Buck is guaranteed to invoke
./external_tool.sh --arg1 --arg2
only once during the build. The three jobs corresponding to the three genrules are submitted synchronously to the single worker process.
Note that the id
values in the messages are not necessarily increasing or sequential,
but they do have to match between the request message and the response message of a given job as
well as in the initial handshake.
If the tool receives a message type it cannot interpret it should answer with:
{
"id": <n>,
"type": "error",
"exit_code": 1
}
If the tool receives a message type it can interpret, but the other attributes of the message are in an inconsistent state, it should answer with:
{
"id": <n>,
"type": "error",
"exit_code": 2
}