2021-04-18 19:41:13 +00:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-06-16 03:20:38 +00:00
|
|
|
|
2016-08-18 23:23:42 +00:00
|
|
|
package cmd
|
2016-06-16 03:20:38 +00:00
|
|
|
|
|
|
|
import (
|
2018-03-15 20:27:16 +00:00
|
|
|
"context"
|
2020-09-12 06:02:32 +00:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
2016-06-16 03:20:38 +00:00
|
|
|
"os"
|
2018-11-12 23:07:16 +00:00
|
|
|
"strings"
|
2018-04-05 22:04:40 +00:00
|
|
|
|
2021-06-01 21:59:40 +00:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2016-06-16 03:20:38 +00:00
|
|
|
)
|
|
|
|
|
2017-07-12 23:33:21 +00:00
|
|
|
func handleSignals() {
|
|
|
|
// Custom exit function
|
2019-08-13 04:25:34 +00:00
|
|
|
exit := func(success bool) {
|
2017-07-12 23:33:21 +00:00
|
|
|
// If global profiler is set stop before we exit.
|
2020-01-11 01:19:58 +00:00
|
|
|
globalProfilerMu.Lock()
|
|
|
|
defer globalProfilerMu.Unlock()
|
2020-01-21 23:49:25 +00:00
|
|
|
for _, p := range globalProfiler {
|
|
|
|
p.Stop()
|
2017-07-12 23:33:21 +00:00
|
|
|
}
|
2016-06-16 03:20:38 +00:00
|
|
|
|
2019-08-13 04:25:34 +00:00
|
|
|
if success {
|
2017-07-12 23:33:21 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2016-06-16 03:20:38 +00:00
|
|
|
|
2017-07-12 23:33:21 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-06-16 03:20:38 +00:00
|
|
|
|
2017-07-12 23:33:21 +00:00
|
|
|
stopProcess := func() bool {
|
|
|
|
var err, oerr error
|
2016-06-16 03:20:38 +00:00
|
|
|
|
2020-09-08 16:10:55 +00:00
|
|
|
// send signal to various go-routines that they need to quit.
|
|
|
|
cancelGlobalContext()
|
|
|
|
|
2022-08-24 13:42:36 +00:00
|
|
|
if globalEventNotifier != nil {
|
|
|
|
globalEventNotifier.RemoveAllRemoteTargets()
|
2018-03-15 20:03:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 23:54:26 +00:00
|
|
|
if httpServer := newHTTPServerFn(); httpServer != nil {
|
|
|
|
err = httpServer.Shutdown()
|
2020-09-12 06:02:32 +00:00
|
|
|
if !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
}
|
2019-12-02 23:54:26 +00:00
|
|
|
}
|
2016-06-16 03:20:38 +00:00
|
|
|
|
2020-10-09 16:59:52 +00:00
|
|
|
if objAPI := newObjectLayerFn(); objAPI != nil {
|
2018-03-15 20:27:16 +00:00
|
|
|
oerr = objAPI.Shutdown(context.Background())
|
2018-04-05 22:04:40 +00:00
|
|
|
logger.LogIf(context.Background(), oerr)
|
2017-07-12 23:33:21 +00:00
|
|
|
}
|
2016-06-16 03:20:38 +00:00
|
|
|
|
2021-11-19 01:19:58 +00:00
|
|
|
if srv := newConsoleServerFn(); srv != nil {
|
|
|
|
logger.LogIf(context.Background(), srv.Shutdown())
|
2021-09-01 01:52:48 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 23:33:21 +00:00
|
|
|
return (err == nil && oerr == nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2022-10-21 21:42:28 +00:00
|
|
|
case err := <-globalHTTPServerErrorCh:
|
|
|
|
logger.LogIf(context.Background(), err)
|
2020-09-08 16:10:55 +00:00
|
|
|
exit(stopProcess())
|
2017-07-12 23:33:21 +00:00
|
|
|
case osSignal := <-globalOSSignalCh:
|
2018-11-12 23:07:16 +00:00
|
|
|
logger.Info("Exiting on signal: %s", strings.ToUpper(osSignal.String()))
|
2017-07-12 23:33:21 +00:00
|
|
|
exit(stopProcess())
|
|
|
|
case signal := <-globalServiceSignalCh:
|
2019-08-28 22:04:43 +00:00
|
|
|
switch signal {
|
|
|
|
case serviceRestart:
|
2018-04-10 16:37:14 +00:00
|
|
|
logger.Info("Restarting on service signal")
|
2018-05-31 19:30:15 +00:00
|
|
|
stop := stopProcess()
|
2017-07-12 23:33:21 +00:00
|
|
|
rerr := restartProcess()
|
2018-04-05 22:04:40 +00:00
|
|
|
logger.LogIf(context.Background(), rerr)
|
2018-05-31 19:30:15 +00:00
|
|
|
exit(stop && rerr == nil)
|
2019-08-28 22:04:43 +00:00
|
|
|
case serviceStop:
|
2018-04-10 16:37:14 +00:00
|
|
|
logger.Info("Stopping on service signal")
|
2017-07-12 23:33:21 +00:00
|
|
|
exit(stopProcess())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 03:20:38 +00:00
|
|
|
}
|