-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
50 lines (40 loc) · 1.2 KB
/
Copy pathmain.go
File metadata and controls
50 lines (40 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
store := NewStore()
pool := NewWorkerPool(store, 4, 100) // 4 workers, queue holds 100 pending jobs
// ctx is cancelled when the process gets SIGINT/SIGTERM (Ctrl+C, or
// `docker stop`, or a k8s pod eviction) — this is what "graceful
// shutdown" means in practice.
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
pool.Start(ctx)
server := NewServer(store, pool)
httpServer := &http.Server{
Addr: ":8080",
Handler: server.Routes(),
}
go func() {
log.Println("listening on :8080")
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("server error: %v", err)
}
}()
<-ctx.Done() // blocks here until a shutdown signal arrives
log.Println("shutdown signal received, draining...")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
log.Printf("http shutdown error: %v", err)
}
pool.Shutdown() // wait for in-flight jobs to finish before exiting
log.Println("shutdown complete")
}