-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
89 lines (75 loc) · 2.68 KB
/
main.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync"
"ghhooks.com/hook/core"
"ghhooks.com/hook/httpinterface"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
// TODO: flag to control if github commit badge should be updated or not
// DONE: gracefull shutdown that waits for server to shutdown and also waits for current build to finish
//===========
// NOTE: CURRENT FINDINGS about graceful shutdown
// what i want is, whenever sigint signal is sent, it should drain all queued jobs but let the current processing
// job continue instead it along with draining ends the current goroutine even though i am listening for sigint signal
// to test it, use drainall function without sigint signal and it will work fine alternatively, comment draining of channel in drain function
// it will exit the current process and start another job in queue immediately
// THE problem was the sigint was being passed to commands that were started by go,
// even if I was catching the signal it was being passed to other processes too, which was cancelling the current running command
//===========
const INIT = `
┌─┐┬ ┬┬ ┬┌─┐┌─┐┬┌─┌─┐
│ ┬├─┤├─┤│ ││ │├┴┐└─┐
└─┘┴ ┴┴ ┴└─┘└─┘┴ ┴└─┘
`
func main() {
configFileLocation := flag.String("config", "example.toml", "location of config file")
httpLogger := flag.Bool("httplog", true, "log http requests (webhook push event and status request)")
addr := flag.String("addr", ":4444", "address/port pair")
flag.Parse()
l := log.New(os.Stdout, "", 0)
var wg sync.WaitGroup
err := core.ServerInit(*configFileLocation, l, &wg)
if err != nil {
log.Fatal(err)
}
r := mux.NewRouter()
httpinterface.RouterInit(r)
var handler http.Handler = r
if *httpLogger {
handler = handlers.LoggingHandler(os.Stdout, handler)
}
srv := &http.Server{
Handler: handler,
Addr: *addr,
}
fmt.Print(INIT)
log.Printf("listening on %s", srv.Addr)
// log.Fatal(srv.ListenAndServe())
// queue drain drains all jobs in queue, but lets the job that is currently underway process without interruption
httpServerCloseChan := make(chan struct{})
go func() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt)
<-sigc
fmt.Printf("\ngracefully shutting down\n")
core.Queues.DrainAll()
if err := srv.Shutdown(context.Background()); err != nil {
l.Printf("HTTP server shurdown error: %v\n", err)
}
httpServerCloseChan <- struct{}{}
}()
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
l.Fatalf("http server listen and serve error %v\n", err)
}
<-httpServerCloseChan
wg.Wait()
fmt.Println("done")
}