blob: 950537c885f38fc3f2289b5a8b2df6ead1cf50c2 [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)
Scott Bakere7144bc2019-10-01 14:16:47 -070048
49 // Block until we receive a signal on the channel
50 <-sigchan
51
52 log.Info("shutting down on signal as requested")
53
54 cleanExit(nil)
55}
56
57func errHandler() {
58
59 err := <-errChan
60
61 cleanExit(err)
62}
63
64func cleanExit(err error) {
65 // Log the shutdown
66 if arProxy != nil {
67 for _, srvr := range arProxy.servers {
68 if srvr.running {
69 log.With(log.Fields{"server": srvr.name}).Debug("Closing server")
70 srvr.proxyServer.GracefulStop()
71 srvr.proxyListener.Close()
72 }
73 }
74 }
75 for _, cl := range clusters {
76 for _, bknd := range cl.backends {
77 log.Debugf("Closing backend %s", bknd.name)
78 for _, conn := range bknd.connections {
79 log.Debugf("Closing connection %s", conn.name)
80 conn.close()
81 }
82 }
83 }
84 doneChan <- err
85 //os.Exit(0)
86}