blob: 37b154b3dd1c5b81bbadefc1cd6840b3d8c497c5 [file] [log] [blame]
sslobodr392ebd52019-01-18 12:41:49 -05001/*
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 */
sslobodr392ebd52019-01-18 12:41:49 -050016
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 (
sslobodr392ebd52019-01-18 12:41:49 -050024 "github.com/opencord/voltha-go/common/log"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040025 "os"
26 "os/signal"
27 "syscall"
sslobodr392ebd52019-01-18 12:41:49 -050028)
29
30var errChan = make(chan error)
31var doneChan = make(chan error)
sslobodr392ebd52019-01-18 12:41:49 -050032
sslobodr392ebd52019-01-18 12:41:49 -050033func 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)
sslobodrd6e07e72019-01-31 16:07:20 -050048 signal.Notify(sigchan, syscall.SIGKILL)
sslobodr392ebd52019-01-18 12:41:49 -050049
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 {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040070 log.With(log.Fields{"server": srvr.name}).Debug("Closing server")
71 srvr.proxyServer.GracefulStop()
72 srvr.proxyListener.Close()
sslobodr392ebd52019-01-18 12:41:49 -050073 }
74 }
75 }
Kent Hagerman1e9061e2019-05-21 16:01:21 -040076 for _, cl := range clusters {
sslobodr5f0b5a32019-01-24 07:45:19 -050077 for _, bknd := range cl.backends {
sslobodr392ebd52019-01-18 12:41:49 -050078 log.Debugf("Closing backend %s", bknd.name)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040079 for _, conn := range bknd.connections {
sslobodr392ebd52019-01-18 12:41:49 -050080 log.Debugf("Closing connection %s", conn.name)
81 conn.close()
82 }
83 }
84 }
85 doneChan <- err
86 //os.Exit(0)
87}