// Copyright 2019 getensh.com. All rights reserved.
// Use of this source code is governed by getensh.com.

package signal

import (
	"gd_management_gateway/controllers/ctrl_v1"
	"os"
	"os/signal"
	"syscall"
	"time"
)

var ExitRecieved bool

func HandleExitSignal() {
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGTERM)
	go func() {
		<-sigs
		ExitRecieved = true
		ctrl_v1.ExitRecieved = true
		t := time.NewTicker(1 * time.Second)
		count := 0
		for {
			select {
			case <-t.C:
				count++
				if count >= 5 && ctrl_v1.IsApiTaskRun() == false {
					os.Exit(0)
				}
				if count >= 10 {
					os.Exit(0)
				}
			}
		}
	}()
}
