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.
This commit is contained in:
Sybren A. Stüvel 2022-06-17 16:24:03 +02:00
parent 857704c184
commit 7327896db9

View File

@ -10,6 +10,7 @@ import (
"net/http" "net/http"
"os" "os"
"runtime" "runtime"
"strings"
"time" "time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -18,6 +19,8 @@ import (
"git.blender.org/flamenco/pkg/api" "git.blender.org/flamenco/pkg/api"
) )
const workerNameEnvVariable = "FLAMENCO_WORKER_NAME"
var ( var (
errSignOnCanceled = errors.New("sign-on cancelled") // For example by closing the context. 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 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. // mustHostname either the hostname or logs a fatal error.
func mustHostname() string { 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() hostname, err := os.Hostname()
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("error getting hostname") log.Fatal().Err(err).Msg("error getting hostname")