Use platform variables
Give a worker a setting the whole context shares, such as a region or a venue's base URL, by reference to a platform variable instead of copying the value into its config. The same worker and the same references run locally and on the cluster. The rules are in Platform variables.
1. Read the variable in the worker
The worker reads the value from its context at start, or from the attribute on each input:
C#:
public Task StartAsync(IWorkerContext context, CancellationToken stop)
{
_region = context.Variables["region"];
return Task.CompletedTask;
}
Python:
def Start(ctx):
global region
region = ctx.variables["region"]
TypeScript:
export function Start(ctx: WorkerContext) {
region = ctx.variables?.["region"] ?? "";
}
The value is also set as the region attribute on every input (over the
event's own) and removed from every output, so a worker never republishes it.
2. Rehearse it locally
Give the local WorkManager the platform variables (the local cfg.env) and
the worker the reference:
C#:
await using var wm = new LocalWorkManager(new LocalWorkManagerOptions
{
PlatformVariables = new Dictionary<string, string> { ["REGION"] = "eu" },
});
await wm.DeployAsync(WorkerSpec.ForInstance(worker, ["x.demo.requests"]) with
{
Variables = new Dictionary<string, string> { ["region"] = "REGION" },
});
Python:
with LocalWorkManager(platform_variables={"REGION": "eu"}) as wm:
wm.deploy(worker, ["x.demo.requests"], variables={"region": "REGION"})
TypeScript:
await using wm = new LocalWorkManager({ platformVariables: { REGION: "eu" } });
await wm.deploy(worker, ["x.demo.requests"], { variables: { region: "REGION" } });
To run a single worker without a manager, give the shortcut both the reference and the platform variables:
C#:
var region = new Dictionary<string, string> { ["region"] = "REGION" };
var platform = new Dictionary<string, string> { ["REGION"] = "eu" };
var outputs = await worker.Run(inputs,
new LocalRunOptions { Variables = region, PlatformVariables = platform }).ToList();
await using var host = new BehaviorTestHost(worker,
new LocalRunOptions { Variables = region, PlatformVariables = platform });
wm.Deploy(worker, ["x.demo.requests"], new LocalDeployOptions { Variables = region });
Python:
run(worker, inputs, variables={"region": "REGION"}, platform_variables={"REGION": "eu"})
TypeScript:
for await (const output of run(worker, inputs, {
variables: { region: "REGION" },
platformVariables: { REGION: "eu" },
})) console.log(output);
Python's and TypeScript's run do not start the worker, so there the worker
reads the region attribute of its inputs instead of ctx.variables.
A reference to a variable the dictionary does not hold fails like on the
cluster, with platform variable 'REGION' not found. A name the cluster's CreateWorker refuses, such as
a variable named like a config key, is refused locally with the same message
(variable name 'region' is also a config key). To see a changed value,
change the dictionary, then stop and start the worker.
3. Deploy it
On the cluster, an admin sets REGION on the clients' Environment variables
page (the state key cfg.env), and the worker is created with the same
reference: CreateWorkerRequest.variables (WorkerSpec.Variables in C#,
variables= in the Python and TypeScript WorkManager clients). The Runtime
resolves it when the worker loads and on every start; see
Worker variables.
Use secrets for credentials, and config for a worker's own settings.