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

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

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

func main() {

	slogger := utils.GetLoggerInstance()

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

	connectionsClose := make(chan struct{})
	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 handler.ChannelMap {
			slogger.Infof("Closing writer goroutine :: %s", eachWriter)
			slogger.Infof("eachChannel:: %v", eachChannel)
			close(eachChannel)
			// This wait time ensures that the each of the channel is killed before
			// main routine finishes.
			time.Sleep(time.Second * 5)
		}
		//once all goroutines are signalled, send close to main thread
		httpServer.Shutdown(context.Background())
		close(connectionsClose)
	}()

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