From 7327896db9d2feba6f519b5ce9bff717a6f7bf63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 17 Jun 2022 16:24:03 +0200 Subject: [PATCH] Worker: allow overriding worker name from environment Allow overriding the worker name by setting the `FLAMENCO_WORKER_NAME` environment variable. This makes it easy to do from Docker configs, and, more importantly, from the scripts I use to run multiple workers on the same machine while developing Flamenco. --- internal/worker/registration.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/internal/worker/registration.go b/internal/worker/registration.go index ef27cbe0..779cf75e 100644 --- a/internal/worker/registration.go +++ b/internal/worker/registration.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "runtime" + "strings" "time" "github.com/rs/zerolog/log" @@ -18,6 +19,8 @@ import ( "git.blender.org/flamenco/pkg/api" ) +const workerNameEnvVariable = "FLAMENCO_WORKER_NAME" + var ( errSignOnCanceled = errors.New("sign-on cancelled") // For example by closing the context. errSignOnRepeatableFailure = errors.New("unable to sign on at Manager, try again later") // For example failed connections @@ -179,6 +182,16 @@ func signOn(ctx context.Context, cfg WorkerConfig, client FlamencoClient) (api.W // mustHostname either the hostname or logs a fatal error. func mustHostname() string { + name, ok := os.LookupEnv(workerNameEnvVariable) + if ok && name != "" { + name = strings.TrimSpace(name) + log.Info(). + Str("name", name). + Str("fromVariable", workerNameEnvVariable). + Msg("worker name obtained from environment variable instead of using the hostname") + return name + } + hostname, err := os.Hostname() if err != nil { log.Fatal().Err(err).Msg("error getting hostname")