add settings yaml
Some checks are pending
continuous-integration/drone/push Build is passing
CI / Lint (push) Waiting to run
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions

This commit is contained in:
2024-09-26 17:24:36 +10:00
parent 44c4bb2d66
commit dcbbff830d
7 changed files with 128 additions and 19 deletions

47
main.go
View File

@@ -9,6 +9,7 @@ import (
"runtime"
"time"
"vctp/db"
"vctp/internal/settings"
"vctp/internal/tasks"
utils "vctp/internal/utils"
"vctp/log"
@@ -55,17 +56,19 @@ func main() {
os.Exit(1)
}
// Prepare the task scheduler
s, err := gocron.NewScheduler()
if err != nil {
logger.Error("failed to create scheduler", "error", err)
os.Exit(1)
// Load settings from yaml
settingsFile := os.Getenv("SETTINGS_FILE")
if settingsFile == "" {
settingsFile = "settings.yaml"
}
// Pass useful information to the cron jobs
c := &tasks.CronTask{
Logger: logger,
Database: database,
// TODO - how to pass this to the other packages that will need this info?
s, err := settings.ReadYMLSettings(logger, settingsFile)
if err != nil {
logger.Error("failed to open yaml settings file", "error", err, "filename", settingsFile)
//os.Exit(1)
} else {
logger.Debug("Loaded yaml settings", "contents", s)
}
// Determine bind IP
@@ -108,6 +111,20 @@ func main() {
utils.GenerateCerts(tlsCertFilename, tlsKeyFilename)
}
// Prepare the task scheduler
c, err := gocron.NewScheduler()
if err != nil {
logger.Error("failed to create scheduler", "error", err)
os.Exit(1)
}
// Pass useful information to the cron jobs
ct := &tasks.CronTask{
Logger: logger,
Database: database,
Settings: s,
}
cronFrequencyString := os.Getenv("VCENTER_POLLING_SECONDS")
if cronFrequencyString != "" {
cronFrequency, err = time.ParseDuration(cronFrequencyString)
@@ -122,10 +139,10 @@ func main() {
// start background processing for events stored in events table
startsAt := time.Now().Add(time.Second * 10)
job, err := s.NewJob(
job, err := c.NewJob(
gocron.DurationJob(cronFrequency),
gocron.NewTask(func() {
c.RunVmCheck(ctx, logger)
ct.RunVmCheck(ctx, logger)
}), gocron.WithSingletonMode(gocron.LimitModeReschedule),
gocron.WithStartAt(gocron.WithStartDateTime(startsAt)),
)
@@ -136,10 +153,10 @@ func main() {
logger.Debug("Created event processing cron job", "job", job.ID())
startsAt2 := time.Now().Add(time.Second * 10)
job2, err := s.NewJob(
job2, err := c.NewJob(
gocron.DurationJob(cronFrequency),
gocron.NewTask(func() {
c.RunVcenterPoll(ctx, logger)
ct.RunVcenterPoll(ctx, logger)
}), gocron.WithSingletonMode(gocron.LimitModeReschedule),
gocron.WithStartAt(gocron.WithStartDateTime(startsAt2)),
)
@@ -150,12 +167,12 @@ func main() {
logger.Debug("Created vcenter polling cron job", "job", job2.ID())
// start cron scheduler
s.Start()
c.Start()
// Start server
svr := server.New(
logger,
s,
c,
cancel,
bindAddress,
server.WithRouter(router.New(logger, database, buildTime, sha1ver, runtime.Version())),