Skip to content

Commit 4694193

Browse files
authored
Merge pull request #55 from gatewayd-io/exit-on-startup-error
Add option whether to exit on startup error or not
2 parents a2f1df3 + e962c27 commit 4694193

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

gatewayd_plugin.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ plugins:
2828
- PERIODIC_INVALIDATOR_INTERVAL=1m
2929
- PERIODIC_INVALIDATOR_START_DELAY=1m
3030
- API_ADDRESS=localhost:18080
31+
- EXIT_ON_STARTUP_ERROR=False
3132
checksum: 3988e10aefce2cd9b30888eddd2ec93a431c9018a695aea1cea0dac46ba91cae

main.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func main() {
3333
})
3434

3535
var metricsConfig *metrics.MetricsConfig
36+
//nolint:nestif
3637
if cfg := cast.ToStringMap(plugin.PluginConfig["config"]); cfg != nil {
3738
metricsConfig = metrics.NewMetricsConfig(cfg)
3839
if metricsConfig != nil && metricsConfig.Enabled {
@@ -43,20 +44,27 @@ func main() {
4344
pluginInstance.Impl.Expiry = cast.ToDuration(cfg["expiry"])
4445
pluginInstance.Impl.DefaultDBName = cast.ToString(cfg["defaultDBName"])
4546
pluginInstance.Impl.ScanCount = cast.ToInt64(cfg["scanCount"])
47+
pluginInstance.Impl.ExitOnStartupError = cast.ToBool(cfg["exitOnStartupError"])
4648

4749
redisConfig, err := redis.ParseURL(pluginInstance.Impl.RedisURL)
4850
if err != nil {
4951
logger.Error("Failed to parse Redis URL", "error", err)
50-
os.Exit(1)
52+
if pluginInstance.Impl.ExitOnStartupError {
53+
logger.Info("Exiting due to startup error")
54+
os.Exit(1)
55+
}
5156
}
5257

5358
pluginInstance.Impl.RedisClient = redis.NewClient(redisConfig)
5459

5560
// Ping the Redis server to check if it is available.
5661
_, err = pluginInstance.Impl.RedisClient.Ping(context.Background()).Result()
5762
if err != nil {
58-
logger.Error("Failed to ping Redis server, plugin exited", "error", err)
59-
os.Exit(1)
63+
logger.Error("Failed to ping Redis server", "error", err)
64+
if pluginInstance.Impl.ExitOnStartupError {
65+
logger.Info("Exiting due to startup error")
66+
os.Exit(1)
67+
}
6068
}
6169

6270
pluginInstance.Impl.PeriodicInvalidatorEnabled = cast.ToBool(

plugin/module.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ var (
4343
"PERIODIC_INVALIDATOR_START_DELAY", "1m"),
4444
"periodicInvalidatorInterval": sdkConfig.GetEnv(
4545
"PERIODIC_INVALIDATOR_INTERVAL", "1m"),
46-
"apiAddress": sdkConfig.GetEnv("API_ADDRESS", "localhost:8080"),
46+
"apiAddress": sdkConfig.GetEnv("API_ADDRESS", "localhost:8080"),
47+
"exitOnStartupError": sdkConfig.GetEnv("EXIT_ON_STARTUP_ERROR", "false"),
4748
},
4849
"hooks": []interface{}{
4950
int32(v1.HookName_HOOK_NAME_ON_CLOSED),

plugin/plugin.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ type Plugin struct {
2424
Logger hclog.Logger
2525

2626
// Cache configuration.
27-
RedisClient *goRedis.Client
28-
RedisURL string
29-
Expiry time.Duration
30-
DefaultDBName string
31-
ScanCount int64
27+
RedisClient *goRedis.Client
28+
RedisURL string
29+
Expiry time.Duration
30+
DefaultDBName string
31+
ScanCount int64
32+
ExitOnStartupError bool
3233

3334
// Periodic invalidator configuration.
3435
PeriodicInvalidatorEnabled bool

0 commit comments

Comments
 (0)