Move web/task shutdown to container. Shutdown web before tasks. Add shutdown timeouts to config.
This commit is contained in:
parent
a7006b3997
commit
b2ad889f56
4 changed files with 37 additions and 40 deletions
|
|
@ -5,39 +5,37 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/mikestefanello/pagoda/pkg/handlers"
|
||||||
|
"github.com/mikestefanello/pagoda/pkg/services"
|
||||||
"github.com/mikestefanello/pagoda/pkg/tasks"
|
"github.com/mikestefanello/pagoda/pkg/tasks"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mikestefanello/pagoda/pkg/handlers"
|
|
||||||
"github.com/mikestefanello/pagoda/pkg/services"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Start a new container
|
// Start a new container.
|
||||||
c := services.NewContainer()
|
c := services.NewContainer()
|
||||||
defer func() {
|
defer func() {
|
||||||
|
// Gracefully shutdown all services.
|
||||||
if err := c.Shutdown(); err != nil {
|
if err := c.Shutdown(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Build the router
|
// Build the router.
|
||||||
if err := handlers.BuildRouter(c); err != nil {
|
if err := handlers.BuildRouter(c); err != nil {
|
||||||
log.Fatalf("failed to build the router: %v", err)
|
log.Fatalf("failed to build the router: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register all task queues
|
// Register all task queues.
|
||||||
tasks.Register(c)
|
tasks.Register(c)
|
||||||
|
|
||||||
// Start the task runner to execute queued tasks
|
// Start the task runner to execute queued tasks.
|
||||||
c.Tasks.Start(context.Background())
|
c.Tasks.Start(context.Background())
|
||||||
|
|
||||||
// Start the server
|
// Start the server.
|
||||||
go func() {
|
go func() {
|
||||||
srv := http.Server{
|
srv := http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%d", c.Config.HTTP.Hostname, c.Config.HTTP.Port),
|
Addr: fmt.Sprintf("%s:%d", c.Config.HTTP.Hostname, c.Config.HTTP.Port),
|
||||||
|
|
@ -63,30 +61,9 @@ func main() {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Wait for interrupt signal to gracefully shut down the server with a timeout of 10 seconds.
|
// Wait for interrupt signal to gracefully shut down the web server and task runner.
|
||||||
quit := make(chan os.Signal, 1)
|
quit := make(chan os.Signal, 1)
|
||||||
signal.Notify(quit, os.Interrupt)
|
signal.Notify(quit, os.Interrupt)
|
||||||
signal.Notify(quit, os.Kill)
|
signal.Notify(quit, os.Kill)
|
||||||
<-quit
|
<-quit
|
||||||
|
|
||||||
// Shutdown both the task runner and web server
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
wg.Add(2)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
c.Tasks.Stop(ctx)
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
if err := c.Web.Shutdown(ctx); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,7 @@ type (
|
||||||
ReadTimeout time.Duration
|
ReadTimeout time.Duration
|
||||||
WriteTimeout time.Duration
|
WriteTimeout time.Duration
|
||||||
IdleTimeout time.Duration
|
IdleTimeout time.Duration
|
||||||
|
ShutdownTimeout time.Duration
|
||||||
TLS struct {
|
TLS struct {
|
||||||
Enabled bool
|
Enabled bool
|
||||||
Certificate string
|
Certificate string
|
||||||
|
|
@ -109,6 +110,7 @@ type (
|
||||||
Goroutines int
|
Goroutines int
|
||||||
ReleaseAfter time.Duration
|
ReleaseAfter time.Duration
|
||||||
CleanupInterval time.Duration
|
CleanupInterval time.Duration
|
||||||
|
ShutdownTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// MailConfig stores the mail configuration
|
// MailConfig stores the mail configuration
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ http:
|
||||||
readTimeout: "5s"
|
readTimeout: "5s"
|
||||||
writeTimeout: "10s"
|
writeTimeout: "10s"
|
||||||
idleTimeout: "2m"
|
idleTimeout: "2m"
|
||||||
|
shutdownTimeout: "10s"
|
||||||
tls:
|
tls:
|
||||||
enabled: false
|
enabled: false
|
||||||
certificate: ""
|
certificate: ""
|
||||||
|
|
@ -35,6 +36,7 @@ tasks:
|
||||||
goroutines: 1
|
goroutines: 1
|
||||||
releaseAfter: "15m"
|
releaseAfter: "15m"
|
||||||
cleanupInterval: "1h"
|
cleanupInterval: "1h"
|
||||||
|
shutdownTimeout: "10s"
|
||||||
|
|
||||||
mail:
|
mail:
|
||||||
hostname: "localhost"
|
hostname: "localhost"
|
||||||
|
|
|
||||||
|
|
@ -71,15 +71,31 @@ func NewContainer() *Container {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutdown shuts the Container down and disconnects all connections.
|
// Shutdown gracefully shuts the Container down and disconnects all connections.
|
||||||
// If the task runner was started, cancel the context to shut it down prior to calling this.
|
|
||||||
func (c *Container) Shutdown() error {
|
func (c *Container) Shutdown() error {
|
||||||
|
// Shutdown the web server.
|
||||||
|
webCtx, webCancel := context.WithTimeout(context.Background(), c.Config.HTTP.ShutdownTimeout)
|
||||||
|
defer webCancel()
|
||||||
|
if err := c.Web.Shutdown(webCtx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shutdown the task runner.
|
||||||
|
taskCtx, taskCancel := context.WithTimeout(context.Background(), c.Config.Tasks.ShutdownTimeout)
|
||||||
|
defer taskCancel()
|
||||||
|
c.Tasks.Stop(taskCtx)
|
||||||
|
|
||||||
|
// Shutdown the ORM.
|
||||||
if err := c.ORM.Close(); err != nil {
|
if err := c.ORM.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shutdown the database.
|
||||||
if err := c.Database.Close(); err != nil {
|
if err := c.Database.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shutdown the cache.
|
||||||
c.Cache.Close()
|
c.Cache.Close()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue