Taobin-Recipe-Manager/server/main.go

48 lines
848 B
Go
Raw Normal View History

2023-09-14 14:52:04 +07:00
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
2023-09-20 10:39:11 +07:00
s := NewServer()
2023-09-14 14:52:04 +07:00
serverCtx, serverStopCtx := context.WithCancel(context.Background())
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
<-sig
2023-09-20 10:42:14 +07:00
shutdownCtx, cancel := context.WithTimeout(serverCtx, 30*time.Second)
2023-09-14 14:52:04 +07:00
go func() {
<-shutdownCtx.Done()
if shutdownCtx.Err() == context.DeadlineExceeded {
log.Println("Shutdown timeout, force exit")
2023-09-20 10:42:14 +07:00
cancel()
2023-09-14 14:52:04 +07:00
}
}()
err := s.Shutdown(shutdownCtx)
if err != nil {
log.Fatal(err)
}
serverStopCtx()
}()
2023-10-30 10:45:33 +07:00
// Spawn a goroutine to run git pull every 10 minutes
2023-11-16 11:03:26 +07:00
//go RunGitRecipeWorker() // ** Use crontab instead **
2023-10-30 10:45:33 +07:00
2023-09-14 14:52:04 +07:00
err := s.Run()
if err != nil {
log.Fatal(err)
}
<-serverCtx.Done()
}