Skip to content

Commit

Permalink
Ported config into Viper - also optimized the code a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
xBlaz3kx committed Aug 25, 2024
1 parent 9184c9c commit cc416c0
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 145 deletions.
92 changes: 30 additions & 62 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package main

import (
"context"
"errors"
"fmt"
"os/signal"
"syscall"
"time"

"github.com/GLCharge/otelzap"
"github.com/ardanlabs/conf/v3"
"github.com/spf13/cobra"
"github.com/spf13/viper"
devxCfg "github.com/xBlaz3kx/DevX/configuration"
Expand All @@ -23,24 +19,18 @@ import (
"go.uber.org/zap"
)

var build = "develop"
const serviceName = "manager"

var serviceInfo = observability.ServiceInfo{
Name: "manager",
Version: build,
Name: serviceName,
Version: "0.1.2",
}

type config struct {
conf.Version
Web struct {
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:10s"`
IdleTimeout time.Duration `conf:"default:120s"`
ShutdownTimeout time.Duration `conf:"default:20s"`
APIHost string `conf:"default:0.0.0.0:8000"`
}
DB database.Config
OpenAPI struct {
Observability observability.Config `mapstructure:"observability" yaml:"observability"`
Http devxHttp.Configuration `mapstructure:"http" yaml:"http"`
DB database.Config `mapstructure:"db" yaml:"db"`
OpenAPI struct {
Scheme string `conf:"default:http"`
Enable bool `conf:"default:true"`
Host string `conf:"default:localhost:8000"`
Expand All @@ -51,7 +41,15 @@ var rootCmd = &cobra.Command{
Use: "scheduler",
Short: "Scheduler manager",
PreRun: func(cmd *cobra.Command, args []string) {
devxCfg.SetupEnv(serviceName)
devxCfg.SetDefaults(serviceName)

viper.SetDefault("storage.encryption.key", "ishouldreallybechanged")
viper.SetDefault("db.disable_tls", true)
viper.SetDefault("db.max_open_conns", 1)
viper.SetDefault("db.max_idle_conns", 10)
viper.SetDefault("observability.logging.level", observability.LogLevelInfo)

devxCfg.InitConfig("", "./config", ".")

postgres.SetEncryptor(security.NewEncryptorFromEnv())
Expand All @@ -62,7 +60,6 @@ var rootCmd = &cobra.Command{
func main() {
cobra.OnInitialize(func() {
logger.SetupLogging()
devxCfg.SetupEnv("manager")
})
err := rootCmd.Execute()
if err != nil {
Expand All @@ -71,48 +68,27 @@ func main() {
}

func runCmd(cmd *cobra.Command, args []string) {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
defer cancel()

obsConfig := observability.Config{}
obs, err := observability.NewObservability(ctx, serviceInfo, obsConfig)
// Configuration
cfg := &config{}
devxCfg.GetConfiguration(viper.GetViper(), cfg)

// Setup observability
obs, err := observability.NewObservability(ctx, serviceInfo, cfg.Observability)
if err != nil {
otelzap.L().Fatal("failed to initialize observability", zap.Error(err))
}

log := obs.Log()

// Configuration
cfg := config{
Version: conf.Version{
Build: build,
Desc: "copyright information here",
},
}

const prefix = "MANAGER"
help, err := conf.Parse(prefix, &cfg)
if err != nil {
if errors.Is(err, conf.ErrHelpWanted) {
fmt.Println(help)
return
}
return
}

// App Starting
log.Info("starting service", zap.String("version", build))
log.Info("Starting the manager", zap.String("version", serviceInfo.Version), zap.Any("config", cfg))
defer log.Info("shutdown complete")

out, err := conf.String(&cfg)
if err != nil {
return
}

log.Info("startup", zap.String("config", out))

// Database Support
log.Info("startup", zap.String("status", "initializing database support"), zap.String("host", cfg.DB.Host))
log.Info("Connecting to the database", zap.String("host", cfg.DB.Host))
db, err := database.Open(database.Config{
User: cfg.DB.User,
Password: cfg.DB.Password,
Expand All @@ -123,17 +99,15 @@ func runCmd(cmd *cobra.Command, args []string) {
DisableTLS: cfg.DB.DisableTLS,
})
if err != nil {
return
log.Fatal("failed to connect to the database", zap.Error(err))
}

defer func() {
log.Info("shutdown", zap.String("status", "stopping database support"), zap.String("host", cfg.DB.Host))
log.Info("Closing the database connection")
_ = db.Close()
}()

httpCfg := devxHttp.Configuration{Address: cfg.Web.APIHost}
httpServer := devxHttp.NewServer(httpCfg, obs)

httpServer := devxHttp.NewServer(cfg.Http, obs)
api.Api(httpServer.Router(), api.APIMuxConfig{
Log: log,
DB: db,
Expand All @@ -145,18 +119,12 @@ func runCmd(cmd *cobra.Command, args []string) {
})

go func() {
log.Info("Starting HTTP server", zap.String("host", httpCfg.Address))
log.Info("Starting HTTP server", zap.String("host", cfg.Http.Address))
databaseCheck := database.NewHealthChecker(db)
httpServer.Run(databaseCheck)
}()

// Start API Service
log.Info("startup", zap.String("status", "initializing Management API support"))

// Shutdown
select {
case sig := <-ctx.Done():
log.Info("shutdown", zap.String("status", "shutdown started"), zap.Any("signal", sig))
defer log.Info("shutdown", zap.String("status", "shutdown complete"), zap.Any("signal", sig))
}
<-ctx.Done()
log.Info("Shutting down the manager")
}
100 changes: 39 additions & 61 deletions cmd/runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package main

import (
"context"
"errors"
"fmt"
"net/http"
"os/signal"
"syscall"
"time"

"github.com/GLCharge/otelzap"
"github.com/ardanlabs/conf/v3"
"github.com/spf13/cobra"
"github.com/spf13/viper"
devxCfg "github.com/xBlaz3kx/DevX/configuration"
Expand All @@ -26,34 +23,38 @@ import (
"go.uber.org/zap"
)

var build = "develop"
const serviceName = "runner"

var serviceInfo = observability.ServiceInfo{
Name: "runner",
Version: build,
Name: serviceName,
Version: "0.1.2",
}

type config struct {
conf.Version
Web struct {
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:10s"`
IdleTimeout time.Duration `conf:"default:120s"`
ShutdownTimeout time.Duration `conf:"default:20s"`
APIHost string `conf:"default:0.0.0.0:8000"`
}
DB database.Config
ID string `conf:"default:instance1"`
Interval time.Duration `conf:"default:10s"`
MaxConcurrentJobs int `conf:"default:100"`
MaxJobLockTime time.Duration `conf:"default:1m"`
Observability observability.Config `mapstructure:"observability" yaml:"observability"`
Http devxHttp.Configuration `mapstructure:"http" yaml:"http"`
DB database.Config `mapstructure:"db" yaml:"db"`
ID string `conf:"default:instance1" yaml:"id"`
JobExecutionSettings runner.JobExecutionSettings `mapstructure:"job_execution_settings" yaml:"jobExecutionSettings"`
}

var rootCmd = &cobra.Command{
Use: "runner",
Short: "Scheduler runner",
PreRun: func(cmd *cobra.Command, args []string) {
devxCfg.SetupEnv(serviceName)
devxCfg.SetDefaults(serviceName)

viper.SetDefault("storage.encryption.key", "ishouldreallybechanged")
viper.SetDefault("db.disable_tls", true)
viper.SetDefault("db.max_open_conns", 1)
viper.SetDefault("db.max_idle_conns", 10)
viper.SetDefault("observability.logging.level", observability.LogLevelInfo)

viper.SetDefault("job_execution_settings.max_concurrent_jobs", 100)
viper.SetDefault("job_execution_settings.interval", time.Second*10)
viper.SetDefault("job_execution_settings.max_job_lock_time", time.Minute)

devxCfg.InitConfig("", "./config", ".")

postgres.SetEncryptor(security.NewEncryptorFromEnv())
Expand All @@ -64,7 +65,6 @@ var rootCmd = &cobra.Command{
func main() {
cobra.OnInitialize(func() {
logger.SetupLogging()
devxCfg.SetupEnv("runner")
})
err := rootCmd.Execute()
if err != nil {
Expand All @@ -73,48 +73,28 @@ func main() {
}

func runCmd(cmd *cobra.Command, args []string) {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
defer cancel()

obsConfig := observability.Config{}
obs, err := observability.NewObservability(ctx, serviceInfo, obsConfig)
// Read the configuration
cfg := &config{}
devxCfg.GetConfiguration(viper.GetViper(), cfg)

obs, err := observability.NewObservability(ctx, serviceInfo, cfg.Observability)
if err != nil {
otelzap.L().Fatal("failed to initialize observability", zap.Error(err))
}

log := obs.Log()

// config
cfg := config{
Version: conf.Version{
Build: build,
Desc: "copyright information here",
},
}

const prefix = "RUNNER"
help, err := conf.Parse(prefix, &cfg)
if err != nil {
if errors.Is(err, conf.ErrHelpWanted) {
fmt.Println(help)
return
}
return
}

// App Starting
log.Info("starting service", zap.String("version", build))
log.Info("Starting the runner", zap.String("version", serviceInfo.Version))
defer log.Info("shutdown complete")

out, err := conf.String(&cfg)
if err != nil {
log.Fatal("parsing config", zap.Error(err))
}

log.Info("Using config", zap.Any("config", out))
log.Info("Using config", zap.Any("config", cfg))

// Database
log.Info("startup", zap.String("status", "initializing database support"), zap.String("host", cfg.DB.Host))
log.Info("Connecting to the database", zap.String("host", cfg.DB.Host))
db, err := database.Open(database.Config{
User: cfg.DB.User,
Password: cfg.DB.Password,
Expand All @@ -127,8 +107,9 @@ func runCmd(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal("Unable to establish DB connection", zap.Error(err))
}

defer func() {
log.Info("closing database connection")
log.Info("Closing the database connection")
_ = db.Close()
}()

Expand All @@ -142,33 +123,30 @@ func runCmd(cmd *cobra.Command, args []string) {
executorFactory := executor.NewFactory(&http.Client{Timeout: 30 * time.Second})

runner := runner.New(runner.Config{
JobService: jobService,
Log: log,
ExecutorFactory: executorFactory,
InstanceId: cfg.ID,
Interval: cfg.Interval,
MaxConcurrentJobs: cfg.MaxConcurrentJobs,
JobLockDuration: cfg.MaxJobLockTime,
JobService: jobService,
Log: log,
ExecutorFactory: executorFactory,
InstanceId: cfg.ID,
JobExecution: cfg.JobExecutionSettings,
})
runner.Start()

httpServer := devxHttp.NewServer(devxHttp.Configuration{Address: cfg.Web.APIHost}, observability.NewNoopObservability())
httpServer := devxHttp.NewServer(cfg.Http, observability.NewNoopObservability())
go func() {
log.Info("Started HTTP server", zap.String("host", cfg.Web.APIHost))
log.Info("Started HTTP server", zap.String("address", cfg.Http.Address))
databaseCheck := database.NewHealthChecker(db)
httpServer.Run(databaseCheck)
}()

//nolint:all
select {
case _ = <-ctx.Done():
log.Info("shutdown", zap.String("status", "shutdown started"))
log.Info("Shutting down the runner")

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// stop the runner
runner.Stop(ctx)
log.Info("shutdown", zap.String("status", "shutdown complete"))
}
}
12 changes: 10 additions & 2 deletions docker-compose.local-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ services:
ports:
- "8000:8000"
environment:
- MANAGER_OBSERVABILITY_LOG_LEVEL=info
- MANAGER_DB_HOST=postgres:5432
- MANAGER_DB_USER=scheduler
- MANAGER_DB_PASS=scheduler
- MANAGER_DB_NAME=scheduler
- MANAGER_DB_PASSWORD=scheduler
- MANAGER_DB_DISABLETLS=true
- MANAGER_STORAGE_ENCRYPTION_KEY=ishouldbechanged
#volumes:
# - ./cmd/manager/config.yaml:/app/config.yaml
depends_on:
- postgres
- migration
Expand All @@ -40,10 +44,14 @@ services:
dockerfile: build/runner/Dockerfile
container_name: runner
restart: always
#volumes:
# - ./cmd/runner/config.yaml:/app/config.yaml
environment:
- RUNNER_OBSERVABILITY_LOG_LEVEL=info
- RUNNER_DB_HOST=postgres:5432
- RUNNER_DB_USER=scheduler
- RUNNER_DB_PASS=scheduler
- RUNNER_DB_PASSWORD=scheduler
- RUNNER_DB_NAME=scheduler
- RUNNER_DB_DISABLETLS=true
- RUNNER_STORAGE_ENCRYPTION_KEY=ishouldbechanged
depends_on:
Expand Down
Loading

0 comments on commit cc416c0

Please sign in to comment.