From 4b8f5598f0eb97a8656f1d5fa73806ecc9bd1e82 Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Thu, 2 Nov 2023 10:23:12 -0700 Subject: [PATCH] fix: infer nats server name FLY_ALLOC_ID --- cli/options.go | 7 +++++++ config/presets.go | 6 ++++++ config/presets_test.go | 1 + 3 files changed, 14 insertions(+) diff --git a/cli/options.go b/cli/options.go index 7979bc58..2bbf0f4f 100644 --- a/cli/options.go +++ b/cli/options.go @@ -508,6 +508,13 @@ func embeddedNatsCLIFlags(c *config.Config, routes *string, gateways *string) [] Destination: &c.EmbeddedNats.StoreDir, }, + &cli.StringFlag{ + Name: "enats_server_name", + Usage: "Embedded NATS unique server name (required for JetStream), auto-generated by default", + Value: c.EmbeddedNats.Name, + Destination: &c.EmbeddedNats.Name, + }, + &cli.BoolFlag{ Name: "enats_debug", Usage: "Enable NATS server logs", diff --git a/config/presets.go b/config/presets.go index bf906484..db78b51e 100644 --- a/config/presets.go +++ b/config/presets.go @@ -66,6 +66,8 @@ func (c *Config) loadFlyPreset(defaults *Config) error { return errors.New("FLY_APP_NAME env is missing") } + appId, _ := os.LookupEnv("FLY_ALLOC_ID") + redisEnabled := (c.Redis.URL != defaults.Redis.URL) // Use the same port for HTTP broadcasts by default @@ -73,6 +75,10 @@ func (c *Config) loadFlyPreset(defaults *Config) error { c.HTTPBroadcast.Port = c.Port } + if c.EmbeddedNats.Name == defaults.EmbeddedNats.Name && appId != "" { + c.EmbeddedNats.Name = fmt.Sprintf("fly-%s-%s", region, appId) + } + if c.EmbeddedNats.ServiceAddr == defaults.EmbeddedNats.ServiceAddr { c.EmbeddedNats.ServiceAddr = "nats://0.0.0.0:4222" } diff --git a/config/presets_test.go b/config/presets_test.go index bf69e57c..d7717b5a 100644 --- a/config/presets_test.go +++ b/config/presets_test.go @@ -43,6 +43,7 @@ func TestFlyPresets(t *testing.T) { assert.Equal(t, true, config.EmbedNats) assert.Equal(t, "nats", config.PubSubAdapter) assert.Equal(t, "", config.BrokerAdapter) + assert.Equal(t, "fly-mag-1234", config.EmbeddedNats.Name) assert.Equal(t, "nats://0.0.0.0:4222", config.EmbeddedNats.ServiceAddr) assert.Equal(t, "nats://0.0.0.0:5222", config.EmbeddedNats.ClusterAddr) assert.Equal(t, "any-test-mag-cluster", config.EmbeddedNats.ClusterName)