test cron job
This commit is contained in:
52
main.go
52
main.go
@@ -1,15 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
"vctp/db"
|
||||
"vctp/internal/tasks"
|
||||
utils "vctp/internal/utils"
|
||||
"vctp/log"
|
||||
"vctp/server"
|
||||
"vctp/server/router"
|
||||
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
@@ -17,6 +21,7 @@ var (
|
||||
bindDisableTls bool
|
||||
sha1ver string // sha1 revision used to build the program
|
||||
buildTime string // when the executable was built
|
||||
cronFrequency time.Duration
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -32,6 +37,8 @@ func main() {
|
||||
log.GetOutput(),
|
||||
)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
// Configure database
|
||||
database, err := db.New(logger, "./db.sqlite3")
|
||||
if err != nil {
|
||||
@@ -45,6 +52,19 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// Prepare the task scheduler
|
||||
s, err := gocron.NewScheduler()
|
||||
if err != nil {
|
||||
logger.Error("failed to create scheduler", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Pass useful information to the cron jobs
|
||||
c := &tasks.CronTask{
|
||||
Logger: logger,
|
||||
Database: database,
|
||||
}
|
||||
|
||||
// Determine bind IP
|
||||
bindIP := os.Getenv("BIND_IP")
|
||||
if bindIP == "" {
|
||||
@@ -85,9 +105,41 @@ func main() {
|
||||
utils.GenerateCerts(tlsCertFilename, tlsKeyFilename)
|
||||
}
|
||||
|
||||
cronFrequencyString := os.Getenv("VCENTER_POLLING_SECONDS")
|
||||
if cronFrequencyString != "" {
|
||||
cronFrequency, err = time.ParseDuration(cronFrequencyString)
|
||||
if err != nil {
|
||||
slog.Error("Can't convert VCENTER_POLLING_SECONDS value to time duration. Defaulting to 60s", "value", cronFrequencyString, "error", err)
|
||||
cronFrequency = time.Second * 60
|
||||
}
|
||||
} else {
|
||||
cronFrequency = time.Second * 60
|
||||
}
|
||||
logger.Debug("Setting VM polling cronjob frequency to", "frequency", cronFrequency)
|
||||
|
||||
// start background processing
|
||||
startsAt := time.Now().Add(time.Second * 10)
|
||||
job, err := s.NewJob(
|
||||
gocron.DurationJob(cronFrequency),
|
||||
gocron.NewTask(func() {
|
||||
c.RunVmCheck(ctx, logger)
|
||||
}), gocron.WithSingletonMode(gocron.LimitModeReschedule),
|
||||
gocron.WithStartAt(gocron.WithStartDateTime(startsAt)),
|
||||
)
|
||||
if err != nil {
|
||||
logger.Error("failed to start cron jobs", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
slog.Debug("Created cron job", "job", job)
|
||||
|
||||
s.Start()
|
||||
|
||||
// Start server
|
||||
svr := server.New(
|
||||
logger,
|
||||
s,
|
||||
cancel,
|
||||
bindAddress,
|
||||
server.WithRouter(router.New(logger, database)),
|
||||
)
|
||||
|
Reference in New Issue
Block a user