Skip to content

Commit

Permalink
test: shutdown priority
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Oct 25, 2024
1 parent 507d071 commit a0c1636
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
39 changes: 27 additions & 12 deletions shutdown/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/signal"
"sync"
"time"

"github.com/flanksource/commons/logger"
)
Expand All @@ -18,19 +19,30 @@ const (

type shutdownHook func()

var m sync.Mutex

var shutdownTaskRegistry ShutdownTasks
var (
registryLock sync.Mutex
shutdownTaskRegistry ShutdownTasks
)

func init() {
heap.Init(&shutdownTaskRegistry)
}

var Shutdown = sync.OnceFunc(func() {
logger.Infof("shutting down")
for _, task := range shutdownTaskRegistry {
logger.Infof("begin shutdown")

for len(shutdownTaskRegistry) > 0 {
_task := heap.Pop(&shutdownTaskRegistry)
if _task == nil {
break
}

task := _task.(ShutdownTask)
logger.Infof("shutting down: %s", task.Label)

s := time.Now()
task.Hook()
logger.Infof("shutdown %s completed in %v", task.Label, time.Since(s))
}
})

Expand All @@ -40,19 +52,23 @@ func ShutdownAndExit(code int, msg string) {
os.Exit(code)
}

// @Deprecated
// Add a hook with the least priority.
// Least priority hooks are run first.
//
// Prefer AddHookWithPriority()
func AddHook(fn shutdownHook) {
m.Lock()
registryLock.Lock()
heap.Push(&shutdownTaskRegistry, ShutdownTask{Hook: fn, Priority: 0})
m.Unlock()
registryLock.Unlock()
}

// AddHookWithPriority adds a hook with a priority level.
//
// Execution order goes from lowest to highest priority numbers.
func AddHookWithPriority(label string, priority int, fn shutdownHook) {
m.Lock()
registryLock.Lock()
heap.Push(&shutdownTaskRegistry, ShutdownTask{Label: label, Hook: fn, Priority: priority})
m.Unlock()
registryLock.Unlock()
}

func WaitForSignal() {
Expand All @@ -77,9 +93,8 @@ type ShutdownTasks []ShutdownTask

func (st ShutdownTasks) Len() int { return len(st) }

// Less defines higher priority numbers will be processed first
func (st ShutdownTasks) Less(i, j int) bool {
return st[i].Priority > st[j].Priority // Higher priority numbers come first
return st[i].Priority < st[j].Priority
}

func (st ShutdownTasks) Swap(i, j int) {
Expand Down
25 changes: 25 additions & 0 deletions shutdown/shutdown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package shutdown

import "testing"

func TestShutdownPriority(t *testing.T) {
var lastClosed int

add := func(label string, priority int) {
AddHookWithPriority(label, priority, func() {
if lastClosed > priority {
t.Fatalf("something higher priority (%d) was closed earlier than (%d)", lastClosed, priority)
} else {
lastClosed = priority
}
})
}

add("database", PriorityCritical)
add("gRPC", PriorityIngress)
add("checkJob", PriorityJobs)
add("echo", PriorityIngress)
add("topologyJob", PriorityJobs)

Shutdown()
}

0 comments on commit a0c1636

Please sign in to comment.