aboutsummaryrefslogtreecommitdiffstats
path: root/vnfs/DAaaS/microservices/GoApps/src/go-hdfs-writer/cmd/hdfs-writer/main.go
blob: 4f3cfcbc2df17c7346b5351c8ad6ec6168b648e5 (plain)
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
package main

import (
	"context"
	"fmt"
	"net/http"
	"os"
	"os/signal"

	handler "hdfs-writer/pkg/handler"
	pipeline "hdfs-writer/pkg/pipeline"
	utils "hdfs-writer/pkg/utils"
)

func main() {

	slogger := utils.GetLoggerInstance()

	// Create the server
	httpServer := &http.Server{
		Handler: handler.CreateRouter(),
		Addr:    ":9393",
	}

	connectionsClose := make(chan bool)
	go func() {
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt)
		<-c // function literal waiting to receive Interrupt signal
		fmt.Printf(":::Got the kill signal:::")
		slogger.Info(":::Got the kill signal:::")
		for eachWriter, eachChannel := range pipeline.ChannelMap {
			slogger.Info("Begin:: Closing writer goroutines::")
			slogger.Infof("Closing writer goroutine :: %s", eachWriter)
			delete(pipeline.ChannelMap, eachWriter)
			eachChannel <- true
		}

		httpServer.Shutdown(context.Background())
		/*once all goroutines are signalled and httpServer is shutdown,
		send close to main thread */
		connectionsClose <- true
		close(connectionsClose)
	}()

	// Sever starts listening
	err := httpServer.ListenAndServe()
	if err != nil && err != http.ErrServerClosed {
		slogger.Fatal(err)
	}
	pipeline.Wg.Wait()
	<-connectionsClose //main thread waiting to receive close signal
}