HDFS-WriterApp-Fixed all the code review comments
[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
10         handler "hdfs-writer/pkg/handler"
11         pipeline "hdfs-writer/pkg/pipeline"
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 bool)
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 pipeline.ChannelMap {
33                         slogger.Info("Begin:: Closing writer goroutines::")
34                         slogger.Infof("Closing writer goroutine :: %s", eachWriter)
35                         delete(pipeline.ChannelMap, eachWriter)
36                         eachChannel <- true
37                 }
38
39                 httpServer.Shutdown(context.Background())
40                 /*once all goroutines are signalled and httpServer is shutdown,
41                 send close to main thread */
42                 connectionsClose <- true
43                 close(connectionsClose)
44         }()
45
46         // Sever starts listening
47         err := httpServer.ListenAndServe()
48         if err != nil && err != http.ErrServerClosed {
49                 slogger.Fatal(err)
50         }
51         pipeline.Wg.Wait()
52         <-connectionsClose //main thread waiting to receive close signal
53 }