test cron job
Some checks are pending
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
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-09-13 14:50:04 +10:00
parent 144d887bda
commit 5042c4bfef
12 changed files with 268 additions and 69 deletions

View File

@@ -44,6 +44,8 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
prettyPrint(event)
}
e := event.CloudEvent.Data
// Convert vmModel to CreateInventoryParams using the utility function
//var params queries.CreateInventoryParams
//db.ConvertToSQLParams(&vm, &params)
@@ -65,15 +67,15 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
vc.Login(event.CloudEvent.Source)
//vmObject, err := vc.FindVMByName(vm.CloudEvent.Data.VM.Name)
//vmObject, err := vc.FindVMByID(vm.CloudEvent.Data.VM.VM.Value)
vmObject, err := vc.FindVMByIDWithDatacenter(event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Datacenter.Value)
vmObject, err := vc.FindVMByIDWithDatacenter(e.VM.VM.Value, e.Datacenter.Datacenter.Value)
if err != nil {
h.Logger.Error("Can't locate vm in vCenter", "vmID", event.CloudEvent.Data.VM.VM.Value, "error", err)
h.Logger.Error("Can't locate vm in vCenter", "vmID", e.VM.VM.Value, "error", err)
} else if vmObject == nil {
h.Logger.Debug("didn't find VM", "vm_id", event.CloudEvent.Data.VM.VM.Value)
h.Logger.Debug("didn't find VM", "vm_id", e.VM.VM.Value)
numRam = 0
numVcpus = 0
datacenter = event.CloudEvent.Data.Datacenter.Name
datacenter = e.Datacenter.Name
} else {
h.Logger.Debug("found VM")
//prettyPrint(vmObject)
@@ -95,15 +97,16 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
// Create an instance of CreateInventoryParams
h.Logger.Debug("Creating database parameters")
/*
params := queries.CreateInventoryParams{
Name: event.CloudEvent.Data.VM.Name,
Name: e.VM.Name,
Vcenter: event.CloudEvent.Source,
EventId: sql.NullString{String: event.CloudEvent.ID, Valid: event.CloudEvent.ID != ""},
EventKey: sql.NullString{String: strconv.Itoa(event.CloudEvent.Data.Key), Valid: strconv.Itoa(event.CloudEvent.Data.Key) != ""},
VmId: sql.NullString{String: event.CloudEvent.Data.VM.VM.Value, Valid: event.CloudEvent.Data.VM.VM.Value != ""},
EventKey: sql.NullString{String: strconv.Itoa(e.Key), Valid: strconv.Itoa(e.Key) != ""},
VmId: sql.NullString{String: e.VM.VM.Value, Valid: e.VM.VM.Value != ""},
Datacenter: sql.NullString{String: datacenter, Valid: datacenter != ""},
Cluster: sql.NullString{String: event.CloudEvent.Data.ComputeResource.Name, Valid: event.CloudEvent.Data.ComputeResource.Name != ""},
Cluster: sql.NullString{String: e.ComputeResource.Name, Valid: e.ComputeResource.Name != ""},
CreationTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0},
InitialVcpus: sql.NullInt64{Int64: int64(numVcpus), Valid: numVcpus > 0},
InitialRam: sql.NullInt64{Int64: int64(numRam), Valid: numRam > 0},
@@ -111,15 +114,18 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
*/
params2 := queries.CreateEventParams{
Source: event.CloudEvent.Source,
CloudId: event.CloudEvent.ID,
EventTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0},
ChainId: strconv.Itoa(event.CloudEvent.Data.ChainID),
VmId: sql.NullString{String: event.CloudEvent.Data.VM.VM.Value, Valid: event.CloudEvent.Data.VM.VM.Value != ""},
EventKey: sql.NullString{String: strconv.Itoa(event.CloudEvent.Data.Key), Valid: strconv.Itoa(event.CloudEvent.Data.Key) != ""},
Datacenter: sql.NullString{String: event.CloudEvent.Data.Datacenter.Name, Valid: event.CloudEvent.Data.Datacenter.Name != ""},
ComputeResource: sql.NullString{String: event.CloudEvent.Data.ComputeResource.Name, Valid: event.CloudEvent.Data.ComputeResource.Name != ""},
UserName: sql.NullString{String: event.CloudEvent.Data.UserName, Valid: event.CloudEvent.Data.UserName != ""},
Source: event.CloudEvent.Source,
CloudId: event.CloudEvent.ID,
EventTime: sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0},
ChainId: strconv.Itoa(e.ChainID),
VmId: sql.NullString{String: e.VM.VM.Value, Valid: e.VM.VM.Value != ""},
VmName: sql.NullString{String: e.VM.Name, Valid: e.VM.Name != ""},
EventKey: sql.NullString{String: strconv.Itoa(e.Key), Valid: strconv.Itoa(e.Key) != ""},
DatacenterName: sql.NullString{String: e.Datacenter.Name, Valid: e.Datacenter.Name != ""},
DatacenterId: sql.NullString{String: e.Datacenter.Datacenter.Value, Valid: e.Datacenter.Datacenter.Value != ""},
ComputeResourceName: sql.NullString{String: e.ComputeResource.Name, Valid: e.ComputeResource.Name != ""},
ComputeResourceId: sql.NullString{String: e.ComputeResource.ComputeResource.Value, Valid: e.ComputeResource.ComputeResource.Value != ""},
UserName: sql.NullString{String: e.UserName, Valid: e.UserName != ""},
}
h.Logger.Debug("database params", "params", params2)
@@ -140,6 +146,7 @@ func (h *Handler) VmCreate(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Create Request : %v\n", result)
}
// prettyPrint comes from https://gist.github.com/sfate/9d45f6c5405dc4c9bf63bf95fe6d1a7c
func prettyPrint(args ...interface{}) {
var caller string

View File

@@ -8,19 +8,23 @@ import (
"os"
"os/signal"
"time"
"github.com/go-co-op/gocron/v2"
)
// Server represents an HTTP server.
type Server struct {
srv *http.Server
logger *slog.Logger
cron gocron.Scheduler
cancel context.CancelFunc
disableTls bool
tlsCertFilename string
tlsKeyFilename string
}
// New creates a new server with the given logger, address and options.
func New(logger *slog.Logger, addr string, opts ...Option) *Server {
func New(logger *slog.Logger, cron gocron.Scheduler, cancel context.CancelFunc, addr string, opts ...Option) *Server {
// Set some options for TLS
tlsConfig := &tls.Config{
@@ -50,6 +54,8 @@ func New(logger *slog.Logger, addr string, opts ...Option) *Server {
return &Server{
srv: srv,
logger: logger,
cron: cron,
cancel: cancel,
}
}
@@ -113,7 +119,6 @@ func (s *Server) Start() {
s.logger.Warn("failed to start server", "error", err)
}
}
}()
}
@@ -133,6 +138,16 @@ func (s *Server) GracefulShutdown() {
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
_ = s.srv.Shutdown(ctx)
s.logger.Info("runing cron shutdown")
err := s.cron.Shutdown()
if err != nil {
s.logger.Error("error shutting cron", "error", err)
}
s.logger.Info("runing cancel")
s.cancel()
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.