a79a3e060d6d9d30459c70cdc6bbeed197eec48b
[demo.git] / vnfs / DAaaS / microservices / GoApps / src / go-hdfs-writer / cmd / hdfs-writer / main.go
1 package main
2
3 import (
4         "context"
5         "fmt"
6         "net/http"
7         "os"
8         "os/signal"
9         "time"
10
11         handler "hdfs-writer/pkg/handler"
12         utils "hdfs-writer/pkg/utils"
13 )
14
15 func main() {
16
17         slogger := utils.GetLoggerInstance()
18
19         // Create the server
20         httpServer := &http.Server{
21                 Handler: handler.CreateRouter(),
22                 Addr:    ":9393",
23         }
24
25         connectionsClose := make(chan struct{})
26         go func() {
27                 c := make(chan os.Signal, 1)
28                 signal.Notify(c, os.Interrupt)
29                 <-c // function literal waiting to receive Interrupt signal
30                 fmt.Printf(":::Got the kill signal:::")
31                 slogger.Info(":::Got the kill signal:::")
32                 for eachWriter, eachChannel := range handler.ChannelMap {
33                         slogger.Infof("Closing writer goroutine :: %s", eachWriter)
34                         slogger.Infof("eachChannel:: %v", eachChannel)
35                         close(eachChannel)
36                         // This wait time ensures that the each of the channel is killed before
37                         // main routine finishes.
38                         time.Sleep(time.Second * 5)
39                 }
40                 //once all goroutines are signalled, send close to main thread
41                 httpServer.Shutdown(context.Background())
42                 close(connectionsClose)
43         }()
44
45         // Sever starts listening
46         err := httpServer.ListenAndServe()
47         if err != nil && err != http.ErrServerClosed {
48                 slogger.Fatal(err)
49         }
50         <-connectionsClose //main thread waiting to receive close signal
51 }