blob: 37b154b3dd1c5b81bbadefc1cd6840b3d8c497c5 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// This file implements an exit handler that tries to shut down all the
18// running servers before finally exiting. There are 2 triggers to this
19// clean exit thread: signals and an exit channel.
20
21package afrouter
22
23import (
24 "github.com/opencord/voltha-go/common/log"
25 "os"
26 "os/signal"
27 "syscall"
28)
29
30var errChan = make(chan error)
31var doneChan = make(chan error)
32
33func InitExitHandler() error {
34
35 // Start the signal handler
36 go signalHandler()
37 // Start the error handler
38 go errHandler()
39
40 return nil
41}
42
43func signalHandler() {
44 // Make signal channel and register notifiers for Interupt and Terminate
45 sigchan := make(chan os.Signal, 1)
46 signal.Notify(sigchan, os.Interrupt)
47 signal.Notify(sigchan, syscall.SIGTERM)
48 signal.Notify(sigchan, syscall.SIGKILL)
49
50 // Block until we receive a signal on the channel
51 <-sigchan
52
53 log.Info("shutting down on signal as requested")
54
55 cleanExit(nil)
56}
57
58func errHandler() {
59
60 err := <-errChan
61
62 cleanExit(err)
63}
64
65func cleanExit(err error) {
66 // Log the shutdown
67 if arProxy != nil {
68 for _, srvr := range arProxy.servers {
69 if srvr.running {
70 log.With(log.Fields{"server": srvr.name}).Debug("Closing server")
71 srvr.proxyServer.GracefulStop()
72 srvr.proxyListener.Close()
73 }
74 }
75 }
76 for _, cl := range clusters {
77 for _, bknd := range cl.backends {
78 log.Debugf("Closing backend %s", bknd.name)
79 for _, conn := range bknd.connections {
80 log.Debugf("Closing connection %s", conn.name)
81 conn.close()
82 }
83 }
84 }
85 doneChan <- err
86 //os.Exit(0)
87}