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

@@ -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.